Reduce disk writes in post-compile triggers
[gitolite.git] / src / commands / 1plus1
blob897d235f59f66959be2cc96cb30bd0ac94e531ce
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
5 # import LOCK_*
6 use Fcntl qw(:flock);
8 my $lockbase = shift; # suggested: $GL_REPO_BASE/$GL_REPO.git/.gl-mirror-push-lock.$SLAVE_NAME
9 my @cmd_plus_args = @ARGV; # the actual 'gitolite mirror ...' command
10 @ARGV = ();
12 # ----------------------------------------------------------------------
14 open( my $fhrun, ">", "$lockbase.run" ) or die "open '$lockbase.run' failed: $!";
15 if ( flock( $fhrun, LOCK_EX | LOCK_NB ) ) {
16 # got run lock; you're good to go
18 system(@cmd_plus_args);
20 flock( $fhrun, LOCK_UN );
21 exit 0;
24 # "run" lock failed; someone is already running the command
26 open( my $fhqueue, ">", "$lockbase.queue" ) or die "open '$lockbase.queue' failed: $!";
27 if ( flock( $fhqueue, LOCK_EX | LOCK_NB ) ) {
28 # got queue lock, now block waiting for "run" lock
29 flock( $fhrun, LOCK_EX );
30 # got run lock, so take yourself out of "queue" state, then run
31 flock( $fhqueue, LOCK_UN );
33 system(@cmd_plus_args);
35 flock( $fhrun, LOCK_UN );
36 exit 0;
39 # "queue" lock also failed; someone is running AND someone is queued; we can go home
40 say STDERR "INFO: nothing to do/queue; '$lockbase' already running and 1 in queue";
41 exit 0;