jobs.php 14 KB

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