3 # jobd - perform Girocco maintenance jobs
5 # Run with --help for details
12 use POSIX
":sys_wait_h";
26 my $max_par_intensive = 1;
27 my $cpus = online_cpus
;
28 my $load_triggers = $cpus ?
sprintf("%g,%g", $cpus * 1.5, $cpus * 0.75) : "6,3";
29 my $lockfile = "/tmp/jobd-$Girocco::Config::tmpsuffix.lock";
30 my $restart_delay = 300;
34 my ($load_trig, $load_untrig);
40 my $p = $job->{'project'};
41 check_project_exists
($job) || return;
42 if (-e get_project_path
($p).".nofetch" || -e get_project_path
($p).".bypass" ||
43 -e get_project_path
($p).".bypass_fetch") {
45 return setup_gc
($job);
47 if (-e get_project_path
($p).".clone_in_progress" && ! -e get_project_path
($p).".clone_failed") {
48 job_skip
($job, "initial mirroring not complete yet");
51 if (-e get_project_path
($p).".clone_failed") {
52 job_skip
($job, "initial mirroring failed");
53 # Still need to gc non top-level clones even if they've failed
54 # otherwise the objects copied into them from the parent will
55 # just accumulate without bound
56 setup_gc
($job) if $p =~ m
,/,;
59 if (my $ts = is_operation_uptodate
($p, 'lastrefresh', rand_adjust
($Girocco::Config
::min_mirror_interval
))) {
60 job_skip
($job, "not needed right now, last run at $ts");
64 if (is_svn_clone
($p)) {
65 # git svn can be very, very slow at times
66 $job->{'timeout_factor'} = 3;
68 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
73 my $p = $job->{'project'};
74 check_project_exists
($job) || return;
75 if (-e get_project_path
($p).".nogc" || -e get_project_path
($p).".bypass") {
79 if (my $ts = is_operation_uptodate
($p, 'lastgc', rand_adjust
($Girocco::Config
::min_gc_interval
))) {
80 job_skip
($job, "not needed right now, last run at $ts");
83 # allow garbage collection to run for longer than an update
84 $job->{'timeout_factor'} = 2;
85 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
91 project
=> $job->{'project'},
93 command
=> \
&gc_project
,
98 sub check_project_exists
{
100 my $p = $job->{'project'};
101 if (!-d get_project_path
($p)) {
102 job_skip
($job, "non-existent project");
108 sub get_project_path
{
109 "$Girocco::Config::reporoot/".shift().".git/";
112 sub is_operation_uptodate
{
113 my ($project, $which, $threshold) = @_;
114 my $path = get_project_path
($project);
115 my $timestamp = get_git
("--git-dir=$path", 'config', "gitweb.$which");
116 defined($timestamp) or $timestamp = '';
118 my $unix_ts = parse_rfc2822_date
($timestamp) || 0;
119 (time - $unix_ts) <= $threshold ?
$timestamp : undef;
124 my $path = get_project_path
($project);
125 my $baseurl = get_git
("--git-dir=$path", 'config', 'gitweb.baseurl');
126 defined($baseurl) or $baseurl = '';
128 my $svnurl = get_git
("--git-dir=$path", 'config', 'svn-remote.svn.url');
129 defined($svnurl) or $svnurl = '';
131 return $baseurl =~ /^svn[:+]/i && $svnurl;
139 command
=> \
&update_project
,
140 on_success
=> \
&setup_gc
,
141 on_error
=> \
&setup_gc
,
146 queue_one
($_) for (Girocco
::Project
->get_full_list());
149 ######### Daemon operation {{{1
159 # Kills and reaps the specified pid. Returns exit status ($?) on success
160 # otherwise undef if process could not be killed or reaped
161 # First sends SIGTERM and if process does not exit within 15 seconds then SIGKILL
162 # Usage: my $exitcode = kill_gently($pid, $kill_process_group = 0);
165 my $use_pg = shift || 0;
166 # Note that the docs for Perl's kill state that a negative signal
167 # number should be used to kill process groups and that while a
168 # a negative process id (and positive signal number) may also do that
169 # on some platforms, that's not portable.
170 my $pg = $use_pg ?
-1 : 1;
171 my $harsh = time() + 15; # SIGKILL after this delay
172 my $count = kill(15*$pg, $targ); # SIGTERM is 15
173 my $reaped = waitpid($targ, WNOHANG
);
174 return undef if $reaped < 0;
175 return $?
if $reaped == $targ;
176 while ($count && time() < $harsh) {
177 select(undef, undef, undef, 0.2);
178 $reaped = waitpid($targ, WNOHANG
);
179 return undef if $reaped < 0;
180 return $?
if $reaped == $targ;
183 $count = kill(9*$pg, $targ); # SIGKILL is 9
184 $reaped = waitpid($targ, WNOHANG
);
185 return undef if $reaped < 0;
186 return $?
if $reaped == $targ;
187 # We should not need to wait to reap a SIGKILL, however, just in case
188 # the system doesn't make a SIGKILL'd process immediately reapable
189 # (perhaps under extremely heavy load) we accomodate a brief delay
190 while ($count && time() < $harsh) {
191 select(undef, undef, undef, 0.2);
192 $reaped = waitpid($targ, WNOHANG
);
193 return undef if $reaped < 0;
194 return $?
if $reaped == $targ;
199 sub handle_softexit
{
200 error
("Waiting for outstanding jobs to finish... ".
201 "^C again to exit immediately");
204 $SIG{'INT'} = \
&handle_exit
;
208 error
("Killing outstanding jobs, please be patient...");
209 $SIG{'TERM'} = 'IGNORE';
211 kill_gently
($_->{'pid'}, 1);
213 unlink $lockfile if ($locked);
219 $opts{'queued_at'} = time;
220 $opts{'dont_run'} = 0;
221 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
229 $job->{'command'}->($job);
230 if ($job->{'dont_run'}) {
239 "[".$job->{'type'}."::".$job->{'project'}."]";
242 # Only one of those per job!
243 sub exec_job_command
{
244 my ($job, $command, $err_only) = @_;
247 if (!defined($pid = fork)) {
248 error
(_job_name
($job) ." Can't fork job: $!");
249 $job->{'finished'} = 1;
253 open STDIN
, '<', '/dev/null' || do {
254 error
(_job_name
($job) ."Can't read from /dev/null: $!");
255 $job->{'finished'} = 1;
259 open STDOUT
, '>', '/dev/null' || do {
260 error
(_job_name
($job) ." Can't write to /dev/null: $!");
261 $job->{'finished'} = 1;
265 # New process group so we can keep track of all of its children
266 if (!defined(POSIX
::setpgid
(0, 0))) {
267 error
(_job_name
($job) ." Can't create process group: $!");
268 $job->{'finished'} = 1;
272 select(undef, undef, undef, 0.1);
274 # Stop perl from complaining
277 $job->{'pid'} = $pid;
278 $job->{'finished'} = 0;
279 $job->{'started_at'} = time;
283 my ($job, $msg) = @_;
284 $job->{'dont_run'} = 1;
285 error
(_job_name
($job) ." Skipping job: $msg") unless $quiet || !$msg;
288 sub reap_hanging_jobs
{
290 my $factor = $_->{'timeout_factor'} || 1;
291 if (defined($_->{'started_at'}) && (time - $_->{'started_at'}) > ($kill_after * $factor)) {
292 $_->{'finished'} = 1;
293 my $exitcode = kill_gently
($_->{'pid'}, 1);
296 error
(_job_name
($_) ." KILLED due to timeout" .
297 (($exitcode & 0x7f) == 9 ?
" with SIGKILL": ""));
298 push @jobs_killed, _job_name
($_);
305 if (!$job->{'finished'}) {
306 $job->{'on_success'}->($job) if defined($job->{'on_success'});
307 $job->{'finished'} = 1;
310 $job->{'on_error'}->($job) if defined($job->{'on_error'});
314 sub reap_finished_jobs
{
316 my $finished_any = 0;
317 foreach my $child (grep { !$_->{'pid'} && $_->{'killed'} } @running) {
318 delete $child->{'killed'};
319 reap_one_job
($child);
323 $pid = waitpid(-1, WNOHANG
);
327 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
329 # XXX- we currently don't care
332 delete $child[0]->{'pid'};
333 reap_one_job
($child[0]);
336 @running = grep { $_->{'finished'} == 0 } @running;
340 sub have_intensive_jobs
{
341 grep { $_->{'intensive'} == 1 } @running;
345 "[". scalar(localtime) ."] ";
349 if ($^O
eq "linux") {
350 # Read /proc/loadavg on Linux
351 open(LOADAV
, '<', '/proc/loadavg') or return undef;
352 my $loadinfo = <LOADAV
>;
354 return (split(/\s/, $loadinfo, 4))[0..2];
356 # Read the output of uptime everywhere else (works on Linux too)
357 open(LOADAV
, '-|', 'uptime') or return undef;
358 my $loadinfo = <LOADAV
>;
360 $loadinfo =~ /load average[^0-9.]*([0-9.]+)[^0-9.]+([0-9.]+)[^0-9.]+([0-9.]+)/iso or return undef;
366 my $last_progress = time;
367 my $last_checkload = time - 5;
368 my $current_load = $load_trig;
375 ferror
("--- Processing %d queued jobs", scalar(@queue));
377 $SIG{'INT'} = \
&handle_softexit
;
378 $SIG{'TERM'} = \
&handle_exit
;
379 while (@queue || @running) {
381 my $proceed_immediately = reap_finished_jobs
();
382 # Check current system load
383 if ($load_trig && (time - $last_checkload) >= 5 && defined((my @loadinfo = get_load_info
())[0])) {
384 my $current_load = $loadinfo[0];
385 if ($current_load > $load_trig && !$overloaded) {
387 error
("PAUSE: system load is at $current_load > $load_trig") if $progress;
388 } elsif ($current_load < $load_untrig && $overloaded) {
390 error
("RESUME: system load is at $current_load < $load_untrig") if $progress;
393 $load_info = ', paused (load '. $current_load .')';
395 $load_info = ', load '. $current_load;
397 $last_checkload = time;
400 if ($progress && (time - $last_progress) >= 60) {
401 ferror
("STATUS: %d queued, %d running, %d finished, %d skipped, %d killed$load_info", scalar(@queue), scalar(@running), $jobs_executed, $jobs_skipped, scalar(@jobs_killed));
405 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
407 error
("STATUS: currently running: ". join(', ', @run_status));
409 $last_progress = time;
411 # Back off if we're too busy
412 if (@running >= $max_par || have_intensive_jobs
() >= $max_par_intensive || !@queue || $overloaded) {
413 sleep 1 unless $proceed_immediately;
417 run_job
(shift(@queue)) if @queue;
420 ferror
("--- Queue processed. %d jobs executed, %d skipped, %d killed.", $jobs_executed, $jobs_skipped, scalar(@jobs_killed));
424 sub run_perpetually
{
426 die "Lockfile '$lockfile' exists. Please make sure no other instance of jobd is running.";
428 open LOCK
, '>', $lockfile || die "Cannot create lockfile '$lockfile': $!";
436 sleep($restart_delay) if $perpetual; # Let the system breathe for a moment
441 ######### Helpers {{{1
444 print STDERR ts
().shift()."\n";
447 error
(sprintf($_[0], @_[1..$#_]));
457 Getopt
::Long
::Configure
('bundling');
458 my $parse_res = GetOptions
(
459 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
460 'quiet|q' => \
$quiet,
461 'progress|P' => \
$progress,
462 'kill-after|k=i' => \
$kill_after,
463 'max-parallel|p=i' => \
$max_par,
464 'max-intensive-parallel|i=i' => \
$max_par_intensive,
465 'load-triggers=s' => \
$load_triggers,
466 'restart-delay|d=i' => \
$restart_delay,
467 'lockfile|l=s' => \
$lockfile,
468 'all-once|a' => \
$all_once,
471 fatal
("Error: can only use one out of --all-once and --one")
472 if ($all_once && $one);
475 $ENV{'show_progress'} = '1';
479 $load_triggers = '0,0' unless defined((get_load_info
())[0]);
480 ($load_trig, $load_untrig) = split(/,/, $load_triggers);
496 ########## Documentation {{{1
502 jobd - Perform Girocco maintenance jobs
509 -h | --help detailed instructions
510 -q | --quiet run quietly
511 -P | --progress show occasional status updates
512 -k SECONDS | --kill-after SECONDS how long to wait before killing jobs
513 -p NUM | --max-parallel NUM how many jobs to run at the same time
514 -i NUM | --max-intensive-parallel NUM how many resource-hungry jobs to run
516 --load-triggers TRIG,UNTRIG stop queueing jobs at load above
517 TRIG and resume at load below UNTRIG
518 -d NUM | --restart-delay SECONDS wait for this many seconds between
520 -l FILE | --lockfile FILE create a lockfile in the given
522 -a | --all-once process the list only once
523 -o PRJNAME | --one PRJNAME process only one project
531 Print the full description of jobd's options.
535 Suppress non-error messages, e.g. for use when running this task as a cronjob.
539 Show information about the current status of the job queue occasionally. This
540 is automatically enabled if --quiet is not given.
542 =item B<--kill-after SECONDS>
544 Kill supervised jobs after a certain time to avoid hanging the daemon.
546 =item B<--max-parallel NUM>
548 Run no more than that many jobs at the same time.
550 =item B<--max-intensive-parallel NUM>
552 Run no more than that many resource-hungry jobs at the same time. Right now,
553 this refers to repacking jobs.
555 =item B<--load-triggers TRIG,UNTRIG>
557 If the first system load average (1 minute average) exceeds TRIG, don't queue
558 any more jobs until it goes below UNTRIG. This is currently only supported on
559 Linux and any other platforms that provide an uptime command with load average
562 If both values are zero, load checks are disabled. The default is the number
563 of cpus * 1.5 for TRIG and half that for UNTRIG. If the number of cpus cannot
564 be determined, the default is 6,3.
566 =item B<--restart-delay NUM>
568 After processing the queue, wait this many seconds until the queue is
569 restarted. The default is 300 seconds.
571 =item B<--lockfile FILE>
573 For perpetual operation, create a lockfile in that place and clean it up after
578 Instead of perpetuously processing all projects over and over again, process
579 them just once and then exit.
581 =item B<--one PRJNAME>
583 Process only the given project (given as just the project name without C<.git>
584 suffix) and then exit.
590 jobd is Girocco's repositories maintenance servant; it periodically checks all
591 the repositories and updates mirrored repositories and repacks push-mode
592 repositories when needed.