build-exclusive.pl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/perl -w
  2. # Builds the website, acquiring a lock first so builds don't collide with
  3. # each other. Run this from the website root directory. Pass command line
  4. # options to the build script by first putting "--" on the command line,
  5. # i.e. "build-exclusive.pl -- -v foo.html" runs "bin/build -v foo.html".
  6. use strict;
  7. # The number of seconds to wait before trying again if another build is going.
  8. # Set this with the --wait command line option.
  9. my $WAIT = 15;
  10. # The maximum amount of time in seconds to wait for a lock before dying.
  11. # Set this with the --maxwait command line option.
  12. #
  13. # If this script is being run periodically by cron, set this low (ideally less
  14. # than the interval between cron runs; f.e. 30 seconds for a run that happens
  15. # every minute) to prevent processes from building up.
  16. #
  17. # If this script gets run by CVS upon commit, set this high (f.e. 450 seconds,
  18. # i.e. 15 minutes) so that multiple simultaneous commits all go through
  19. # one after the other even if some of them have to wait a while.
  20. #
  21. my $MAX_WAIT = 30;
  22. # Get any options passed on the command line, like --wait and --maxwait.
  23. use Getopt::Long;
  24. my $result = GetOptions ("wait=i" => \$WAIT,
  25. "maxwait=i" => \$MAX_WAIT);
  26. # Use Proc::PID::File to handle the locking.
  27. use Proc::PID::File;
  28. my $LOCK_OPTS = {
  29. dir => "/tmp",
  30. name => "smprojorg-build",
  31. debug => 0
  32. };
  33. # Try to get a lock.
  34. my $started = time();
  35. my $waited = 0;
  36. while (Proc::PID::File->running($LOCK_OPTS) && ((time()-$started)<=$MAX_WAIT)) {
  37. print STDERR "Couldn't get build lock; waiting another $WAIT seconds...\n";
  38. $waited = 1;
  39. sleep $WAIT ;
  40. }
  41. # Die if we couldn't get a lock.
  42. if (Proc::PID::File->running($LOCK_OPTS)) {
  43. die "Couldn't get build lock.";
  44. }
  45. elsif ($waited) {
  46. print STDERR "Got build lock after " . (time() - $started) . " seconds.\n";
  47. }
  48. # Build the website.
  49. #print STDERR "Building site.\n";
  50. system("bin/build", @ARGV);