jobs.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * @defgroup tripal_jobs_api Tripal Jobs API
  4. * @{
  5. * Provides an application programming interface (API) to interact with the Tripal Jobs
  6. * @}
  7. * @ingroup tripal_api
  8. */
  9. /**
  10. * Adds a job to the Tripal Jbo queue
  11. *
  12. * @param $job_name
  13. * The human readable name for the job
  14. * @param $modulename
  15. * The name of the module adding the job
  16. * @param $callback
  17. * The name of a function to be called when the job is executed
  18. * @param $arguments
  19. * An array of arguements to be passed on to the callback
  20. * @param $uid
  21. * The uid of the user adding the job
  22. * @param $priority
  23. * The priority at which to run the job where the highest priority is 10 and the lowest priority
  24. * is 1. The default priority is 10.
  25. *
  26. * @return
  27. * The job_id of the registered job
  28. *
  29. * @ingroup tripal_jobs_api
  30. */
  31. function tripal_add_job ($job_name,$modulename,$callback,$arguments,$uid,$priority = 10){
  32. # convert the arguments into a string for storage in the database
  33. $args = implode("::",$arguments);
  34. $record = new stdClass();
  35. $record->job_name = $job_name;
  36. $record->modulename = $modulename;
  37. $record->callback = $callback;
  38. $record->status = 'Waiting';
  39. $record->submit_date = time();
  40. $record->uid = $uid;
  41. $record->priority = $priority; # the lower the number the higher the priority
  42. if($args){
  43. $record->arguments = $args;
  44. }
  45. if(drupal_write_record('tripal_jobs',$record)){
  46. $jobs_url = url("admin/tripal/tripal_jobs");
  47. drupal_set_message(t("Job '$job_name' submitted. Check the <a href='$jobs_url'>jobs page</a> for status"));
  48. } else {
  49. drupal_set_message("Failed to add job $job_name.");
  50. }
  51. return $record->job_id;
  52. }
  53. /**
  54. * An internal function for setting the progress for a current job
  55. *
  56. * @param $job_id
  57. * The job_id to set the progress for
  58. * @param $percentage
  59. * The progress to set the job to
  60. *
  61. * @return
  62. * True on success and False otherwise
  63. *
  64. * @ingroup tripal_core
  65. */
  66. function tripal_job_set_progress($job_id,$percentage){
  67. if(preg_match("/^(\d\d|100)$/",$percentage)){
  68. $record = new stdClass();
  69. $record->job_id = $job_id;
  70. $record->progress = $percentage;
  71. if(drupal_write_record('tripal_jobs',$record,'job_id')){
  72. return 1;
  73. }
  74. }
  75. return 0;
  76. }
  77. /**
  78. * Returns a list of jobs associated with the given module
  79. *
  80. * @param $modulename
  81. * The module to return a list of jobs for
  82. *
  83. * @return
  84. * An array of objects where each object describes a tripal job
  85. *
  86. * @ingroup tripal_jobs_api
  87. */
  88. function tripal_get_module_active_jobs ($modulename){
  89. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  90. "WHERE TJ.end_time IS NULL and TJ.modulename = '%s' ";
  91. return db_fetch_object(db_query($sql,$modulename));
  92. }
  93. /**
  94. * Returns the Tripal Job Report
  95. *
  96. * @return
  97. * The HTML to be rendered which describes the job report
  98. *
  99. * @ingroup tripal_core
  100. */
  101. function tripal_jobs_report () {
  102. //$jobs = db_query("SELECT * FROM {tripal_jobs} ORDER BY job_id DESC");
  103. $jobs = pager_query(
  104. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  105. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  106. TJ.end_time,TJ.priority,U.name as username
  107. FROM {tripal_jobs} TJ
  108. INNER JOIN users U on TJ.uid = U.uid
  109. ORDER BY job_id DESC", 10,0,"SELECT count(*) FROM {tripal_jobs}");
  110. // create a table with each row containig stats for
  111. // an individual job in the results set.
  112. $output .= "Waiting jobs are executed first by priority level (the lower the ".
  113. "number the higher the priority) and second by the order they ".
  114. "were entered";
  115. $output .= "<table class=\"tripal-table tripal-table-horz\">".
  116. " <tr>".
  117. " <th>Job ID</th>".
  118. " <th>User</th>".
  119. " <th>Job Name</th>".
  120. " <th nowrap>Dates</th>".
  121. " <th>Priority</th>".
  122. " <th>Progress</th>".
  123. " <th>Status</th>".
  124. " <th>Actions</th>".
  125. " </tr>";
  126. $i = 0;
  127. while($job = db_fetch_object($jobs)){
  128. $class = 'tripal-table-odd-row';
  129. if($i % 2 == 0 ){
  130. $class = 'tripal-table-even-row';
  131. }
  132. $submit = tripal_jobs_get_submit_date($job);
  133. $start = tripal_jobs_get_start_time($job);
  134. $end = tripal_jobs_get_end_time($job);
  135. $cancel_link = '';
  136. if($job->start_time == 0 and $job->end_time == 0){
  137. $cancel_link = "<a href=\"".url("admin/tripal/tripal_jobs/cancel/".$job->job_id)."\">Cancel</a><br>";
  138. }
  139. $rerun_link = "<a href=\"".url("admin/tripal/tripal_jobs/rerun/".$job->job_id)."\">Re-run</a><br>";
  140. $view_link ="<a href=\"".url("admin/tripal/tripal_jobs/view/".$job->job_id)."\">View</a>";
  141. $output .= " <tr class=\"$class\">";
  142. $output .= " <td>$job->job_id</td>".
  143. " <td>$job->username</td>".
  144. " <td>$job->job_name</td>".
  145. " <td nowrap>Submit Date: $submit".
  146. " <br>Start Time: $start".
  147. " <br>End Time: $end</td>".
  148. " <td>$job->priority</td>".
  149. " <td>$job->progress%</td>".
  150. " <td>$job->job_status</td>".
  151. " <td>$cancel_link $rerun_link $view_link</td>".
  152. " </tr>";
  153. $i++;
  154. }
  155. $output .= "</table>";
  156. $output .= theme_pager();
  157. return $output;
  158. }
  159. /**
  160. * Returns the start time for a given job
  161. *
  162. * @param $job
  163. * An object describing the job
  164. *
  165. * @return
  166. * The start time of the job if it was already run and either "Cancelled" or "Not Yet Started" otherwise
  167. *
  168. * @ingroup tripal_jobs_api
  169. */
  170. function tripal_jobs_get_start_time($job){
  171. if($job->start_time > 0){
  172. $start = format_date($job->start_time);
  173. } else {
  174. if(strcmp($job->job_status,'Cancelled')==0){
  175. $start = 'Cancelled';
  176. } else {
  177. $start = 'Not Yet Started';
  178. }
  179. }
  180. return $start;
  181. }
  182. /**
  183. * Returns the end time for a given job
  184. *
  185. * @param $job
  186. * An object describing the job
  187. *
  188. * @return
  189. * The end time of the job if it was already run and empty otherwise
  190. *
  191. * @ingroup tripal_jobs_api
  192. */
  193. function tripal_jobs_get_end_time($job){
  194. if($job->end_time > 0){
  195. $end = format_date($job->end_time);
  196. } else {
  197. $end = '';
  198. }
  199. return $end;
  200. }
  201. /**
  202. * Returns the date the job was added to the queue
  203. *
  204. * @param $job
  205. * An object describing the job
  206. *
  207. * @return
  208. * The date teh job was submitted
  209. *
  210. * @ingroup tripal_jobs_api
  211. */
  212. function tripal_jobs_get_submit_date($job){
  213. return format_date($job->submit_date);
  214. }
  215. /**
  216. * A function used to manually launch all queued tripal jobs
  217. *
  218. * @param $do_parallel
  219. * A boolean indicating whether jobs should be attempted to run in parallel
  220. *
  221. * @ingroup tripal_jobs_api
  222. */
  223. function tripal_jobs_launch ($do_parallel = 0){
  224. // first check if any jobs are currently running
  225. // if they are, don't continue, we don't want to have
  226. // more than one job script running at a time
  227. if(!$do_parallel and tripal_jobs_check_running()){
  228. return;
  229. }
  230. // get all jobs that have not started and order them such that
  231. // they are processed in a FIFO manner.
  232. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  233. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
  234. "ORDER BY priority ASC,job_id ASC";
  235. $job_res = db_query($sql);
  236. while($job = db_fetch_object($job_res)){
  237. // set the start time for this job
  238. $record = new stdClass();
  239. $record->job_id = $job->job_id;
  240. $record->start_time = time();
  241. $record->status = 'Running';
  242. $record->pid = getmypid();
  243. drupal_write_record('tripal_jobs',$record,'job_id');
  244. // call the function provided in the callback column.
  245. // Add the job_id as the last item in the list of arguments. All
  246. // callback functions should support this argument.
  247. $callback = $job->callback;
  248. $args = split("::",$job->arguments);
  249. $args[] = $job->job_id;
  250. print "Calling: $callback(" . implode(", ",$args) . ")\n";
  251. call_user_func_array($callback,$args);
  252. // set the end time for this job
  253. $record->end_time = time();
  254. $record->status = 'Completed';
  255. $record->progress = '100';
  256. drupal_write_record('tripal_jobs',$record,'job_id');
  257. // send an email to the user advising that the job has finished
  258. }
  259. }
  260. /**
  261. * Returns a list of running tripal jobs
  262. *
  263. * @return
  264. * and array of objects where each object describes a running job or false if no jobs are running
  265. *
  266. * @ingroup tripal_jobs_api
  267. */
  268. function tripal_jobs_check_running () {
  269. // iterate through each job that has not ended
  270. // and see if it is still running. If it is not
  271. // running but does not have an end_time then
  272. // set the end time and set the status to 'Error'
  273. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  274. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  275. $jobs = db_query($sql);
  276. while($job = db_fetch_object($jobs)){
  277. if($job->pid and posix_kill($job->pid, 0)) {
  278. // the job is still running so let it go
  279. // we return 1 to indicate that a job is running
  280. print "Job is still running (pid $job->pid)\n";
  281. return 1;
  282. } else {
  283. // the job is not running so terminate it
  284. $record = new stdClass();
  285. $record->job_id = $job->job_id;
  286. $record->end_time = time();
  287. $record->status = 'Error';
  288. $record->error_msg = 'Job has terminated unexpectedly.';
  289. drupal_write_record('tripal_jobs',$record,'job_id');
  290. }
  291. }
  292. // return 1 to indicate that no jobs are currently running.
  293. return 0;
  294. }
  295. /**
  296. * Returns the HTML code to display a given job
  297. *
  298. * @param $job_id
  299. * The job_id of the job to display
  300. *
  301. * @return
  302. * The HTML describing the indicated job
  303. * @ingroup tripal_core
  304. */
  305. function tripal_jobs_view ($job_id){
  306. return theme('tripal_core_job_view',$job_id);
  307. }
  308. /**
  309. * Registers variables for the tripal_core_job_view themeing function
  310. *
  311. * @param $variables
  312. * An array containing all variables supplied to this template
  313. *
  314. * @ingroup tripal_core
  315. */
  316. function tripal_core_preprocess_tripal_core_job_view (&$variables){
  317. // get the job record
  318. $job_id = $variables['job_id'];
  319. $sql =
  320. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  321. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  322. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  323. TJ.callback,TJ.error_msg,TJ.pid
  324. FROM {tripal_jobs} TJ
  325. INNER JOIN users U on TJ.uid = U.uid
  326. WHERE TJ.job_id = %d";
  327. $job = db_fetch_object(db_query($sql,$job_id));
  328. // we do not know what the arguments are for and we want to provide a
  329. // meaningful description to the end-user. So we use a callback function
  330. // deinfed in the module that created the job to describe in an array
  331. // the arguments provided. If the callback fails then just use the
  332. // arguments as they are
  333. $args = preg_split("/::/",$job->arguments);
  334. $arg_hook = $job->modulename."_job_describe_args";
  335. if(is_callable($arg_hook)){
  336. $new_args = call_user_func_array($arg_hook,array($job->callback,$args));
  337. if(is_array($new_args) and count($new_args)){
  338. $job->arguments = $new_args;
  339. } else {
  340. $job->arguments = $args;
  341. }
  342. } else {
  343. $job->arguments = $args;
  344. }
  345. // make our start and end times more legible
  346. $job->submit_date = tripal_jobs_get_submit_date($job);
  347. $job->start_time = tripal_jobs_get_start_time($job);
  348. $job->end_time = tripal_jobs_get_end_time($job);
  349. // add the job to the variables that get exported to the template
  350. $variables['job'] = $job;
  351. }
  352. /**
  353. * Set a job to be re-ran (ie: add it back into the job queue)
  354. *
  355. * @param $job_id
  356. * The job_id of the job to be re-ran
  357. *
  358. * @ingroup tripal_jobs_api
  359. */
  360. function tripal_jobs_rerun ($job_id){
  361. global $user;
  362. $sql = "select * from {tripal_jobs} where job_id = %d";
  363. $job = db_fetch_object(db_query($sql,$job_id));
  364. $args = explode("::",$job->arguments);
  365. tripal_add_job ($job->job_name,$job->modulename,$job->callback,$args,$user->uid,
  366. $job->priority);
  367. drupal_goto("admin/tripal/tripal_jobs");
  368. }
  369. /**
  370. * Cancel a Tripal Job currently waiting in the job queue
  371. *
  372. * @param $job_id
  373. * The job_id of the job to be cancelled
  374. *
  375. * @ingroup tripal_jobs_api
  376. */
  377. function tripal_jobs_cancel ($job_id){
  378. $sql = "select * from {tripal_jobs} where job_id = %d";
  379. $job = db_fetch_object(db_query($sql,$job_id));
  380. // set the end time for this job
  381. if($job->start_time == 0){
  382. $record = new stdClass();
  383. $record->job_id = $job->job_id;
  384. $record->end_time = time();
  385. $record->status = 'Cancelled';
  386. $record->progress = '0';
  387. drupal_write_record('tripal_jobs',$record,'job_id');
  388. drupal_set_message("Job #$job_id cancelled");
  389. } else {
  390. drupal_set_message("Job #$job_id cannot be cancelled. It is in progress or has finished.");
  391. }
  392. drupal_goto("admin/tripal/tripal_jobs");
  393. }