aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Iurt/Process.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Iurt/Process.pm')
-rw-r--r--lib/Iurt/Process.pm59
1 files changed, 29 insertions, 30 deletions
diff --git a/lib/Iurt/Process.pm b/lib/Iurt/Process.pm
index 46b8de2..c234d0b 100644
--- a/lib/Iurt/Process.pm
+++ b/lib/Iurt/Process.pm
@@ -14,8 +14,8 @@ our @EXPORT = qw(
clean_process
check_pid
perform_command
- set_alarm_message
sudo
+ wait_for_lock
);
my $sudo = '/usr/bin/sudo';
@@ -24,6 +24,7 @@ my $sudo = '/usr/bin/sudo';
Check that there is no other program running and create a pidfile lock
I<$run> current running options
+I<$exit> whether to exit when the lock is in use
Return true.
=cut
@@ -32,7 +33,7 @@ Return true.
# should be designed
sub check_pid {
- my ($run) = @_;
+ my ($run, $exit) = @_;
my $pidfile = "$run->{pidfile_home}/$run->{pidfile}";
@@ -59,9 +60,7 @@ sub check_pid {
if ($time < time()-7200 || $state eq 'Z') {
my $i;
- my $msg = "another instance [$pid] is too old, killing it";
- set_alarm_message($msg);
- plog('WARN', $msg);
+ plog('WARN', "another instance [$pid] is too old, killing it");
while ($i < 5 && getpgrp $pid != -1) {
kill_for_good($pid);
@@ -69,9 +68,10 @@ sub check_pid {
sleep 1;
}
} else {
- plog('WARN', "another instance [$pid] is already running for ",
- time()-$time, " seconds");
- exit();
+ plog('WARN', "another instance [$pid] is already running for",
+ time()-$time, "seconds");
+ exit() if $exit;
+ return;
}
} else {
plog('WARN', "cleaning stale lockfile");
@@ -84,6 +84,20 @@ sub check_pid {
$pidfile;
}
+sub wait_for_lock {
+ my ($run) = @_;
+
+ plog('INFO', 'Waiting for ulri lock to be available');
+ my $delay = 10;
+ my $pidfile = check_pid($run, 0);
+ while (!defined($pidfile)) {
+ plog('WARN', "waiting $delay seconds before retrying");
+ sleep $delay;
+ $pidfile = check_pid($run, 0);
+ }
+
+ $pidfile;
+}
sub fork_to_monitor {
my ($run, $config, $logfile, %opt) = @_;
@@ -105,18 +119,14 @@ sub fork_to_monitor {
my (@stat) = stat $logfile;
if ($stat[7] > $size_limit) {
# FIXME: we left runaway processes (eg: urpmi)
- my $msg = "Killing current command because of log size exceeding limit ($stat[7] > $config->{log_size_limit})";
- set_alarm_message($msg);
- plog('ERROR', $msg);
+ plog('ERROR', "Killing current command because of log size exceeding limit ($stat[7] > $config->{log_size_limit})");
kill 14, "-$parent_pid";
exit();
}
if ($opt{stalled_timeout} && $stat[9] && $stat[9] + $opt{stalled_timeout} < time()) {
# If nothing was written to the logfile for more than stalled_timeout, check if the system seems busy
if ((getload())[1] < 0.5) {
- my $msg = "Killing current command because it seems blocked";
- set_alarm_message($msg);
- plog('ERROR', $msg);
+ plog('ERROR', "Killing current command because it seems blocked");
kill 14, "-$parent_pid";
exit();
}
@@ -125,8 +135,7 @@ sub fork_to_monitor {
my $df = df $opt{log};
if ($df->{per} >= 99) {
# FIXME: we left runaway processes (eg: urpmi)
- set_alarm_message(my $msg = "Killing current command because running out of disk space at $opt{log} (only $df->{bavail}KB left)");
- plog('ERROR', $msg);
+ plog('ERROR', "Killing current command because running out of disk space at $opt{log} (only $df->{bavail}KB left)");
kill 14, "-$parent_pid";
exit();
}
@@ -192,7 +201,7 @@ sub handle_wait_regexp {
sub generate_comment {
my ($run, $config, $output, $command, $comment, $pipe, $kill, %opt) = @_;
if ($kill && $opt{type} ne 'shell') {
- $comment = "Command killed after $opt{timeout}s: $command\n";
+ $comment = "Command killed: $command\n";
my ($cmd_to_kill) = $command =~ /sudo(?: chroot \S+)? (.*)/;
clean_process($cmd_to_kill);
} elsif ($pipe) {
@@ -210,13 +219,6 @@ sub generate_comment {
}
}
-my $alarm_message;
-
-sub set_alarm_message {
- my ($msg) = @_;
- $alarm_message = $msg;
-}
-
=head2 perform_command($command, $run, $config, %opt)
Run a command and check various running parameters such as log size, timeout...
@@ -258,19 +260,18 @@ sub perform_command {
my $retry = $opt{retry} || 1;
my $call_ret = 1;
my ($err, $try);
- my $logfile = "$opt{log}/$opt{logname}.$run->{run}.log";
+ my $logfile = "$opt{log}/$opt{logname}.$run->{my_arch}.$run->{run}.log";
my $max_retry = max($config->{max_command_retry}, $retry);
while ($retry) {
$try++;
- $logfile = "$opt{log}/$opt{logname}-$try.$run->{run}.log" if $opt{retry} > 1;
+ $logfile = "$opt{log}/$opt{logname}-$try.$run->{my_arch}.$run->{run}.log" if $opt{retry} > 1;
my $pid = $opt{log} ? fork_to_monitor($run, $config, $logfile, %opt) : 0;
eval {
# handle timeout:
- set_alarm_message("Timeout! (timeout was $opt{timeout})");
local $SIG{ALRM} = sub {
- print $alarm_message, "\n";
+ print "Killed! (probably because of the $opt{timeout} timeout)\n";
$kill = 1;
die "alarm\n"; # NB: \n required
};
@@ -306,7 +307,6 @@ sub perform_command {
$err = 0 if any { $_ == $err } @{$opt{error_ok}};
# kill pid watching log file size
- set_alarm_message("kill pid watching log file size");
kill_for_good($pid) if $pid;
if ($perl_err) { # timed out
@@ -382,7 +382,6 @@ sub kill_for_good {
my ($pid) = @_;
# try SIGALARM first:
- set_alarm_message("Killing current command because it seems blocked");
kill 14, $pid;
sleep 1;
waitpid(-1, POSIX::WNOHANG);