3 # jobd - perform Girocco maintenance jobs
5 # Run with --help for details
22 my $lockfile = "/tmp/jobd.lock";
30 my $p = $job->{'project'};
31 check_project_exists
($job) || return;
32 (-e
"$Girocco::Config::reporoot/$p.git/.nofetch") && do {
37 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
42 my $p = $job->{'project'};
43 check_project_exists
($job) || return;
44 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
50 project
=> $job->{'project'},
52 command
=> \
&gc_project
,
57 sub check_project_exists
{
59 my $p = $job->{'project'};
60 if (!-d
"$Girocco::Config::reporoot/$p.git") {
61 error
("Warning: skipping non-existent project: $job->{project}")
74 command
=> \
&update_project
,
75 on_success
=> \
&setup_gc
,
76 on_error
=> \
&setup_gc
,
81 queue_one
($_) for (Girocco
::Project
->get_full_list());
84 ######### Daemon operation {{{1
95 error
("Waiting for outstanding jobs to finish... ".
96 "^C again to exit immediately");
99 $SIG{'INT'} = \
&handle_exit
;
103 error
("Killing outstanding jobs...");
104 $SIG{'TERM'} = 'IGNORE';
106 kill 'KILL', $_->{'pid'};
108 unlink $lockfile if ($locked);
114 $opts{'queued_at'} = time;
115 $opts{'dont_run'} = 0;
116 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
124 $job->{'command'}->($job);
125 if ($job->{'dont_run'}) {
134 "[".$job->{'type'}."::".$job->{'project'}."]";
137 # Only one of those per job!
138 sub exec_job_command
{
139 my ($job, $command, $err_only) = @_;
142 if (!defined($pid = fork)) {
143 error
(_job_name
($job) ." Can't fork job: $!");
144 $job->{'finished'} = 1;
149 open STDOUT
, '>/dev/null' || do {
150 error
(_job_name
($job) ." Can't write to /dev/null: $!");
151 $job->{'finished'} = 1;
156 select(undef, undef, undef, 0.25);
158 # Stop perl from complaining
161 $job->{'pid'} = $pid;
162 $job->{'finished'} = 0;
163 $job->{'started_at'} = time;
168 $job->{'dont_run'} = 1;
171 sub reap_hanging_jobs
{
173 if ((time - $_->{'started_at'}) > $kill_after) {
174 $_->{'finished'} = 1;
175 kill 'KILL', $_->{'pid'};
176 print STDERR _job_name
($_) ." KILLED due to timeout\n";
177 push @jobs_killed, _job_name
($_);
182 sub reap_finished_jobs
{
185 $pid = waitpid(-1, WNOHANG
);
188 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
190 # XXX- we currently don't care
192 $child[0]->{'finished'} = 2 if (@child && !$child[0]->{'finished'});
193 my $status = $child[0]->{'finished'};
194 if ($status == 0) { next; }
195 elsif ($status == 1 && defined($child[0]->{'on_error'})) {
196 $child[0]->{'on_error'}->($_);
197 } elsif ($status == 2 && defined($child[0]_
->{'on_success'})) {
198 $child[0]->{'on_success'}->($_);
202 @running = grep { $_->{'finished'} == 0 } @running;
205 sub have_intensive_jobs
{
206 grep { $_->{'intensive'} == 1 } @running;
210 my $last_progress = time;
215 printf STDERR
"--- Processing %d queued jobs\n", scalar(@queue);
217 $SIG{'INT'} = \
&handle_softexit
;
218 $SIG{'TERM'} = \
&handle_exit
;
219 while (@queue || @running) {
221 reap_finished_jobs
();
222 # Back off if we're too busy
223 if (@running >= $max_par || have_intensive_jobs
() || !@queue) {
225 if ($progress && (time - $last_progress) >= 60) {
226 printf STDERR
"STATUS: %d queued, %d running, %d finished, %d skipped, %d killed\n", scalar(@queue), scalar(@running), $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
230 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
232 error
("STATUS: currently running: ". join(', ', @run_status));
234 $last_progress = time;
239 run_job
(shift(@queue)) if @queue;
242 printf STDERR
"--- Queue processed. %d jobs executed, %d skipped, %d killed. Now restarting.\n", $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
246 sub run_perpetually
{
248 die "Lockfile exists. Please make sure no other instance of jobd is running.";
250 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
262 ######### Helpers {{{1
265 print STDERR
shift()."\n";
275 Getopt
::Long
::Configure
('bundling');
276 my $parse_res = GetOptions
(
277 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
278 'quiet|q' => \
$quiet,
279 'progress|P' => \
$progress,
280 'kill-after|k=i' => \
$kill_after,
281 'max-parallel|p=i' => \
$max_par,
282 'lockfile|l=s' => \
$lockfile,
283 'all-once|a' => \
$all_once,
286 fatal
("Error: can only use one out of --all-once and --one")
287 if ($all_once && $one);
290 $ENV{'show_progress'} = '1';
308 ########## Documentation {{{1
314 jobd - Perform Girocco maintenance jobs
321 -h | --help detailed instructions
322 -q | --quiet run quietly
323 -P | --progress show occasional status updates
324 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
325 -p NUM | --max-parallel=NUM how many jobs to run at the same time
326 -l FILE | --lockfile=FILE create a lockfile in the given location
327 -a | --all-once process the list only once
328 -o PRJNAME | --one=PRJNAME process only one project
336 Print the full description of jobd's options.
340 Suppress non-error messages, e.g. for use when running this task as a cronjob.
344 Show information about the current status of the job queue occasionally. This
345 is automatically enabled if --quiet is not given.
347 =item B<--kill-after=SECONDS>
349 Kill supervised jobs after a certain time to avoid hanging the daemon.
351 =item B<--max-parallel=NUM>
353 Run no more than that many jobs at the same time.
355 =item B<--lockfile=FILE>
357 For perpetual operation, create a lockfile in that place and clean it up after
362 Instead of perpetuously processing all projects over and over again, process
363 them just once and then exit.
365 =item B<--one=PRJNAME>
367 Process only the given project (given as just the project name without C<.git>
368 suffix) and then exit.
374 jobd is Girocco's repositories maintenance servant; it periodically checks all
375 the repositories and updates mirrored repositories and repacks push-mode
376 repositories when needed.