PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/MEME/website/scripts/Queue.pm

https://bitbucket.org/bdartigu/stagereport
Perl | 121 lines | 99 code | 10 blank | 12 comment | 9 complexity | 7a560ef62edfbec7016093aefc586391 MD5 | raw file
  1. # FILE: Queue.pm
  2. # PROJECT: svm web server
  3. # AUTHOR: Paul Pavlidis, based on code by Phan Lu, Darrin Lewis, Andrew Liu, Ilan Wapinski.
  4. # CREATED: 12/02
  5. # $Id: Queue.pm,v 1.1.1.1 2004/10/07 19:44:13 cegrant Exp $
  6. package Queue;
  7. require Exporter;
  8. @ISA = qw(Exporter);
  9. @EXPORT = qw(enqueue dequeue);
  10. use MetaGlobals;
  11. use Log;
  12. use Process;
  13. use strict;
  14. ############################################################################
  15. ##### QUEUEING THE JOB. Place the job in the queue. Only returns when
  16. ##### the queue is free or we give up.
  17. ############################################################################
  18. sub enqueue {
  19. my ($uid, $log) = @_;
  20. # create a queue file
  21. my $queue_file = "$QUEUE_DIR/${uid}.queue";
  22. open (Q, ">$queue_file") || die("Cannot open queue file: $!");
  23. print Q time, "\n";
  24. $log->log("Enqueued $uid");
  25. $log->debug("Enqueued $uid");
  26. close Q;
  27. my $t = 0;
  28. my %ages;
  29. my $old_pos = 10000000000;
  30. $log->log("Waiting...");
  31. writeSemaphore($uid, "WAITING|", 0);
  32. cleanqueue();
  33. while ( $t < $PATIENCE ) {
  34. $^T = time; # what happens to this?
  35. undef %ages;
  36. opendir( QDIR, "$QUEUE_DIR" );
  37. while ( defined ($_ = readdir(QDIR))) {
  38. if (/queue$/) {
  39. my $age = -M "$QUEUE_DIR/$_";
  40. $ages{$_} = $age;
  41. }
  42. }
  43. closedir(QDIR);
  44. my $qfile;
  45. my $pos = 0;
  46. foreach $qfile (keys %ages) {
  47. if ( $ages{$qfile} > $ages{"${uid}.queue"} ) {
  48. $pos++; # how many are ahead of me.
  49. }
  50. }
  51. # sanity check
  52. if ($MAXJOBS < $SIMULTANEOUSJOBS) {
  53. return("ABORT|CONFIGERROR");
  54. $log->log("Notify the administrator that the MAXJOBS settings is too low given the SIMULTANEOUSJOBS setting");
  55. }
  56. # process job: ABORT or RUN or WAIT
  57. if ( $pos > $MAXJOBS ) {
  58. $log->log("Sorry, too many jobs waiting now. Try later."); # Should we do this? What does it hurt to have a long queue?
  59. return( "ABORT|MAXJOBS" );
  60. } else {
  61. if ( $pos < $SIMULTANEOUSJOBS ) {
  62. $log->log("It's your turn.");
  63. return( "RUN" );
  64. } else {
  65. if ($pos < $old_pos) {
  66. $log->log("$pos jobs are ahead of yours."); # todo: show how many are actually running. Look for old jobs.
  67. }
  68. $old_pos = $pos;
  69. sleep $WAIT;
  70. $t += $WAIT;
  71. }
  72. }
  73. } # while waiting
  74. if ( $t > $PATIENCE ) {
  75. $log->log("Could not run the job. Ran out of patience.");
  76. return( "ABORT|PATIENCE" );
  77. }
  78. }
  79. ############################################################################
  80. # Clean up some 'zombies' (internal function)
  81. ############################################################################
  82. sub cleanqueue {
  83. # At least one job in the queue should be running. If not, kill the oldest one.
  84. # To figure out which one is running is a bit complicated....maybe too tricky.
  85. return 1;
  86. }
  87. ############################################################################
  88. ##### DEQUEUEING THE JOB
  89. ############################################################################
  90. sub dequeue {
  91. my($uid, $log) = @_;
  92. my $success = 0;
  93. if (-e "$QUEUE_DIR/${uid}.queue") {
  94. $success = unlink "$QUEUE_DIR/${uid}.queue";
  95. } else {
  96. # Can happen if it already was cleaned up.
  97. # $log->debug("Tried to dequeue $QUEUE_DIR/${uid}.queue but the file is not there");
  98. $success = 1;
  99. }
  100. if ($success < 1) {
  101. $log->log("Dequeue could not remove the queue file. Please contact the system administrator!!!");
  102. } else {
  103. $log->log("Dequeued $uid");
  104. }
  105. }
  106. 1;