3 # jobd - perform Girocco maintenance jobs
5 # Run with --help for details
21 my $lockfile = "/tmp/jobd.lock";
29 my $p = $job->{'project'};
30 check_project_exists
($job) || return;
31 (-e
"$Girocco::Config::reporoot/$p.git/.nofetch") || do {
35 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
40 my $p = $job->{'project'};
41 check_project_exists
($job) || return;
42 exec_job_command
($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
48 project
=> $job->{'project'},
50 command
=> \
&gc_project
,
54 sub check_project_exists
{
56 my $p = $job->{'project'};
57 if (!-d
"$Girocco::Config::reporoot/$p.git") {
58 error
("Warning: skipping non-existent project: $job->{project}")
71 command
=> \
&update_project
,
72 on_success
=> \
&setup_gc
,
73 on_error
=> \
&setup_gc
,
78 queue_one
($_) for (Girocco
::Project
->get_full_list());
81 ######### Daemon operation {{{1
91 error
("Waiting for outstanding jobs to finish... ".
92 "^C again to exit immediately");
95 $SIG{'INT'} = \
&handle_exit
;
99 error
("Killing outstanding jobs...");
100 $SIG{'CHLD'} = 'IGNORE';
101 $SIG{'TERM'} = 'IGNORE';
103 kill 'KILL', $_->{'pid'};
105 unlink $lockfile if ($locked);
109 sub handle_childgone
{
112 my $child = grep { $_->{'pid'} == $pid } @running;
114 # XXX- we currently don't care
116 $child->{'finished'} = 2;
120 $SIG{'CHLD'} = \
&handle_childgone
;
125 $opts{'queued_at'} = time;
133 $job->{'command'}->($job);
138 "[".$job->{'type'}."::".$job->{'project'}."]";
141 # Only one of those per job!
142 sub exec_job_command
{
143 my ($job, $command, $err_only) = @_;
146 if (!defined($pid = fork)) {
147 error
(_job_name
($job) ." Can't fork job: $!");
148 $job->{'finished'} = 1;
153 open STDOUT
, '>/dev/null' || do {
154 error
(_job_name
($job) ." Can't write to /dev/null: $!");
155 $job->{'finished'} = 1;
162 $job->{'pid'} = $pid;
163 $job->{'finished'} = 0;
164 $job->{'started_at'} = time;
169 exec_job_command
($job, ['/bin/false']);
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
{
185 my $status = $_->{'finished'};
186 if ($status == 0) { next; }
187 elsif ($status == 1 && defined($_->{'on_error'})) {
188 $_->{'on_error'}->($_);
189 } elsif ($status == 2 && defined($_->{'on_success'})) {
190 $_->{'on_success'}->($_);
193 @running = grep { $_->{'finished'} == 0 } @running;
201 printf STDERR
"--- Processing %d queued jobs\n", @queue;
203 while (@queue || @running) {
205 reap_finished_jobs
();
206 # Back off if we're too busy
207 if (@running >= $max_par) {
210 unless ($quiet || ($queue_steps % 10)) {
211 printf STDERR
"STATUS: %d queued, %d running, %d finished, %d killed\n", @queue, @running, $jobs_executed, @jobs_killed;
216 run_job
(shift(@queue)) if @queue;
219 printf STDERR
"--- Queue processed. %d jobs executed, %d killed due to timeouts. Now restarting.\n", @queue, @jobs_killed;
223 sub run_perpetually
{
225 die "Lockfile exists. Please make sure no other instance of jobd is running.";
227 open LOCK
, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
239 ######### Helpers {{{1
242 print STDERR
shift()."\n";
252 Getopt
::Long
::Configure
('bundling', 'auto_help');
253 my $parse_res = GetOptions
(
254 'quiet|q' => \
$quiet,
255 'kill-after|k=i' => \
$kill_after,
256 'max-parallel|p=i' => \
$max_par,
257 'lockfile|l=s' => \
$lockfile,
258 'all-once|a' => \
$all_once,
261 fatal
("Error: can only use one out of --all-once and --one")
262 if ($all_once && $one);
265 $ENV{'show_progress'} = '1';
282 ########## Documentation {{{1
288 jobd - Perform Girocco maintenance jobs
295 -h | --help detailed instructions
296 -q | --quiet run quietly
297 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
298 -p NUM | --max-parallel=NUM how many jobs to run at the same time
299 -l FILE | --lockfile=FILE create a lockfile in the given location
300 -a | --all-once process the list only once
301 -o PRJNAME | --one=PRJNAME process only one project
309 Print the full description of jobd's options.
313 Suppress non-error messages, e.g. for use when running this task as a cronjob.
315 =item B<--kill-after=SECONDS>
317 Kill supervised jobs after a certain time to avoid hanging the daemon.
319 =item B<--max-parallel=NUM>
321 Run no more than that many jobs at the same time.
323 =item B<--lockfile=FILE>
325 For perpetual operation, create a lockfile in that place and clean it up after
330 Instead of perpetuously processing all projects over and over again, process
331 them just once and then exit.
333 =item B<--one=PRJNAME>
335 Process only the given project (given as just the project name without C<.git>
336 suffix) and then exit.
342 jobd is Girocco's repositories maintenance servant; it periodically checks all
343 the repositories and updates mirrored repositories and repacks push-mode
344 repositories when needed.