3 # jobd - perform Girocco maintenance jobs
5 # Run with --help for details
12 use POSIX
":sys_wait_h";
23 my $max_par_intensive = 1;
24 my $lockfile = "/tmp/jobd.lock";
25 my $restart_delay = 60;
33 my $p = $job->{'project'};
34 check_project_exists
($job) || return;
35 if (-e get_project_path
($p).".nofetch") {
37 return setup_gc
($job);
39 if (-e get_project_path
($p).".clone_in_progress") {
40 job_skip
($job, "initial mirroring not complete yet");
43 if (my $ts = is_operation_uptodate
($p, 'lastrefresh', $Girocco::Config
::min_mirror_interval
)) {
44 job_skip
($job, "not needed right now, last run at $ts");
48 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
53 my $p = $job->{'project'};
54 check_project_exists
($job) || return;
55 if (my $ts = is_operation_uptodate
($p, 'lastgc', $Girocco::Config
::min_gc_interval
)) {
56 job_skip
($job, "not needed right now, last run at $ts");
59 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
65 project
=> $job->{'project'},
67 command
=> \
&gc_project
,
72 sub check_project_exists
{
74 my $p = $job->{'project'};
75 if (!-d get_project_path
($p)) {
76 job_skip
($job, "non-existent project");
82 sub get_project_path
{
83 "$Girocco::Config::reporoot/".shift().".git/";
86 sub is_operation_uptodate
{
87 my ($project, $which, $threshold) = @_;
88 my $path = get_project_path
($project);
89 my $timestamp = `GIT_DIR="$path" $Girocco::Config::git_bin config "gitweb.$which"`;
90 my $unix_ts = `date +%s -d "$timestamp"`;
91 (time - $unix_ts) <= $threshold ?
$timestamp : undef;
99 command
=> \
&update_project
,
100 on_success
=> \
&setup_gc
,
101 on_error
=> \
&setup_gc
,
106 queue_one
($_) for (Girocco
::Project
->get_full_list());
109 ######### Daemon operation {{{1
119 sub handle_softexit
{
120 error
("Waiting for outstanding jobs to finish... ".
121 "^C again to exit immediately");
124 $SIG{'INT'} = \
&handle_exit
;
128 error
("Killing outstanding jobs...");
129 $SIG{'TERM'} = 'IGNORE';
131 kill 'KILL', -($_->{'pid'});
133 unlink $lockfile if ($locked);
139 $opts{'queued_at'} = time;
140 $opts{'dont_run'} = 0;
141 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
149 $job->{'command'}->($job);
150 if ($job->{'dont_run'}) {
159 "[".$job->{'type'}."::".$job->{'project'}."]";
162 # Only one of those per job!
163 sub exec_job_command
{
164 my ($job, $command, $err_only) = @_;
167 if (!defined($pid = fork)) {
168 error
(_job_name
($job) ." Can't fork job: $!");
169 $job->{'finished'} = 1;
173 open STDIN
, '/dev/null' || do {
174 error
(_job_name
($job) ."Can't read from /dev/null: $!");
175 $job->{'finished'} = 1;
179 open STDOUT
, '>/dev/null' || do {
180 error
(_job_name
($job) ." Can't write to /dev/null: $!");
181 $job->{'finished'} = 1;
185 # New process group so we can keep track of all of its children
186 if (!defined(POSIX
::setpgid
(0, 0))) {
187 error
(_job_name
($job) ." Can't create process group: $!");
188 $job->{'finished'} = 1;
192 select(undef, undef, undef, 0.1);
194 # Stop perl from complaining
197 $job->{'pid'} = $pid;
198 $job->{'finished'} = 0;
199 $job->{'started_at'} = time;
203 my ($job, $msg) = @_;
204 $job->{'dont_run'} = 1;
205 error
(_job_name
($job) ." Skipping job: $msg") unless $quiet || !$msg;
208 sub reap_hanging_jobs
{
210 if (defined($_->{'started_at'}) && (time - $_->{'started_at'}) > $kill_after) {
211 $_->{'finished'} = 1;
212 kill 'KILL', -($_->{'pid'});
213 error
(_job_name
($_) ." KILLED due to timeout");
214 push @jobs_killed, _job_name
($_);
219 sub reap_finished_jobs
{
221 my $finished_any = 0;
223 $pid = waitpid(-1, WNOHANG
);
227 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
229 # XXX- we currently don't care
231 if (@child && !$child[0]->{'finished'}) {
232 $child[0]->{'on_success'}->($child[0]) if defined($child[0]->{'on_success'});
233 $child[0]->{'finished'} = 1;
236 $child[0]->{'on_error'}->($child[0]) if defined($child[0]->{'on_error'});
239 @running = grep { $_->{'finished'} == 0 } @running;
243 sub have_intensive_jobs
{
244 grep { $_->{'intensive'} == 1 } @running;
248 "[". scalar(localtime) ."] ";
252 my $last_progress = time;
257 error
("--- Processing %d queued jobs", scalar(@queue));
259 $SIG{'INT'} = \
&handle_softexit
;
260 $SIG{'TERM'} = \
&handle_exit
;
261 while (@queue || @running) {
263 my $proceed_immediately = reap_finished_jobs
();
265 if ($progress && (time - $last_progress) >= 60) {
266 ferror
("STATUS: %d queued, %d running, %d finished, %d skipped, %d killed", scalar(@queue), scalar(@running), $jobs_executed, $jobs_skipped, scalar(@jobs_killed));
270 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
272 error
("STATUS: currently running: ". join(', ', @run_status));
274 $last_progress = time;
276 # Back off if we're too busy
277 if (@running >= $max_par || have_intensive_jobs
() >= $max_par_intensive || !@queue) {
278 sleep 1 unless $proceed_immediately;
282 run_job
(shift(@queue)) if @queue;
285 ferror
("--- Queue processed. %d jobs executed, %d skipped, %d killed.", $jobs_executed, $jobs_skipped, scalar(@jobs_killed));
289 sub run_perpetually
{
291 die "Lockfile exists. Please make sure no other instance of jobd is running.";
293 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
301 sleep($restart_delay) if $perpetual; # Let the system breathe for a moment
306 ######### Helpers {{{1
309 print STDERR ts
().shift()."\n";
322 my $parse_res = GetOptions
(
323 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
324 'quiet|q' => \
$quiet,
325 'progress|P' => \
$progress,
326 'kill-after|k=i' => \
$kill_after,
327 'max-parallel|p=i' => \
$max_par,
328 'max-intensive-parallel|i=i' => \
$max_par_intensive,
329 'restart-delay|d=i' => \
$restart_delay,
330 'lockfile|l=s' => \
$lockfile,
331 'all-once|a' => \
$all_once,
334 fatal
("Error: can only use one out of --all-once and --one")
335 if ($all_once && $one);
338 $ENV{'show_progress'} = '1';
356 ########## Documentation {{{1
362 jobd - Perform Girocco maintenance jobs
369 -h | --help detailed instructions
370 -q | --quiet run quietly
371 -P | --progress show occasional status updates
372 -k SECONDS | --kill-after SECONDS how long to wait before killing jobs
373 -p NUM | --max-parallel NUM how many jobs to run at the same time
374 -i NUM | --max-intensive-parallel NUM how many resource-hungry jobs to run
376 -d NUM | --restart-delay SECONDS wait for this many seconds between
378 -l FILE | --lockfile FILE create a lockfile in the given
380 -a | --all-once process the list only once
381 -o PRJNAME | --one PRJNAME process only one project
389 Print the full description of jobd's options.
393 Suppress non-error messages, e.g. for use when running this task as a cronjob.
397 Show information about the current status of the job queue occasionally. This
398 is automatically enabled if --quiet is not given.
400 =item B<--kill-after SECONDS>
402 Kill supervised jobs after a certain time to avoid hanging the daemon.
404 =item B<--max-parallel NUM>
406 Run no more than that many jobs at the same time.
408 =item B<--max-intensive-parallel NUM>
410 Run no more than that many resource-hungry jobs at the same time. Right now,
411 this refers to repacking jobs.
413 =item B<--restart-delay NUM>
415 After processing the queue, wait this many seconds until the queue is
418 =item B<--lockfile FILE>
420 For perpetual operation, create a lockfile in that place and clean it up after
425 Instead of perpetuously processing all projects over and over again, process
426 them just once and then exit.
428 =item B<--one PRJNAME>
430 Process only the given project (given as just the project name without C<.git>
431 suffix) and then exit.
437 jobd is Girocco's repositories maintenance servant; it periodically checks all
438 the repositories and updates mirrored repositories and repacks push-mode
439 repositories when needed.