jobs.php 5.9 KB

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