jobs.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_core
  6. * @ingroup tripal_api
  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. * @ingroup tripal_core
  32. * @ingroup tripal_api
  33. */
  34. function tripal_job_set_progress($job_id,$percentage){
  35. if(preg_match("/^(\d\d|100)$/",$percentage)){
  36. $record = new stdClass();
  37. $record->job_id = $job_id;
  38. $record->progress = $percentage;
  39. if(drupal_write_record('tripal_jobs',$record,'job_id')){
  40. return 1;
  41. }
  42. }
  43. return 0;
  44. }
  45. /**
  46. * Returns a list of jobs associated with the given module
  47. *
  48. * @ingroup tripal_core
  49. */
  50. function tripal_get_module_active_jobs ($modulename){
  51. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  52. "WHERE TJ.end_time IS NULL and TJ.modulename = '%s' ";
  53. return db_fetch_object(db_query($sql,$modulename));
  54. }
  55. /**
  56. *
  57. *
  58. * @ingroup tripal_core
  59. */
  60. function tripal_jobs_report () {
  61. //$jobs = db_query("SELECT * FROM {tripal_jobs} ORDER BY job_id DESC");
  62. $jobs = pager_query(
  63. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  64. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  65. TJ.end_time,TJ.priority,U.name as username
  66. FROM {tripal_jobs} TJ
  67. INNER JOIN users U on TJ.uid = U.uid
  68. ORDER BY job_id DESC", 10,0,"SELECT count(*) FROM {tripal_jobs}");
  69. // create a table with each row containig stats for
  70. // an individual job in the results set.
  71. $output .= "Waiting jobs are executed first by priority level (the lower the ".
  72. "number the higher the priority) and second by the order they ".
  73. "were entered";
  74. $output .= "<table class=\"tripal-table tripal-table-horz\">".
  75. " <tr>".
  76. " <th>Job ID</th>".
  77. " <th>User</th>".
  78. " <th>Job Name</th>".
  79. " <th nowrap>Dates</th>".
  80. " <th>Priority</th>".
  81. " <th>Progress</th>".
  82. " <th>Status</th>".
  83. " <th>Actions</th>".
  84. " </tr>";
  85. $i = 0;
  86. while($job = db_fetch_object($jobs)){
  87. $class = 'tripal-table-odd-row';
  88. if($i % 2 == 0 ){
  89. $class = 'tripal-table-even-row';
  90. }
  91. $submit = format_date($job->submit_date);
  92. if($job->start_time > 0){
  93. $start = format_date($job->start_time);
  94. } else {
  95. if(strcmp($job->job_status,'Cancelled')==0){
  96. $start = 'Cancelled';
  97. } else {
  98. $start = 'Not Yet Started';
  99. }
  100. }
  101. if($job->end_time > 0){
  102. $end = format_date($job->end_time);
  103. } else {
  104. $end = '';
  105. }
  106. $cancel_link = '';
  107. if($job->start_time == 0 and $job->end_time == 0){
  108. $cancel_link = "<a href=\"".url("admin/tripal/tripal_jobs/cancel/".$job->job_id)."\">Cancel</a>";
  109. }
  110. $rerun_link = "<a href=\"".url("admin/tripal/tripal_jobs/rerun/".$job->job_id)."\">Re-run</a>";
  111. $output .= " <tr class=\"$class\">";
  112. $output .= " <td>$job->job_id</td>".
  113. " <td>$job->username</td>".
  114. " <td>$job->job_name</td>".
  115. " <td nowrap>Submit Date: $submit".
  116. " <br>Start Time: $start".
  117. " <br>End Time: $end</td>".
  118. " <td>$job->priority</td>".
  119. " <td>$job->progress%</td>".
  120. " <td>$job->job_status</td>".
  121. " <td>$cancel_link $rerun_link</td>".
  122. " </tr>";
  123. $i++;
  124. }
  125. $output .= "</table>";
  126. $output .= theme_pager();
  127. return $output;
  128. }
  129. /**
  130. *
  131. *
  132. * @ingroup tripal_core
  133. */
  134. function tripal_jobs_launch ($do_parallel = 0){
  135. // first check if any jobs are currently running
  136. // if they are, don't continue, we don't want to have
  137. // more than one job script running at a time
  138. if(!$do_parallel and tripal_jobs_check_running()){
  139. return;
  140. }
  141. // get all jobs that have not started and order them such that
  142. // they are processed in a FIFO manner.
  143. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  144. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
  145. "ORDER BY priority ASC,job_id ASC";
  146. $job_res = db_query($sql);
  147. while($job = db_fetch_object($job_res)){
  148. // set the start time for this job
  149. $record = new stdClass();
  150. $record->job_id = $job->job_id;
  151. $record->start_time = time();
  152. $record->status = 'Running';
  153. $record->pid = getmypid();
  154. drupal_write_record('tripal_jobs',$record,'job_id');
  155. // call the function provided in the callback column.
  156. // Add the job_id as the last item in the list of arguments. All
  157. // callback functions should support this argument.
  158. $callback = $job->callback;
  159. $args = split("::",$job->arguments);
  160. $args[] = $job->job_id;
  161. print "Calling: $callback(" . implode(", ",$args) . ")\n";
  162. call_user_func_array($callback,$args);
  163. // set the end time for this job
  164. $record->end_time = time();
  165. $record->status = 'Completed';
  166. $record->progress = '100';
  167. drupal_write_record('tripal_jobs',$record,'job_id');
  168. // send an email to the user advising that the job has finished
  169. }
  170. }
  171. /**
  172. *
  173. *
  174. * @ingroup tripal_core
  175. * @ingroup tripal_api
  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. * @ingroup tripal_core
  207. * @ingroup tripal_api
  208. */
  209. function tripal_jobs_rerun ($job_id){
  210. global $user;
  211. $sql = "select * from {tripal_jobs} where job_id = %d";
  212. $job = db_fetch_object(db_query($sql,$job_id));
  213. $args = explode("::",$job->arguments);
  214. tripal_add_job ($job->job_name,$job->modulename,$job->callback,$args,$user->uid,
  215. $job->priority);
  216. drupal_goto("admin/tripal/tripal_jobs");
  217. }
  218. /**
  219. *
  220. * @ingroup tripal_core
  221. * @ingroup tripal_api
  222. */
  223. function tripal_jobs_cancel ($job_id){
  224. $sql = "select * from {tripal_jobs} where job_id = %d";
  225. $job = db_fetch_object(db_query($sql,$job_id));
  226. // set the end time for this job
  227. if($job->start_time == 0){
  228. $record = new stdClass();
  229. $record->job_id = $job->job_id;
  230. $record->end_time = time();
  231. $record->status = 'Cancelled';
  232. $record->progress = '0';
  233. drupal_write_record('tripal_jobs',$record,'job_id');
  234. drupal_set_message("Job #$job_id cancelled");
  235. } else {
  236. drupal_set_message("Job #$job_id cannot be cancelled. It is in progress or has finished.");
  237. }
  238. drupal_goto("admin/tripal/tripal_jobs");
  239. }