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 (my $ts = is_operation_uptodate
($p, 'lastrefresh', $Girocco::Config
::min_mirror_interval
)) {
39 job_skip
($job, "not needed right now, last run at $ts");
43 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
48 my $p = $job->{'project'};
49 check_project_exists
($job) || return;
50 if (my $ts = is_operation_uptodate
($p, 'lastgc', $Girocco::Config
::min_gc_interval
)) {
51 job_skip
($job, "not needed right now, last run at $ts");
54 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
60 project
=> $job->{'project'},
62 command
=> \
&gc_project
,
67 sub check_project_exists
{
69 my $p = $job->{'project'};
70 if (!-d get_project_path
($p)) {
71 job_skip
($job, "non-existent project");
77 sub get_project_path
{
78 "$Girocco::Config::reporoot/".shift().".git/";
81 sub is_operation_uptodate
{
82 my ($project, $which, $threshold) = @_;
83 my $path = get_project_path
($project);
84 my $timestamp = `GIT_DIR="$path" $Girocco::Config::git_bin config "gitweb.$which"`;
85 my $unix_ts = `date +%s -d "$timestamp"`;
86 (time - $unix_ts) <= $threshold ?
$timestamp : undef;
94 command
=> \
&update_project
,
95 on_success
=> \
&setup_gc
,
96 on_error
=> \
&setup_gc
,
101 queue_one
($_) for (Girocco
::Project
->get_full_list());
104 ######### Daemon operation {{{1
114 sub handle_softexit
{
115 error
("Waiting for outstanding jobs to finish... ".
116 "^C again to exit immediately");
119 $SIG{'INT'} = \
&handle_exit
;
123 error
("Killing outstanding jobs...");
124 $SIG{'TERM'} = 'IGNORE';
126 kill 'KILL', -($_->{'pid'});
128 unlink $lockfile if ($locked);
134 $opts{'queued_at'} = time;
135 $opts{'dont_run'} = 0;
136 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
144 $job->{'command'}->($job);
145 if ($job->{'dont_run'}) {
154 "[".$job->{'type'}."::".$job->{'project'}."]";
157 # Only one of those per job!
158 sub exec_job_command
{
159 my ($job, $command, $err_only) = @_;
162 if (!defined($pid = fork)) {
163 error
(_job_name
($job) ." Can't fork job: $!");
164 $job->{'finished'} = 1;
168 open STDIN
, '/dev/null' || do {
169 error
(_job_name
($job) ."Can't read from /dev/null: $!");
170 $job->{'finished'} = 1;
174 open STDOUT
, '>/dev/null' || do {
175 error
(_job_name
($job) ." Can't write to /dev/null: $!");
176 $job->{'finished'} = 1;
180 # New process group so we can keep track of all of its children
181 if (!defined(POSIX
::setpgid
(0, 0))) {
182 error
(_job_name
($job) ." Can't create process group: $!");
183 $job->{'finished'} = 1;
187 select(undef, undef, undef, 0.1);
189 # Stop perl from complaining
192 $job->{'pid'} = $pid;
193 $job->{'finished'} = 0;
194 $job->{'started_at'} = time;
198 my ($job, $msg) = @_;
199 $job->{'dont_run'} = 1;
200 error
(_job_name
($job) ." Skipping job: $msg") unless $quiet || !$msg;
203 sub reap_hanging_jobs
{
205 if (defined($_->{'started_at'}) && (time - $_->{'started_at'}) > $kill_after) {
206 $_->{'finished'} = 1;
207 kill 'KILL', -($_->{'pid'});
208 print STDERR _job_name
($_) ." KILLED due to timeout\n";
209 push @jobs_killed, _job_name
($_);
214 sub reap_finished_jobs
{
216 my $finished_any = 0;
218 $pid = waitpid(-1, WNOHANG
);
222 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
224 # XXX- we currently don't care
226 if (@child && !$child[0]->{'finished'}) {
227 $child[0]->{'on_success'}->($child[0]) if defined($child[0]->{'on_success'});
228 $child[0]->{'finished'} = 1;
231 $child[0]->{'on_error'}->($child[0]) if defined($child[0]->{'on_error'});
234 @running = grep { $_->{'finished'} == 0 } @running;
238 sub have_intensive_jobs
{
239 grep { $_->{'intensive'} == 1 } @running;
243 my $last_progress = time;
248 printf STDERR
"--- Processing %d queued jobs\n", scalar(@queue);
250 $SIG{'INT'} = \
&handle_softexit
;
251 $SIG{'TERM'} = \
&handle_exit
;
252 while (@queue || @running) {
254 my $proceed_immediately = reap_finished_jobs
();
255 # Back off if we're too busy
256 if (@running >= $max_par || have_intensive_jobs
() >= $max_par_intensive || !@queue) {
257 sleep 1 unless $proceed_immediately;
258 if ($progress && (time - $last_progress) >= 60) {
259 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);
263 push @run_status, _job_name
($_)." ". (time - $_->{'started_at'}) ."s";
265 error
("STATUS: currently running: ". join(', ', @run_status));
267 $last_progress = time;
272 run_job
(shift(@queue)) if @queue;
275 printf STDERR
"--- Queue processed. %d jobs executed, %d skipped, %d killed. Now restarting.\n", $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
279 sub run_perpetually
{
281 die "Lockfile exists. Please make sure no other instance of jobd is running.";
283 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
295 ######### Helpers {{{1
298 print STDERR
shift()."\n";
308 Getopt
::Long
::Configure
('bundling');
309 my $parse_res = GetOptions
(
310 'help|?' => sub { pod2usage
(-verbose
=> 1, -exitval
=> 0); },
311 'quiet|q' => \
$quiet,
312 'progress|P' => \
$progress,
313 'kill-after|k=i' => \
$kill_after,
314 'max-parallel|p=i' => \
$max_par,
315 'lockfile|l=s' => \
$lockfile,
316 'all-once|a' => \
$all_once,
319 fatal
("Error: can only use one out of --all-once and --one")
320 if ($all_once && $one);
323 $ENV{'show_progress'} = '1';
341 ########## Documentation {{{1
347 jobd - Perform Girocco maintenance jobs
354 -h | --help detailed instructions
355 -q | --quiet run quietly
356 -P | --progress show occasional status updates
357 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
358 -p NUM | --max-parallel=NUM how many jobs to run at the same time
359 -l FILE | --lockfile=FILE create a lockfile in the given location
360 -a | --all-once process the list only once
361 -o PRJNAME | --one=PRJNAME process only one project
369 Print the full description of jobd's options.
373 Suppress non-error messages, e.g. for use when running this task as a cronjob.
377 Show information about the current status of the job queue occasionally. This
378 is automatically enabled if --quiet is not given.
380 =item B<--kill-after=SECONDS>
382 Kill supervised jobs after a certain time to avoid hanging the daemon.
384 =item B<--max-parallel=NUM>
386 Run no more than that many jobs at the same time.
388 =item B<--lockfile=FILE>
390 For perpetual operation, create a lockfile in that place and clean it up after
395 Instead of perpetuously processing all projects over and over again, process
396 them just once and then exit.
398 =item B<--one=PRJNAME>
400 Process only the given project (given as just the project name without C<.git>
401 suffix) and then exit.
407 jobd is Girocco's repositories maintenance servant; it periodically checks all
408 the repositories and updates mirrored repositories and repacks push-mode
409 repositories when needed.