jobs.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. /************************************************************************
  6. *
  7. */
  8. function tripal_add_job ($job_name,$modulename,$callback,$arguments,$uid,
  9. $priority = 10){
  10. # convert the arguments into a string for storage in the database
  11. $args = implode("::",$arguments);
  12. $record = new stdClass();
  13. $record->job_name = $job_name;
  14. $record->modulename = $modulename;
  15. $record->callback = $callback;
  16. $record->status = 'Waiting';
  17. $record->submit_date = time();
  18. $record->uid = $uid;
  19. $record->priority = $priority; # the lower the number the higher the priority
  20. if($args){
  21. $record->arguments = $args;
  22. }
  23. drupal_write_record('tripal_jobs',$record);
  24. $jobs_url = url("admin/tripal/tripal_jobs");
  25. drupal_set_message(t("Job '$job_name' submitted. Check the <a href='$jobs_url'>jobs page</a> for status"));
  26. return 1;
  27. }
  28. /************************************************************************
  29. *
  30. */
  31. function tripal_job_set_progress($job_id,$percentage){
  32. if(preg_match("/^(\d\d|100)$/",$percentage)){
  33. $record = new stdClass();
  34. $record->job_id = $job_id;
  35. $record->progress = $percentage;
  36. if(drupal_write_record('tripal_jobs',$record,'job_id')){
  37. return 1;
  38. }
  39. }
  40. return 0;
  41. }
  42. /************************************************************************
  43. * Returns a list of jobs associated with the given module
  44. */
  45. function tripal_get_module_active_jobs ($modulename){
  46. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  47. "WHERE TJ.end_time IS NULL and TJ.modulename = '%s' ";
  48. return db_fetch_object(db_query($sql,$modulename));
  49. }
  50. /************************************************************************
  51. *
  52. */
  53. function tripal_jobs_report () {
  54. //$jobs = db_query("SELECT * FROM {tripal_jobs} ORDER BY job_id DESC");
  55. $jobs = pager_query("SELECT * FROM {tripal_jobs} TJ INNER JOIN users U on TJ.uid = U.uid ORDER BY job_id DESC",
  56. 10,0,"SELECT count(*) FROM {tripal_jobs}");
  57. // create a table with each row containig stats for
  58. // an individual job in the results set.
  59. $output .= "Waiting jobs are executed first by priority level (the lower the ".
  60. "number the higher the priority) and second by the order they ".
  61. "were entered";
  62. $output .= "<table class=\"border-table\">".
  63. " <tr>".
  64. " <th>Job ID</th>".
  65. " <th>User</th>".
  66. " <th>Job Name</th>".
  67. " <th nowrap>Dates</th>".
  68. " <th>Priority</th>".
  69. " <th>Progress</th>".
  70. " <th>Status</th>".
  71. " </tr>";
  72. while($job = db_fetch_object($jobs)){
  73. $submit = format_date($job->submit_date);
  74. if($job->start_time > 0){
  75. $start = format_date($job->start_time);
  76. } else {
  77. $start = 'Not Yet Started';
  78. }
  79. if($job->end_time > 0){
  80. $end = format_date($job->end_time);
  81. } else {
  82. $end = '';
  83. }
  84. $output .= " <tr>";
  85. $output .= " <td>$job->job_id</td>".
  86. " <td>$job->name</td>".
  87. " <td>$job->job_name</td>".
  88. " <td nowrap>Submit Date: $submit".
  89. " <br>Start Time: $start".
  90. " <br>End Time: $end</td>".
  91. " <td>$job->priority</td>".
  92. " <td>$job->progress%</td>".
  93. " <td>$job->status</td>".
  94. " </tr>";
  95. }
  96. $output .= "</table>";
  97. $output .= theme_pager();
  98. return $output;
  99. }
  100. /************************************************************************
  101. *
  102. */
  103. function tripal_jobs_launch (){
  104. // first check if any jobs are currently running
  105. // if they are, don't continue, we don't want to have
  106. // more than one job script running at a time
  107. if(tripal_jobs_check_running()){
  108. return;
  109. }
  110. // get all jobs that have not started and order them such that
  111. // they are processed in a FIFO manner.
  112. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  113. "WHERE TJ.start_time IS NULL ".
  114. "ORDER BY priority ASC,job_id ASC";
  115. $job_res = db_query($sql);
  116. while($job = db_fetch_object($job_res)){
  117. // set the start time for this job
  118. $record = new stdClass();
  119. $record->job_id = $job->job_id;
  120. $record->start_time = time();
  121. $record->status = 'Running';
  122. $record->pid = getmypid();
  123. drupal_write_record('tripal_jobs',$record,'job_id');
  124. // call the function provided in the callback column.
  125. // Add the job_id as the last item in the list of arguments. All
  126. // callback functions should support this argument.
  127. $callback = $job->callback;
  128. $args = split("::",$job->arguments);
  129. $args[] = $job->job_id;
  130. print "Calling: $callback(" . implode(", ",$args) . ")\n";
  131. call_user_func_array($callback,$args);
  132. // set the end time for this job
  133. $record->end_time = time();
  134. $record->status = 'Completed';
  135. $record->progress = '100';
  136. drupal_write_record('tripal_jobs',$record,'job_id');
  137. // send an email to the user advising that the job has finished
  138. }
  139. }
  140. /************************************************************************
  141. *
  142. */
  143. function tripal_jobs_check_running () {
  144. // iterate through each job that has not ended
  145. // and see if it is still running. If it is not
  146. // running but does not have an end_time then
  147. // set the end time and set the status to 'Error'
  148. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  149. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  150. $jobs = db_query($sql);
  151. while($job = db_fetch_object($jobs)){
  152. if($job->pid and posix_kill($job->pid, 0)) {
  153. // the job is still running so let it go
  154. // we return 1 to indicate that a job is running
  155. print "Job is still running (pid $job->pid)\n";
  156. return 1;
  157. } else {
  158. // the job is not running so terminate it
  159. $record = new stdClass();
  160. $record->job_id = $job->job_id;
  161. $record->end_time = time();
  162. $record->status = 'Error';
  163. $record->error_msg = 'Job has terminated unexpectedly.';
  164. drupal_write_record('tripal_jobs',$record,'job_id');
  165. }
  166. }
  167. // return 1 to indicate that no jobs are currently running.
  168. return 0;
  169. }
  170. ?>