jobs.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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,$priority = 10){
  9. # convert the arguments into a string for storage in the database
  10. $args = implode("::",$arguments);
  11. $record = new stdClass();
  12. $record->job_name = $job_name;
  13. $record->modulename = $modulename;
  14. $record->callback = $callback;
  15. $record->status = 'Waiting';
  16. $record->submit_date = time();
  17. $record->uid = $uid;
  18. $record->priority = $priority; # the lower the number the higher the priority
  19. if($args){
  20. $record->arguments = $args;
  21. }
  22. if(drupal_write_record('tripal_jobs',$record)){
  23. $jobs_url = url("admin/tripal/tripal_jobs");
  24. drupal_set_message(t("Job '$job_name' submitted. Check the <a href='$jobs_url'>jobs page</a> for status"));
  25. } else {
  26. drupal_set_message("Failed to add job $job_name.");
  27. }
  28. return $record->job_id;
  29. }
  30. /**
  31. *
  32. *
  33. * @ingroup tripal_core
  34. * @ingroup tripal_api
  35. */
  36. function tripal_job_set_progress($job_id,$percentage){
  37. if(preg_match("/^(\d\d|100)$/",$percentage)){
  38. $record = new stdClass();
  39. $record->job_id = $job_id;
  40. $record->progress = $percentage;
  41. if(drupal_write_record('tripal_jobs',$record,'job_id')){
  42. return 1;
  43. }
  44. }
  45. return 0;
  46. }
  47. /**
  48. * Returns a list of jobs associated with the given module
  49. *
  50. * @ingroup tripal_core
  51. */
  52. function tripal_get_module_active_jobs ($modulename){
  53. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  54. "WHERE TJ.end_time IS NULL and TJ.modulename = '%s' ";
  55. return db_fetch_object(db_query($sql,$modulename));
  56. }
  57. /**
  58. *
  59. *
  60. * @ingroup tripal_core
  61. */
  62. function tripal_jobs_report () {
  63. //$jobs = db_query("SELECT * FROM {tripal_jobs} ORDER BY job_id DESC");
  64. $jobs = pager_query(
  65. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  66. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  67. TJ.end_time,TJ.priority,U.name as username
  68. FROM {tripal_jobs} TJ
  69. INNER JOIN users U on TJ.uid = U.uid
  70. ORDER BY job_id DESC", 10,0,"SELECT count(*) FROM {tripal_jobs}");
  71. // create a table with each row containig stats for
  72. // an individual job in the results set.
  73. $output .= "Waiting jobs are executed first by priority level (the lower the ".
  74. "number the higher the priority) and second by the order they ".
  75. "were entered";
  76. $output .= "<table class=\"tripal-table tripal-table-horz\">".
  77. " <tr>".
  78. " <th>Job ID</th>".
  79. " <th>User</th>".
  80. " <th>Job Name</th>".
  81. " <th nowrap>Dates</th>".
  82. " <th>Priority</th>".
  83. " <th>Progress</th>".
  84. " <th>Status</th>".
  85. " <th>Actions</th>".
  86. " </tr>";
  87. $i = 0;
  88. while($job = db_fetch_object($jobs)){
  89. $class = 'tripal-table-odd-row';
  90. if($i % 2 == 0 ){
  91. $class = 'tripal-table-even-row';
  92. }
  93. $submit = tripal_jobs_get_submit_date($job);
  94. $start = tripal_jobs_get_start_time($job);
  95. $end = tripal_jobs_get_end_time($job);
  96. $cancel_link = '';
  97. if($job->start_time == 0 and $job->end_time == 0){
  98. $cancel_link = "<a href=\"".url("admin/tripal/tripal_jobs/cancel/".$job->job_id)."\">Cancel</a><br>";
  99. }
  100. $rerun_link = "<a href=\"".url("admin/tripal/tripal_jobs/rerun/".$job->job_id)."\">Re-run</a><br>";
  101. $view_link ="<a href=\"".url("admin/tripal/tripal_jobs/view/".$job->job_id)."\">View</a>";
  102. $output .= " <tr class=\"$class\">";
  103. $output .= " <td>$job->job_id</td>".
  104. " <td>$job->username</td>".
  105. " <td>$job->job_name</td>".
  106. " <td nowrap>Submit Date: $submit".
  107. " <br>Start Time: $start".
  108. " <br>End Time: $end</td>".
  109. " <td>$job->priority</td>".
  110. " <td>$job->progress%</td>".
  111. " <td>$job->job_status</td>".
  112. " <td>$cancel_link $rerun_link $view_link</td>".
  113. " </tr>";
  114. $i++;
  115. }
  116. $output .= "</table>";
  117. $output .= theme_pager();
  118. return $output;
  119. }
  120. /**
  121. *
  122. */
  123. function tripal_jobs_get_start_time($job){
  124. if($job->start_time > 0){
  125. $start = format_date($job->start_time);
  126. } else {
  127. if(strcmp($job->job_status,'Cancelled')==0){
  128. $start = 'Cancelled';
  129. } else {
  130. $start = 'Not Yet Started';
  131. }
  132. }
  133. return $start;
  134. }
  135. /**
  136. *
  137. */
  138. function tripal_jobs_get_end_time($job){
  139. if($job->end_time > 0){
  140. $end = format_date($job->end_time);
  141. } else {
  142. $end = '';
  143. }
  144. return $end;
  145. }
  146. /**
  147. *
  148. */
  149. function tripal_jobs_get_submit_date($job){
  150. return format_date($job->submit_date);
  151. }
  152. /**
  153. *
  154. *
  155. * @ingroup tripal_core
  156. */
  157. function tripal_jobs_launch ($do_parallel = 0){
  158. // first check if any jobs are currently running
  159. // if they are, don't continue, we don't want to have
  160. // more than one job script running at a time
  161. if(!$do_parallel and tripal_jobs_check_running()){
  162. return;
  163. }
  164. // get all jobs that have not started and order them such that
  165. // they are processed in a FIFO manner.
  166. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  167. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
  168. "ORDER BY priority ASC,job_id ASC";
  169. $job_res = db_query($sql);
  170. while($job = db_fetch_object($job_res)){
  171. // set the start time for this job
  172. $record = new stdClass();
  173. $record->job_id = $job->job_id;
  174. $record->start_time = time();
  175. $record->status = 'Running';
  176. $record->pid = getmypid();
  177. drupal_write_record('tripal_jobs',$record,'job_id');
  178. // call the function provided in the callback column.
  179. // Add the job_id as the last item in the list of arguments. All
  180. // callback functions should support this argument.
  181. $callback = $job->callback;
  182. $args = split("::",$job->arguments);
  183. $args[] = $job->job_id;
  184. print "Calling: $callback(" . implode(", ",$args) . ")\n";
  185. call_user_func_array($callback,$args);
  186. // set the end time for this job
  187. $record->end_time = time();
  188. $record->status = 'Completed';
  189. $record->progress = '100';
  190. drupal_write_record('tripal_jobs',$record,'job_id');
  191. // send an email to the user advising that the job has finished
  192. }
  193. }
  194. /**
  195. *
  196. *
  197. * @ingroup tripal_core
  198. * @ingroup tripal_api
  199. */
  200. function tripal_jobs_check_running () {
  201. // iterate through each job that has not ended
  202. // and see if it is still running. If it is not
  203. // running but does not have an end_time then
  204. // set the end time and set the status to 'Error'
  205. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  206. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  207. $jobs = db_query($sql);
  208. while($job = db_fetch_object($jobs)){
  209. if($job->pid and posix_kill($job->pid, 0)) {
  210. // the job is still running so let it go
  211. // we return 1 to indicate that a job is running
  212. print "Job is still running (pid $job->pid)\n";
  213. return 1;
  214. } else {
  215. // the job is not running so terminate it
  216. $record = new stdClass();
  217. $record->job_id = $job->job_id;
  218. $record->end_time = time();
  219. $record->status = 'Error';
  220. $record->error_msg = 'Job has terminated unexpectedly.';
  221. drupal_write_record('tripal_jobs',$record,'job_id');
  222. }
  223. }
  224. // return 1 to indicate that no jobs are currently running.
  225. return 0;
  226. }
  227. /**
  228. *
  229. */
  230. function tripal_jobs_view ($job_id){
  231. return theme('tripal_core_job_view',$job_id);
  232. }
  233. /**
  234. *
  235. */
  236. function tripal_core_preprocess_tripal_core_job_view (&$variables){
  237. // get the job record
  238. $job_id = $variables['job_id'];
  239. $sql =
  240. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  241. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  242. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  243. TJ.callback,TJ.error_msg,TJ.pid
  244. FROM {tripal_jobs} TJ
  245. INNER JOIN users U on TJ.uid = U.uid
  246. WHERE TJ.job_id = %d";
  247. $job = db_fetch_object(db_query($sql,$job_id));
  248. // we do not know what the arguments are for and we want to provide a
  249. // meaningful description to the end-user. So we use a callback function
  250. // deinfed in the module that created the job to describe in an array
  251. // the arguments provided. If the callback fails then just use the
  252. // arguments as they are
  253. $args = preg_split("/::/",$job->arguments);
  254. $arg_hook = $job->modulename."_job_describe_args";
  255. if(is_callable($arg_hook)){
  256. $new_args = call_user_func_array($arg_hook,array($job->callback,$args));
  257. if(is_array($new_args) and count($new_args)){
  258. $job->arguments = $new_args;
  259. } else {
  260. $job->arguments = $args;
  261. }
  262. } else {
  263. $job->arguments = $args;
  264. }
  265. // make our start and end times more legible
  266. $job->submit_date = tripal_jobs_get_submit_date($job);
  267. $job->start_time = tripal_jobs_get_start_time($job);
  268. $job->end_time = tripal_jobs_get_end_time($job);
  269. // add the job to the variables that get exported to the template
  270. $variables['job'] = $job;
  271. }
  272. /**
  273. *
  274. * @ingroup tripal_core
  275. * @ingroup tripal_api
  276. */
  277. function tripal_jobs_rerun ($job_id){
  278. global $user;
  279. $sql = "select * from {tripal_jobs} where job_id = %d";
  280. $job = db_fetch_object(db_query($sql,$job_id));
  281. $args = explode("::",$job->arguments);
  282. tripal_add_job ($job->job_name,$job->modulename,$job->callback,$args,$user->uid,
  283. $job->priority);
  284. drupal_goto("admin/tripal/tripal_jobs");
  285. }
  286. /**
  287. *
  288. * @ingroup tripal_core
  289. * @ingroup tripal_api
  290. */
  291. function tripal_jobs_cancel ($job_id){
  292. $sql = "select * from {tripal_jobs} where job_id = %d";
  293. $job = db_fetch_object(db_query($sql,$job_id));
  294. // set the end time for this job
  295. if($job->start_time == 0){
  296. $record = new stdClass();
  297. $record->job_id = $job->job_id;
  298. $record->end_time = time();
  299. $record->status = 'Cancelled';
  300. $record->progress = '0';
  301. drupal_write_record('tripal_jobs',$record,'job_id');
  302. drupal_set_message("Job #$job_id cancelled");
  303. } else {
  304. drupal_set_message("Job #$job_id cannot be cancelled. It is in progress or has finished.");
  305. }
  306. drupal_goto("admin/tripal/tripal_jobs");
  307. }