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{'CHLD'} = 'IGNORE';
105 $SIG{'TERM'} = 'IGNORE';
107 kill 'KILL', $_->{'pid'};
109 unlink $lockfile if ($locked);
113 sub handle_childgone
{
116 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
118 # XXX- we currently don't care
120 $child[0]->{'finished'} = 2 if (@child);
124 $SIG{'CHLD'} = \
&handle_childgone
;
129 $opts{'queued_at'} = time;
130 $opts{'dont_run'} = 0;
131 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
139 $job->{'command'}->($job);
140 if ($job->{'dont_run'}) {
149 "[".$job->{'type'}."::".$job->{'project'}."]";
152 # Only one of those per job!
153 sub exec_job_command
{
154 my ($job, $command, $err_only) = @_;
157 if (!defined($pid = fork)) {
158 error
(_job_name
($job) ." Can't fork job: $!");
159 $job->{'finished'} = 1;
164 open STDOUT
, '>/dev/null' || do {
165 error
(_job_name
($job) ." Can't write to /dev/null: $!");
166 $job->{'finished'} = 1;
173 $job->{'pid'} = $pid;
174 $job->{'finished'} = 0;
175 $job->{'started_at'} = time;
180 $job->{'dont_run'} = 1;
183 sub reap_hanging_jobs
{
185 if ((time - $_->{'started_at'}) > $kill_after) {
186 $_->{'finished'} = 1;
187 kill 'KILL', $_->{'pid'};
188 print STDERR _job_name
($_) ." KILLED due to timeout\n";
189 push @jobs_killed, _job_name
($_);
194 sub reap_finished_jobs
{
196 my $status = $_->{'finished'};
197 if ($status == 0) { next; }
198 elsif ($status == 1 && defined($_->{'on_error'})) {
199 $_->{'on_error'}->($_);
200 } elsif ($status == 2 && defined($_->{'on_success'})) {
201 $_->{'on_success'}->($_);
204 @running = grep { $_->{'finished'} == 0 } @running;
207 sub have_intensive_jobs
{
208 grep { $_->{'intensive'} == 1 } @running;
212 my $last_progress = time;
217 printf STDERR
"--- Processing %d queued jobs\n", scalar(@queue);
219 $SIG{'CHLD'} = \
&handle_childgone
;
220 $SIG{'INT'} = \
&handle_softexit
;
221 $SIG{'TERM'} = \
&handle_exit
;
222 while (@queue || @running) {
224 reap_finished_jobs
();
225 # Back off if we're too busy
226 if (@running >= $max_par || have_intensive_jobs
() || !@queue) {
228 if ($progress && (time - $last_progress) >= 60) {
229 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);
233 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
235 error
("STATUS: currently running: ". join(', ', @run_status));
237 $last_progress = time;
242 run_job
(shift(@queue)) if @queue;
245 printf STDERR
"--- Queue processed. %d jobs executed, %d skipped, %d killed. Now restarting.\n", $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
249 sub run_perpetually
{
251 die "Lockfile exists. Please make sure no other instance of jobd is running.";
253 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
265 ######### Helpers {{{1
268 print STDERR
shift()."\n";
278 Getopt
::Long
::Configure
('bundling');
279 my $parse_res = GetOptions
(
280 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
281 'quiet|q' => \
$quiet,
282 'progress|P' => \
$progress,
283 'kill-after|k=i' => \
$kill_after,
284 'max-parallel|p=i' => \
$max_par,
285 'lockfile|l=s' => \
$lockfile,
286 'all-once|a' => \
$all_once,
289 fatal
("Error: can only use one out of --all-once and --one")
290 if ($all_once && $one);
293 $ENV{'show_progress'} = '1';
311 ########## Documentation {{{1
317 jobd - Perform Girocco maintenance jobs
324 -h | --help detailed instructions
325 -q | --quiet run quietly
326 -P | --progress show occasional status updates
327 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
328 -p NUM | --max-parallel=NUM how many jobs to run at the same time
329 -l FILE | --lockfile=FILE create a lockfile in the given location
330 -a | --all-once process the list only once
331 -o PRJNAME | --one=PRJNAME process only one project
339 Print the full description of jobd's options.
343 Suppress non-error messages, e.g. for use when running this task as a cronjob.
347 Show information about the current status of the job queue occasionally. This
348 is automatically enabled if --quiet is not given.
350 =item B<--kill-after=SECONDS>
352 Kill supervised jobs after a certain time to avoid hanging the daemon.
354 =item B<--max-parallel=NUM>
356 Run no more than that many jobs at the same time.
358 =item B<--lockfile=FILE>
360 For perpetual operation, create a lockfile in that place and clean it up after
365 Instead of perpetuously processing all projects over and over again, process
366 them just once and then exit.
368 =item B<--one=PRJNAME>
370 Process only the given project (given as just the project name without C<.git>
371 suffix) and then exit.
377 jobd is Girocco's repositories maintenance servant; it periodically checks all
378 the repositories and updates mirrored repositories and repacks push-mode
379 repositories when needed.