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;
149 open STDIN
, '/dev/null' || do {
150 error
(_job_name
($job) ."Can't read from /dev/null: $!");
151 $job->{'finished'} = 1;
155 open STDOUT
, '>/dev/null' || do {
156 error
(_job_name
($job) ." Can't write to /dev/null: $!");
157 $job->{'finished'} = 1;
162 select(undef, undef, undef, 0.1);
164 # Stop perl from complaining
167 $job->{'pid'} = $pid;
168 $job->{'finished'} = 0;
169 $job->{'started_at'} = time;
174 $job->{'dont_run'} = 1;
177 sub reap_hanging_jobs
{
179 if (defined($_->{'started_at'}) && (time - $_->{'started_at'}) > $kill_after) {
180 $_->{'finished'} = 1;
181 kill 'KILL', $_->{'pid'};
182 print STDERR _job_name
($_) ." KILLED due to timeout\n";
183 push @jobs_killed, _job_name
($_);
188 sub reap_finished_jobs
{
191 $pid = waitpid(-1, WNOHANG
);
194 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
196 # XXX- we currently don't care
198 if (@child && !$child[0]->{'finished'}) {
199 $child[0]->{'on_success'}->($child[0]) if defined($child[0]->{'on_success'});
200 $child[0]->{'finished'} = 1;
203 $child[0]->{'on_error'}->($child[0]) if defined($child[0]->{'on_error'});
206 @running = grep { $_->{'finished'} == 0 } @running;
209 sub have_intensive_jobs
{
210 grep { $_->{'intensive'} == 1 } @running;
214 my $last_progress = time;
219 printf STDERR
"--- Processing %d queued jobs\n", scalar(@queue);
221 $SIG{'INT'} = \
&handle_softexit
;
222 $SIG{'TERM'} = \
&handle_exit
;
223 while (@queue || @running) {
225 reap_finished_jobs
();
226 # Back off if we're too busy
227 if (@running >= $max_par || have_intensive_jobs
() || !@queue) {
229 if ($progress && (time - $last_progress) >= 60) {
230 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);
234 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
236 error
("STATUS: currently running: ". join(', ', @run_status));
238 $last_progress = time;
243 run_job
(shift(@queue)) if @queue;
246 printf STDERR
"--- Queue processed. %d jobs executed, %d skipped, %d killed. Now restarting.\n", $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
250 sub run_perpetually
{
252 die "Lockfile exists. Please make sure no other instance of jobd is running.";
254 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
266 ######### Helpers {{{1
269 print STDERR
shift()."\n";
279 Getopt
::Long
::Configure
('bundling');
280 my $parse_res = GetOptions
(
281 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
282 'quiet|q' => \
$quiet,
283 'progress|P' => \
$progress,
284 'kill-after|k=i' => \
$kill_after,
285 'max-parallel|p=i' => \
$max_par,
286 'lockfile|l=s' => \
$lockfile,
287 'all-once|a' => \
$all_once,
290 fatal
("Error: can only use one out of --all-once and --one")
291 if ($all_once && $one);
294 $ENV{'show_progress'} = '1';
312 ########## Documentation {{{1
318 jobd - Perform Girocco maintenance jobs
325 -h | --help detailed instructions
326 -q | --quiet run quietly
327 -P | --progress show occasional status updates
328 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
329 -p NUM | --max-parallel=NUM how many jobs to run at the same time
330 -l FILE | --lockfile=FILE create a lockfile in the given location
331 -a | --all-once process the list only once
332 -o PRJNAME | --one=PRJNAME process only one project
340 Print the full description of jobd's options.
344 Suppress non-error messages, e.g. for use when running this task as a cronjob.
348 Show information about the current status of the job queue occasionally. This
349 is automatically enabled if --quiet is not given.
351 =item B<--kill-after=SECONDS>
353 Kill supervised jobs after a certain time to avoid hanging the daemon.
355 =item B<--max-parallel=NUM>
357 Run no more than that many jobs at the same time.
359 =item B<--lockfile=FILE>
361 For perpetual operation, create a lockfile in that place and clean it up after
366 Instead of perpetuously processing all projects over and over again, process
367 them just once and then exit.
369 =item B<--one=PRJNAME>
371 Process only the given project (given as just the project name without C<.git>
372 suffix) and then exit.
378 jobd is Girocco's repositories maintenance servant; it periodically checks all
379 the repositories and updates mirrored repositories and repacks push-mode
380 repositories when needed.