jobs.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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(
  64. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  65. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  66. TJ.end_time,TJ.priority
  67. FROM {tripal_jobs} TJ
  68. INNER JOIN users U on TJ.uid = U.uid
  69. ORDER BY job_id DESC", 10,0,"SELECT count(*) FROM {tripal_jobs}");
  70. // create a table with each row containig stats for
  71. // an individual job in the results set.
  72. $output .= "Waiting jobs are executed first by priority level (the lower the ".
  73. "number the higher the priority) and second by the order they ".
  74. "were entered";
  75. $output .= "<table class=\"tripal-table tripal-table-horz\">".
  76. " <tr>".
  77. " <th>Job ID</th>".
  78. " <th>User</th>".
  79. " <th>Job Name</th>".
  80. " <th nowrap>Dates</th>".
  81. " <th>Priority</th>".
  82. " <th>Progress</th>".
  83. " <th>Status</th>".
  84. " <th>Actions</th>".
  85. " </tr>";
  86. $i = 0;
  87. while($job = db_fetch_object($jobs)){
  88. $class = 'tripal_feature-table-odd-row tripal-table-odd-row';
  89. if($i % 2 == 0 ){
  90. $class = 'tripal_feature-table-odd-row tripal-table-even-row';
  91. }
  92. $submit = format_date($job->submit_date);
  93. if($job->start_time > 0){
  94. $start = format_date($job->start_time);
  95. } else {
  96. if(strcmp($job->job_status,'Cancelled')==0){
  97. $start = 'Cancelled';
  98. } else {
  99. $start = 'Not Yet Started';
  100. }
  101. }
  102. if($job->end_time > 0){
  103. $end = format_date($job->end_time);
  104. } else {
  105. $end = '';
  106. }
  107. $cancel_link = '';
  108. if($job->start_time == 0 and $job->end_time == 0){
  109. $cancel_link = "<a href=\"".url("admin/tripal/tripal_jobs/cancel/".$job->job_id)."\">Cancel</a>";
  110. }
  111. $rerun_link = "<a href=\"".url("admin/tripal/tripal_jobs/rerun/".$job->job_id)."\">Re-run</a>";
  112. $output .= " <tr $class>";
  113. $output .= " <td>$job->job_id</td>".
  114. " <td>$job->name</td>".
  115. " <td>$job->job_name</td>".
  116. " <td nowrap>Submit Date: $submit".
  117. " <br>Start Time: $start".
  118. " <br>End Time: $end</td>".
  119. " <td>$job->priority</td>".
  120. " <td>$job->progress%</td>".
  121. " <td>$job->job_status</td>".
  122. " <td>$cancel_link $rerun_link</td>".
  123. " </tr>";
  124. $i++;
  125. }
  126. $output .= "</table>";
  127. $output .= theme_pager();
  128. return $output;
  129. }
  130. /**
  131. *
  132. *
  133. * @ingroup tripal_core
  134. */
  135. function tripal_jobs_launch (){
  136. // first check if any jobs are currently running
  137. // if they are, don't continue, we don't want to have
  138. // more than one job script running at a time
  139. if(tripal_jobs_check_running()){
  140. return;
  141. }
  142. // get all jobs that have not started and order them such that
  143. // they are processed in a FIFO manner.
  144. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  145. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
  146. "ORDER BY priority ASC,job_id ASC";
  147. $job_res = db_query($sql);
  148. while($job = db_fetch_object($job_res)){
  149. // set the start time for this job
  150. $record = new stdClass();
  151. $record->job_id = $job->job_id;
  152. $record->start_time = time();
  153. $record->status = 'Running';
  154. $record->pid = getmypid();
  155. drupal_write_record('tripal_jobs',$record,'job_id');
  156. // call the function provided in the callback column.
  157. // Add the job_id as the last item in the list of arguments. All
  158. // callback functions should support this argument.
  159. $callback = $job->callback;
  160. $args = split("::",$job->arguments);
  161. $args[] = $job->job_id;
  162. print "Calling: $callback(" . implode(", ",$args) . ")\n";
  163. call_user_func_array($callback,$args);
  164. // set the end time for this job
  165. $record->end_time = time();
  166. $record->status = 'Completed';
  167. $record->progress = '100';
  168. drupal_write_record('tripal_jobs',$record,'job_id');
  169. // send an email to the user advising that the job has finished
  170. }
  171. }
  172. /**
  173. *
  174. *
  175. * @ingroup tripal_core
  176. */
  177. function tripal_jobs_check_running () {
  178. // iterate through each job that has not ended
  179. // and see if it is still running. If it is not
  180. // running but does not have an end_time then
  181. // set the end time and set the status to 'Error'
  182. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  183. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  184. $jobs = db_query($sql);
  185. while($job = db_fetch_object($jobs)){
  186. if($job->pid and posix_kill($job->pid, 0)) {
  187. // the job is still running so let it go
  188. // we return 1 to indicate that a job is running
  189. print "Job is still running (pid $job->pid)\n";
  190. return 1;
  191. } else {
  192. // the job is not running so terminate it
  193. $record = new stdClass();
  194. $record->job_id = $job->job_id;
  195. $record->end_time = time();
  196. $record->status = 'Error';
  197. $record->error_msg = 'Job has terminated unexpectedly.';
  198. drupal_write_record('tripal_jobs',$record,'job_id');
  199. }
  200. }
  201. // return 1 to indicate that no jobs are currently running.
  202. return 0;
  203. }
  204. /**
  205. *
  206. */
  207. function tripal_jobs_rerun ($job_id){
  208. global $user;
  209. $sql = "select * from {tripal_jobs} where job_id = %d";
  210. $job = db_fetch_object(db_query($sql,$job_id));
  211. $args = explode("::",$job->arguments);
  212. tripal_add_job ($job->job_name,$job->modulename,$job->callback,$args,$user->uid,
  213. $job->priority);
  214. drupal_goto("admin/tripal/tripal_jobs");
  215. }
  216. /**
  217. *
  218. */
  219. function tripal_jobs_cancel ($job_id){
  220. $sql = "select * from {tripal_jobs} where job_id = %d";
  221. $job = db_fetch_object(db_query($sql,$job_id));
  222. // set the end time for this job
  223. if($job->start_time == 0){
  224. $record = new stdClass();
  225. $record->job_id = $job->job_id;
  226. $record->end_time = time();
  227. $record->status = 'Cancelled';
  228. $record->progress = '0';
  229. drupal_write_record('tripal_jobs',$record,'job_id');
  230. drupal_set_message("Job #$job_id cancelled");
  231. } else {
  232. drupal_set_message("Job #$job_id cannot be cancelled. It is in progress or has finished.");
  233. }
  234. drupal_goto("admin/tripal/tripal_jobs");
  235. }