jobd: fix a few typos that prevent it from running
[girocco.git] / jobd / jobd.pl
blob6e0fe906f38896ec9af55c98644325e2e894cfb0
1 #!/usr/bin/perl
3 # jobd - perform Girocco maintenance jobs
5 # Run with --help for details
7 use strict;
8 use warnings;
10 use Getopt::Long;
11 use Pod::Usage;
12 use POSIX ":sys_wait_h";
14 use Girocco::Config;
15 use Girocco::Project;
16 use Girocco::User;
18 # Options
19 my $quiet;
20 my $progress;
21 my $kill_after = 900;
22 my $max_par = 20;
23 my $lockfile = "/tmp/jobd.lock";
24 my $all_once;
25 my $one;
27 ######### Jobs {{{1
29 sub update_project {
30 my $job = shift;
31 my $p = $job->{'project'};
32 check_project_exists($job) || return;
33 (-e "$Girocco::Config::reporoot/$p.git/.nofetch") && do {
34 job_skip($job);
35 setup_gc($job);
36 return;
38 exec_job_command($job, ["$Girocco::Config::basedir/jobd/update.sh", $p], $quiet);
41 sub gc_project {
42 my $job = shift;
43 my $p = $job->{'project'};
44 check_project_exists($job) || return;
45 exec_job_command($job, ["$Girocco::Config::basedir/jobd/gc.sh", $p], $quiet);
48 sub setup_gc {
49 my $job = shift;
50 queue_job(
51 project => $job->{'project'},
52 type => 'gc',
53 command => \&gc_project,
54 intensive => 1,
58 sub check_project_exists {
59 my $job = shift;
60 my $p = $job->{'project'};
61 if (!-d "$Girocco::Config::reporoot/$p.git") {
62 error("Warning: skipping non-existent project: $job->{project}")
63 unless $quiet;
64 job_skip();
65 return 0;
70 sub queue_one {
71 my $project = shift;
72 queue_job(
73 project => $project,
74 type => 'update',
75 command => \&update_project,
76 on_success => \&setup_gc,
77 on_error => \&setup_gc,
81 sub queue_all {
82 queue_one($_) for (Girocco::Project->get_full_list());
85 ######### Daemon operation {{{1
87 my @queue;
88 my @running;
89 my $perpetual = 1;
90 my $locked = 0;
91 my $jobs_executed;
92 my $jobs_skipped;
93 my @jobs_killed;
95 sub handle_softexit {
96 error("Waiting for outstanding jobs to finish... ".
97 "^C again to exit immediately");
98 @queue = ();
99 $perpetual = 0;
100 $SIG{'INT'} = \&handle_exit;
103 sub handle_exit {
104 error("Killing outstanding jobs...");
105 $SIG{'TERM'} = 'IGNORE';
106 for (@running) {
107 kill 'KILL', $_->{'pid'};
109 unlink $lockfile if ($locked);
110 exit(0);
113 sub queue_job {
114 my %opts = @_;
115 $opts{'queued_at'} = time;
116 $opts{'dont_run'} = 0;
117 $opts{'intensive'} = 0 unless exists $opts{'intensive'};
118 push @queue, \%opts;
121 sub run_job {
122 my $job = shift;
124 push @running, $job;
125 $job->{'command'}->($job);
126 if ($job->{'dont_run'}) {
127 pop @running;
128 $jobs_skipped++;
129 return;
133 sub _job_name {
134 my $job = shift;
135 "[".$job->{'type'}."::".$job->{'project'}."]";
138 # Only one of those per job!
139 sub exec_job_command {
140 my ($job, $command, $err_only) = @_;
142 my $pid;
143 if (!defined($pid = fork)) {
144 error(_job_name($job) ." Can't fork job: $!");
145 $job->{'finished'} = 1;
146 return;
148 if (!$pid) {
149 if ($err_only) {
150 open STDOUT, '>/dev/null' || do {
151 error(_job_name($job) ." Can't write to /dev/null: $!");
152 $job->{'finished'} = 1;
153 return;
156 # "Prevent" races
157 select(undef, undef, undef, 0.25);
158 exec @$command;
159 # Stop perl from complaining
160 exit $?;
162 $job->{'pid'} = $pid;
163 $job->{'finished'} = 0;
164 $job->{'started_at'} = time;
167 sub job_skip {
168 my $job = shift;
169 $job->{'dont_run'} = 1;
172 sub reap_hanging_jobs {
173 for (@running) {
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 {
184 my $pid;
185 while (1) {
186 $pid = waitpid(-1, WNOHANG);
187 last if $pid == -1;
189 my @child = grep { $_->{'pid'} && $_->{'pid'} == $pid } @running;
190 if ($?) {
191 # XXX- we currently don't care
193 $child[0]->{'finished'} = 2 if (@child && !$child[0]->{'finished'});
194 my $status = $child[0]->{'finished'};
195 if ($status == 0) { next; }
196 elsif ($status == 1 && defined($child[0]->{'on_error'})) {
197 $child[0]->{'on_error'}->($_);
198 } elsif ($status == 2 && defined($child[0]->{'on_success'})) {
199 $child[0]->{'on_success'}->($_);
201 $jobs_executed++;
203 @running = grep { $_->{'finished'} == 0 } @running;
206 sub have_intensive_jobs {
207 grep { $_->{'intensive'} == 1 } @running;
210 sub run_queue {
211 my $last_progress = time;
212 $jobs_executed = 0;
213 $jobs_skipped = 0;
214 @jobs_killed = ();
215 if ($progress) {
216 printf STDERR "--- Processing %d queued jobs\n", scalar(@queue);
218 $SIG{'INT'} = \&handle_softexit;
219 $SIG{'TERM'} = \&handle_exit;
220 while (@queue || @running) {
221 reap_hanging_jobs();
222 reap_finished_jobs();
223 # Back off if we're too busy
224 if (@running >= $max_par || have_intensive_jobs() || !@queue) {
225 sleep 1;
226 if ($progress && (time - $last_progress) >= 60) {
227 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);
228 if (@running) {
229 my @run_status;
230 for (@running) {
231 push @run_status, _job_name($_)." ". (time - $_->{'started_at'}) ."s";
233 error("STATUS: currently running: ". join(', ', @run_status));
235 $last_progress = time;
237 next;
239 # Run next
240 run_job(shift(@queue)) if @queue;
242 if ($progress) {
243 printf STDERR "--- Queue processed. %d jobs executed, %d skipped, %d killed. Now restarting.\n", $jobs_executed, $jobs_skipped, scalar(@jobs_killed);
247 sub run_perpetually {
248 if (-e $lockfile) {
249 die "Lockfile exists. Please make sure no other instance of jobd is running.";
251 open LOCK, '>', $lockfile || die "Cannot create lockfile $lockfile: $!";
252 print LOCK $$;
253 close LOCK;
254 $locked = 1;
256 while ($perpetual) {
257 queue_all();
258 run_queue();
260 unlink $lockfile;
263 ######### Helpers {{{1
265 sub error($) {
266 print STDERR shift()."\n";
268 sub fatal($) {
269 error(shift);
270 exit 1;
273 ######### Main {{{1
275 # Parse options
276 Getopt::Long::Configure('bundling');
277 my $parse_res = GetOptions(
278 'help|?' => sub { pod2usage(-verbose => 1, -exitval => 0); },
279 'quiet|q' => \$quiet,
280 'progress|P' => \$progress,
281 'kill-after|k=i' => \$kill_after,
282 'max-parallel|p=i' => \$max_par,
283 'lockfile|l=s' => \$lockfile,
284 'all-once|a' => \$all_once,
285 'one|o=s' => \$one,
286 ) || pod2usage(2);
287 fatal("Error: can only use one out of --all-once and --one")
288 if ($all_once && $one);
290 unless ($quiet) {
291 $ENV{'show_progress'} = '1';
292 $progress = 1;
295 if ($one) {
296 queue_one($one);
297 run_queue();
298 exit;
301 if ($all_once) {
302 queue_all();
303 run_queue();
304 exit;
307 run_perpetually();
309 ########## Documentation {{{1
311 __END__
313 =head1 NAME
315 jobd - Perform Girocco maintenance jobs
317 =head1 SYNOPSIS
319 jobd [options]
321 Options:
322 -h | --help detailed instructions
323 -q | --quiet run quietly
324 -P | --progress show occasional status updates
325 -k SECONDS | --kill-after=SECONDS how long to wait before killing jobs
326 -p NUM | --max-parallel=NUM how many jobs to run at the same time
327 -l FILE | --lockfile=FILE create a lockfile in the given location
328 -a | --all-once process the list only once
329 -o PRJNAME | --one=PRJNAME process only one project
331 =head1 OPTIONS
333 =over 8
335 =item B<--help>
337 Print the full description of jobd's options.
339 =item B<--quiet>
341 Suppress non-error messages, e.g. for use when running this task as a cronjob.
343 =item B<--progress>
345 Show information about the current status of the job queue occasionally. This
346 is automatically enabled if --quiet is not given.
348 =item B<--kill-after=SECONDS>
350 Kill supervised jobs after a certain time to avoid hanging the daemon.
352 =item B<--max-parallel=NUM>
354 Run no more than that many jobs at the same time.
356 =item B<--lockfile=FILE>
358 For perpetual operation, create a lockfile in that place and clean it up after
359 finishing/aborting.
361 =item B<--all-once>
363 Instead of perpetuously processing all projects over and over again, process
364 them just once and then exit.
366 =item B<--one=PRJNAME>
368 Process only the given project (given as just the project name without C<.git>
369 suffix) and then exit.
371 =back
373 =head1 DESCRIPTION
375 jobd is Girocco's repositories maintenance servant; it periodically checks all
376 the repositories and updates mirrored repositories and repacks push-mode
377 repositories when needed.
379 =cut