jobs.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 = tripal_jobs_get_submit_date($job);
  92. $start = tripal_jobs_get_start_time($job);
  93. $end = tripal_jobs_get_end_time($job);
  94. $cancel_link = '';
  95. if($job->start_time == 0 and $job->end_time == 0){
  96. $cancel_link = "<a href=\"".url("admin/tripal/tripal_jobs/cancel/".$job->job_id)."\">Cancel</a>";
  97. }
  98. $rerun_link = "<a href=\"".url("admin/tripal/tripal_jobs/rerun/".$job->job_id)."\">Re-run</a>";
  99. $view_link ="<a href=\"".url("admin/tripal/tripal_jobs/view/".$job->job_id)."\">View</a>";
  100. $output .= " <tr class=\"$class\">";
  101. $output .= " <td>$job->job_id</td>".
  102. " <td>$job->username</td>".
  103. " <td>$job->job_name</td>".
  104. " <td nowrap>Submit Date: $submit".
  105. " <br>Start Time: $start".
  106. " <br>End Time: $end</td>".
  107. " <td>$job->priority</td>".
  108. " <td>$job->progress%</td>".
  109. " <td>$job->job_status</td>".
  110. " <td>$cancel_link $rerun_link $view_link</td>".
  111. " </tr>";
  112. $i++;
  113. }
  114. $output .= "</table>";
  115. $output .= theme_pager();
  116. return $output;
  117. }
  118. /**
  119. *
  120. */
  121. function tripal_jobs_get_start_time($job){
  122. if($job->start_time > 0){
  123. $start = format_date($job->start_time);
  124. } else {
  125. if(strcmp($job->job_status,'Cancelled')==0){
  126. $start = 'Cancelled';
  127. } else {
  128. $start = 'Not Yet Started';
  129. }
  130. }
  131. return $start;
  132. }
  133. /**
  134. *
  135. */
  136. function tripal_jobs_get_end_time($job){
  137. if($job->end_time > 0){
  138. $end = format_date($job->end_time);
  139. } else {
  140. $end = '';
  141. }
  142. return $end;
  143. }
  144. /**
  145. *
  146. */
  147. function tripal_jobs_get_submit_date($job){
  148. return format_date($job->submit_date);
  149. }
  150. /**
  151. *
  152. *
  153. * @ingroup tripal_core
  154. */
  155. function tripal_jobs_launch ($do_parallel = 0){
  156. // first check if any jobs are currently running
  157. // if they are, don't continue, we don't want to have
  158. // more than one job script running at a time
  159. if(!$do_parallel and tripal_jobs_check_running()){
  160. return;
  161. }
  162. // get all jobs that have not started and order them such that
  163. // they are processed in a FIFO manner.
  164. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  165. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
  166. "ORDER BY priority ASC,job_id ASC";
  167. $job_res = db_query($sql);
  168. while($job = db_fetch_object($job_res)){
  169. // set the start time for this job
  170. $record = new stdClass();
  171. $record->job_id = $job->job_id;
  172. $record->start_time = time();
  173. $record->status = 'Running';
  174. $record->pid = getmypid();
  175. drupal_write_record('tripal_jobs',$record,'job_id');
  176. // call the function provided in the callback column.
  177. // Add the job_id as the last item in the list of arguments. All
  178. // callback functions should support this argument.
  179. $callback = $job->callback;
  180. $args = split("::",$job->arguments);
  181. $args[] = $job->job_id;
  182. print "Calling: $callback(" . implode(", ",$args) . ")\n";
  183. call_user_func_array($callback,$args);
  184. // set the end time for this job
  185. $record->end_time = time();
  186. $record->status = 'Completed';
  187. $record->progress = '100';
  188. drupal_write_record('tripal_jobs',$record,'job_id');
  189. // send an email to the user advising that the job has finished
  190. }
  191. }
  192. /**
  193. *
  194. *
  195. * @ingroup tripal_core
  196. * @ingroup tripal_api
  197. */
  198. function tripal_jobs_check_running () {
  199. // iterate through each job that has not ended
  200. // and see if it is still running. If it is not
  201. // running but does not have an end_time then
  202. // set the end time and set the status to 'Error'
  203. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  204. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  205. $jobs = db_query($sql);
  206. while($job = db_fetch_object($jobs)){
  207. if($job->pid and posix_kill($job->pid, 0)) {
  208. // the job is still running so let it go
  209. // we return 1 to indicate that a job is running
  210. print "Job is still running (pid $job->pid)\n";
  211. return 1;
  212. } else {
  213. // the job is not running so terminate it
  214. $record = new stdClass();
  215. $record->job_id = $job->job_id;
  216. $record->end_time = time();
  217. $record->status = 'Error';
  218. $record->error_msg = 'Job has terminated unexpectedly.';
  219. drupal_write_record('tripal_jobs',$record,'job_id');
  220. }
  221. }
  222. // return 1 to indicate that no jobs are currently running.
  223. return 0;
  224. }
  225. /**
  226. *
  227. */
  228. function tripal_jobs_view ($job_id){
  229. return theme('tripal_core_job_view',$job_id);
  230. }
  231. /**
  232. *
  233. */
  234. function tripal_core_preprocess_tripal_core_job_view (&$variables){
  235. // get the job record
  236. $job_id = $variables['job_id'];
  237. $sql =
  238. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  239. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  240. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  241. TJ.callback,TJ.error_msg,TJ.pid
  242. FROM {tripal_jobs} TJ
  243. INNER JOIN users U on TJ.uid = U.uid
  244. WHERE TJ.job_id = %d";
  245. $job = db_fetch_object(db_query($sql,$job_id));
  246. // we do not know what the arguments are for and we want to provide a
  247. // meaningful description to the end-user. So we use a callback function
  248. // deinfed in the module that created the job to describe in an array
  249. // the arguments provided. If the callback fails then just use the
  250. // arguments as they are
  251. $args = preg_split("/::/",$job->arguments);
  252. $arg_hook = $job->modulename."_job_describe_args";
  253. $new_args = call_user_func_array($arg_hook,array($job->callback,$args));
  254. if(is_array($new_args) and count($new_args)){
  255. $job->arguments = $new_args;
  256. } else {
  257. $job->arguments = $args;
  258. }
  259. // make our start and end times more legible
  260. $job->submit_date = tripal_jobs_get_submit_date($job);
  261. $job->start_time = tripal_jobs_get_start_time($job);
  262. $job->end_time = tripal_jobs_get_end_time($job);
  263. // add the job to the variables that get exported to the template
  264. $variables['job'] = $job;
  265. }
  266. /**
  267. *
  268. * @ingroup tripal_core
  269. * @ingroup tripal_api
  270. */
  271. function tripal_jobs_rerun ($job_id){
  272. global $user;
  273. $sql = "select * from {tripal_jobs} where job_id = %d";
  274. $job = db_fetch_object(db_query($sql,$job_id));
  275. $args = explode("::",$job->arguments);
  276. tripal_add_job ($job->job_name,$job->modulename,$job->callback,$args,$user->uid,
  277. $job->priority);
  278. drupal_goto("admin/tripal/tripal_jobs");
  279. }
  280. /**
  281. *
  282. * @ingroup tripal_core
  283. * @ingroup tripal_api
  284. */
  285. function tripal_jobs_cancel ($job_id){
  286. $sql = "select * from {tripal_jobs} where job_id = %d";
  287. $job = db_fetch_object(db_query($sql,$job_id));
  288. // set the end time for this job
  289. if($job->start_time == 0){
  290. $record = new stdClass();
  291. $record->job_id = $job->job_id;
  292. $record->end_time = time();
  293. $record->status = 'Cancelled';
  294. $record->progress = '0';
  295. drupal_write_record('tripal_jobs',$record,'job_id');
  296. drupal_set_message("Job #$job_id cancelled");
  297. } else {
  298. drupal_set_message("Job #$job_id cannot be cancelled. It is in progress or has finished.");
  299. }
  300. drupal_goto("admin/tripal/tripal_jobs");
  301. }