3 # jobd - perform Girocco maintenance jobs
5 # Run with --help for details
12 use POSIX
":sys_wait_h";
23 my $max_par_intensive = 3; # no command line option right now
24 my $lockfile = "/tmp/jobd.lock";
32 my $p = $job->{'project'};
33 check_project_exists
($job) || return;
34 if (-e get_project_path
($p).".nofetch") {
36 return setup_gc
($job);
38 if (-e get_project_path
($p).".clone_in_progress") {
39 job_skip
($job, "initial mirroring not complete yet");
40 return setup_gc
($job);
42 if (my $ts = is_operation_uptodate
($p, 'lastrefresh', $Girocco::Config
::min_mirror_interval
)) {
43 job_skip
($job, "not needed right now, last run at $ts");
47 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
52 my $p = $job->{'project'};
53 check_project_exists
($job) || return;
54 if (my $ts = is_operation_uptodate
($p, 'lastgc', $Girocco::Config
::min_gc_interval
)) {
55 job_skip
($job, "not needed right now, last run at $ts");
58 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
64 project
=> $job->{'project'},
66 command
=> \
&gc_project
,
71 sub check_project_exists
{
73 my $p = $job->{'project'};
74 if (!-d get_project_path
($p)) {
75 job_skip
($job, "non-existent project");
81 sub get_project_path
{
82 "$Girocco::Config::reporoot/".shift().".git/";
85 sub is_operation_uptodate
{
86 my ($project, $which, $threshold) = @_;
87 my $path = get_project_path
($project);
88 my $timestamp = `GIT_DIR="$path" $Girocco::Config::git_bin config "gitweb.$which"`;
89 my $unix_ts = `date +%s -d "$timestamp"`;
90 (time - $unix_ts) <= $threshold ?
$timestamp : undef;
98 command
=> \
&update_project
,
99 on_success
=> \
&setup_gc
,
100 on_error
=> \
&setup_gc
,
105 queue_one
($_) for (Girocco
::Project
->get_full_list());
108 ######### Daemon operation {{{1
118 sub handle_softexit
{
119 error
("Waiting for outstanding jobs to finish... ".
120 "^C again to exit immediately");
123 $SIG{'INT'} = \
&handle_exit
;
127 error
("Killing outstanding jobs...");
128 $SIG{'TERM'} = 'IGNORE';
130 kill 'KILL', -($_->{'pid'});
132 unlink $lockfile if ($locked);
138 $opts{'queued_at'} = time;
139 $opts{'dont_run'} = 0;
140 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
148 $job->{'command'}->($job);
149 if ($job->{'dont_run'}) {
158 "[".$job->{'type'}."::".$job->{'project'}."]";
161 # Only one of those per job!
162 sub exec_job_command
{
163 my ($job, $command, $err_only) = @_;
166 if (!defined($pid = fork)) {
167 error
(_job_name
($job) ." Can't fork job: $!");
168 $job->{'finished'} = 1;
172 open STDIN
, '/dev/null' || do {
173 error
(_job_name
($job) ."Can't read from /dev/null: $!");
174 $job->{'finished'} = 1;
178 open STDOUT
, '>/dev/null' || do {
179 error
(_job_name
($job) ." Can't write to /dev/null: $!");
180 $job->{'finished'} = 1;
184 # New process group so we can keep track of all of its children
185 if (!defined(POSIX
::setpgid
(0, 0))) {
186 error
(_job_name
($job) ." Can't create process group: $!");
187 $job->{'finished'} = 1;
191 select(undef, undef, undef, 0.1);
193 # Stop perl from complaining
196 $job->{'pid'} = $pid;
197 $job->{'finished'} = 0;
198 $job->{'started_at'} = time;
202 my ($job, $msg) = @_;
203 $job->{'dont_run'} = 1;
204 error
(_job_name
($job) ." Skipping job: $msg") unless $quiet || !$msg;
207 sub reap_hanging_jobs
{
209 if (defined($_->{'started_at'}) && (time - $_->{'started_at'}) > $kill_after) {
210 $_->{'finished'} = 1;
211 kill 'KILL', -($_->{'pid'});
212 print STDERR _job_name
($_) ." KILLED due to timeout\n";
213 push @jobs_killed, _job_name
($_);
218 sub reap_finished_jobs
{
220 my $finished_any = 0;
222 $pid = waitpid(-1, WNOHANG
);
226 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
228 # XXX- we currently don't care
230 if (@child && !$child[0]->{'finished'}) {
231 $child[0]->{'on_success'}->($child[0]) if defined($child[0]->{'on_success'});
232 $child[0]->{'finished'} = 1;
235 $child[0]->{'on_error'}->($child[0]) if defined($child[0]->{'on_error'});
238 @running = grep { $_->{'finished'} == 0 } @running;
242 sub have_intensive_jobs
{
243 grep { $_->{'intensive'} == 1 } @running;
247 my $last_progress = time;
252 printf STDERR
"--- Processing %d queued jobs\n", scalar(@queue);
254 $SIG{'INT'} = \
&handle_softexit
;
255 $SIG{'TERM'} = \
&handle_exit
;
256 while (@queue || @running) {
258 my $proceed_immediately = reap_finished_jobs
();
259 # Back off if we're too busy
260 if (@running >= $max_par || have_intensive_jobs
() >= $max_par_intensive || !@queue) {
261 sleep 1 unless $proceed_immediately;
262 if ($progress && (time - $last_progress) >= 60) {
263 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);
267 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
269 error
("STATUS: currently running: ". join(', ', @run_status));
271 $last_progress = time;
276 run_job
(shift(@queue)) if @queue;
279 printf STDERR
"--- Queue processed. %d jobs executed, %d skipped, %d killed. Now restarting.\n", $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
283 sub run_perpetually
{
285 die "Lockfile exists. Please make sure no other instance of jobd is running.";
287 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
299 ######### Helpers {{{1
302 print STDERR
shift()."\n";
312 Getopt
::Long
::Configure
('bundling');
313 my $parse_res = GetOptions
(
314 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
315 'quiet|q' => \
$quiet,
316 'progress|P' => \
$progress,
317 'kill-after|k=i' => \
$kill_after,
318 'max-parallel|p=i' => \
$max_par,
319 'lockfile|l=s' => \
$lockfile,
320 'all-once|a' => \
$all_once,
323 fatal
("Error: can only use one out of --all-once and --one")
324 if ($all_once && $one);
327 $ENV{'show_progress'} = '1';
345 ########## Documentation {{{1
351 jobd - Perform Girocco maintenance jobs
358 -h | --help detailed instructions
359 -q | --quiet run quietly
360 -P | --progress show occasional status updates
361 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
362 -p NUM | --max-parallel=NUM how many jobs to run at the same time
363 -l FILE | --lockfile=FILE create a lockfile in the given location
364 -a | --all-once process the list only once
365 -o PRJNAME | --one=PRJNAME process only one project
373 Print the full description of jobd's options.
377 Suppress non-error messages, e.g. for use when running this task as a cronjob.
381 Show information about the current status of the job queue occasionally. This
382 is automatically enabled if --quiet is not given.
384 =item B<--kill-after=SECONDS>
386 Kill supervised jobs after a certain time to avoid hanging the daemon.
388 =item B<--max-parallel=NUM>
390 Run no more than that many jobs at the same time.
392 =item B<--lockfile=FILE>
394 For perpetual operation, create a lockfile in that place and clean it up after
399 Instead of perpetuously processing all projects over and over again, process
400 them just once and then exit.
402 =item B<--one=PRJNAME>
404 Process only the given project (given as just the project name without C<.git>
405 suffix) and then exit.
411 jobd is Girocco's repositories maintenance servant; it periodically checks all
412 the repositories and updates mirrored repositories and repacks push-mode
413 repositories when needed.