3 # jobd - perform Girocco maintenance jobs
5 # Run with --help for details
12 use POSIX
":sys_wait_h";
23 my $lockfile = "/tmp/jobd.lock";
31 my $p = $job->{'project'};
32 check_project_exists
($job) || return;
33 (-e
"$Girocco::Config::reporoot/$p.git/.nofetch") && do {
38 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
43 my $p = $job->{'project'};
44 check_project_exists
($job) || return;
45 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
51 project
=> $job->{'project'},
53 command
=> \
&gc_project
,
58 sub check_project_exists
{
60 my $p = $job->{'project'};
61 if (!-d
"$Girocco::Config::reporoot/$p.git") {
62 error
("Warning: skipping non-existent project: $job->{project}")
75 command
=> \
&update_project
,
76 on_success
=> \
&setup_gc
,
77 on_error
=> \
&setup_gc
,
82 queue_one
($_) for (Girocco
::Project
->get_full_list());
85 ######### Daemon operation {{{1
96 error
("Waiting for outstanding jobs to finish... ".
97 "^C again to exit immediately");
100 $SIG{'INT'} = \
&handle_exit
;
104 error
("Killing outstanding jobs...");
105 $SIG{'TERM'} = 'IGNORE';
107 kill 'KILL', $_->{'pid'};
109 unlink $lockfile if ($locked);
115 $opts{'queued_at'} = time;
116 $opts{'dont_run'} = 0;
117 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
125 $job->{'command'}->($job);
126 if ($job->{'dont_run'}) {
135 "[".$job->{'type'}."::".$job->{'project'}."]";
138 # Only one of those per job!
139 sub exec_job_command
{
140 my ($job, $command, $err_only) = @_;
143 if (!defined($pid = fork)) {
144 error
(_job_name
($job) ." Can't fork job: $!");
145 $job->{'finished'} = 1;
150 open STDOUT
, '>/dev/null' || do {
151 error
(_job_name
($job) ." Can't write to /dev/null: $!");
152 $job->{'finished'} = 1;
157 select(undef, undef, undef, 0.25);
159 # Stop perl from complaining
162 $job->{'pid'} = $pid;
163 $job->{'finished'} = 0;
164 $job->{'started_at'} = time;
169 $job->{'dont_run'} = 1;
172 sub reap_hanging_jobs
{
174 if ((time - $_->{'started_at'}) > $kill_after) {
175 $_->{'finished'} = 1;
176 kill 'KILL', $_->{'pid'};
177 print STDERR _job_name
($_) ." KILLED due to timeout\n";
178 push @jobs_killed, _job_name
($_);
183 sub reap_finished_jobs
{
186 $pid = waitpid(-1, WNOHANG
);
189 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
191 # XXX- we currently don't care
193 $child[0]->{'finished'} = 2 if (@child && !$child[0]->{'finished'});
194 my $status = $child[0]->{'finished'};
195 if ($status == 0) { next; }
196 elsif ($status == 1 && defined($child[0]->{'on_error'})) {
197 $child[0]->{'on_error'}->($_);
198 } elsif ($status == 2 && defined($child[0]->{'on_success'})) {
199 $child[0]->{'on_success'}->($_);
203 @running = grep { $_->{'finished'} == 0 } @running;
206 sub have_intensive_jobs
{
207 grep { $_->{'intensive'} == 1 } @running;
211 my $last_progress = time;
216 printf STDERR
"--- Processing %d queued jobs\n", scalar(@queue);
218 $SIG{'INT'} = \
&handle_softexit
;
219 $SIG{'TERM'} = \
&handle_exit
;
220 while (@queue || @running) {
222 reap_finished_jobs
();
223 # Back off if we're too busy
224 if (@running >= $max_par || have_intensive_jobs
() || !@queue) {
226 if ($progress && (time - $last_progress) >= 60) {
227 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);
231 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
233 error
("STATUS: currently running: ". join(', ', @run_status));
235 $last_progress = time;
240 run_job
(shift(@queue)) if @queue;
243 printf STDERR
"--- Queue processed. %d jobs executed, %d skipped, %d killed. Now restarting.\n", $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
247 sub run_perpetually
{
249 die "Lockfile exists. Please make sure no other instance of jobd is running.";
251 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
263 ######### Helpers {{{1
266 print STDERR
shift()."\n";
276 Getopt
::Long
::Configure
('bundling');
277 my $parse_res = GetOptions
(
278 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
279 'quiet|q' => \
$quiet,
280 'progress|P' => \
$progress,
281 'kill-after|k=i' => \
$kill_after,
282 'max-parallel|p=i' => \
$max_par,
283 'lockfile|l=s' => \
$lockfile,
284 'all-once|a' => \
$all_once,
287 fatal
("Error: can only use one out of --all-once and --one")
288 if ($all_once && $one);
291 $ENV{'show_progress'} = '1';
309 ########## Documentation {{{1
315 jobd - Perform Girocco maintenance jobs
322 -h | --help detailed instructions
323 -q | --quiet run quietly
324 -P | --progress show occasional status updates
325 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
326 -p NUM | --max-parallel=NUM how many jobs to run at the same time
327 -l FILE | --lockfile=FILE create a lockfile in the given location
328 -a | --all-once process the list only once
329 -o PRJNAME | --one=PRJNAME process only one project
337 Print the full description of jobd's options.
341 Suppress non-error messages, e.g. for use when running this task as a cronjob.
345 Show information about the current status of the job queue occasionally. This
346 is automatically enabled if --quiet is not given.
348 =item B<--kill-after=SECONDS>
350 Kill supervised jobs after a certain time to avoid hanging the daemon.
352 =item B<--max-parallel=NUM>
354 Run no more than that many jobs at the same time.
356 =item B<--lockfile=FILE>
358 For perpetual operation, create a lockfile in that place and clean it up after
363 Instead of perpetuously processing all projects over and over again, process
364 them just once and then exit.
366 =item B<--one=PRJNAME>
368 Process only the given project (given as just the project name without C<.git>
369 suffix) and then exit.
375 jobd is Girocco's repositories maintenance servant; it periodically checks all
376 the repositories and updates mirrored repositories and repacks push-mode
377 repositories when needed.