tripal_core_jobs.api.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions related to the Tripal Jobs API
  5. *
  6. * @defgroup tripal_jobs_api Jobs API
  7. * @ingroup tripal_core_api
  8. * @{
  9. * Tripal offers a job management subsystem for managing tasks that may require an extended period of time for
  10. * completion. Drupal uses a UNIX-based cron job to handle tasks such as checking the availability of updates,
  11. * indexing new nodes for searching, etc. Drupal's cron uses the web interface for launching these tasks, however,
  12. * Tripal provides several administrative tasks that may time out and not complete due to limitations of the web
  13. * server. Examples including syncing of a large number of features between chado and Drupal. To circumvent this,
  14. * as well as provide more fine-grained control and monitoring, Tripal uses a jobs management sub-system built into
  15. * the Tripal Core module. It is anticipated that this functionality will be used for managing analysis jobs provided by
  16. * future tools, with eventual support for distributed computing.
  17. *
  18. * The Tripal jobs management system allows administrators to submit tasks to be performed which can then be
  19. * launched through a UNIX command-line PHP script or cron job. This command-line script can be added to a cron
  20. * entry along-side the Drupal cron entry for automatic, regular launching of Tripal jobs. The order of execution of
  21. * waiting jobs is determined first by priority and second by the order the jobs were entered.
  22. *
  23. * The API functions described below provide a programmatic interface for adding, checking and viewing jobs.
  24. * @}
  25. */
  26. /**
  27. * Adds a job to the Tripal Jbo queue
  28. *
  29. * @param $job_name
  30. * The human readable name for the job
  31. * @param $modulename
  32. * The name of the module adding the job
  33. * @param $callback
  34. * The name of a function to be called when the job is executed
  35. * @param $arguments
  36. * An array of arguments to be passed on to the callback
  37. * @param $uid
  38. * The uid of the user adding the job
  39. * @param $priority
  40. * The priority at which to run the job where the highest priority is 10 and the lowest priority
  41. * is 1. The default priority is 10.
  42. *
  43. * @return
  44. * The job_id of the registered job
  45. *
  46. * Example usage:
  47. * @code
  48. * $args = array($dfile, $organism_id, $type, $library_id, $re_name, $re_uname,
  49. * $re_accession, $db_id, $rel_type, $re_subject, $parent_type, $method,
  50. * $user->uid, $analysis_id, $match_type);
  51. *
  52. * tripal_add_job("Import FASTA file: $dfile", 'tripal_feature',
  53. * 'tripal_feature_load_fasta', $args, $user->uid);
  54. * @endcode
  55. * The code above is copied from the tripal_feature/fasta_loader.php file. The
  56. * snipped first builds an array of arguments that will then be passed to the
  57. * tripal_add_job function. The number of arguments provided in the $arguments
  58. * variable should match the argument set for the callback function provided
  59. * as the third argument.
  60. *
  61. * @ingroup tripal_jobs_api
  62. */
  63. function tripal_add_job($job_name, $modulename, $callback, $arguments, $uid, $priority = 10) {
  64. // convert the arguments into a string for storage in the database
  65. $args = array();
  66. if (is_array($arguments)) {
  67. $args = serialize($arguments);
  68. }
  69. $record = new stdClass();
  70. $record->job_name = $job_name;
  71. $record->modulename = $modulename;
  72. $record->callback = $callback;
  73. $record->status = 'Waiting';
  74. $record->submit_date = REQUEST_TIME;
  75. $record->uid = $uid;
  76. $record->priority = $priority; # the lower the number the higher the priority
  77. $record->arguments = $args;
  78. if (drupal_write_record('tripal_jobs', $record)) {
  79. $jobs_url = url("admin/tripal/tripal_jobs");
  80. drupal_set_message(t("Job '%job_name' submitted. Check the <a href='!jobs_url'>jobs page</a> for status", array('%job_name' => $job_name, '!jobs_url' => $jobs_url)));
  81. }
  82. else {
  83. drupal_set_message(t("Failed to add job %job_name.", array('%job_name' => $job_name)), 'error');
  84. }
  85. return $record->job_id;
  86. }
  87. /**
  88. * Returns a list of running tripal jobs
  89. *
  90. * @return
  91. * and array of objects where each object describes a running job or FALSE if no jobs are running
  92. *
  93. * @ingroup tripal_jobs_api
  94. */
  95. function tripal_jobs_check_running() {
  96. // iterate through each job that has not ended
  97. // and see if it is still running. If it is not
  98. // running but does not have an end_time then
  99. // set the end time and set the status to 'Error'
  100. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  101. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  102. $jobs = db_query($sql);
  103. foreach ($jobs as $job) {
  104. $status = `ps -p $job->pid -o pid=`;
  105. if ($job->pid && $status) {
  106. // the job is still running so let it go
  107. // we return 1 to indicate that a job is running
  108. return TRUE;
  109. }
  110. else {
  111. // the job is not running so terminate it
  112. $record = new stdClass();
  113. $record->job_id = $job->job_id;
  114. $record->end_time = REQUEST_TIME;
  115. $record->status = 'Error';
  116. $record->error_msg = 'Job has terminated unexpectedly.';
  117. drupal_write_record('tripal_jobs', $record, 'job_id');
  118. }
  119. }
  120. // return 1 to indicate that no jobs are currently running.
  121. return FALSE;
  122. }
  123. /**
  124. * Returns the start time for a given job
  125. *
  126. * @param $job
  127. * An object describing the job
  128. *
  129. * @return
  130. * The start time of the job if it was already run and either "Cancelled" or "Not Yet Started" otherwise
  131. *
  132. * @ingroup tripal_jobs_api
  133. */
  134. function tripal_jobs_get_start_time($job) {
  135. if ($job->start_time > 0) {
  136. $start = format_date($job->start_time);
  137. }
  138. else {
  139. if (strcmp($job->job_status, 'Cancelled')==0) {
  140. $start = 'Cancelled';
  141. }
  142. else {
  143. $start = 'Not Yet Started';
  144. }
  145. }
  146. return $start;
  147. }
  148. /**
  149. * Returns the end time for a given job
  150. *
  151. * @param $job
  152. * An object describing the job
  153. *
  154. * @return
  155. * The end time of the job if it was already run and empty otherwise
  156. *
  157. * @ingroup tripal_jobs_api
  158. */
  159. function tripal_jobs_get_end_time($job) {
  160. if ($job->end_time > 0) {
  161. $end = format_date($job->end_time);
  162. }
  163. else {
  164. $end = '';
  165. }
  166. return $end;
  167. }
  168. /**
  169. * Set a job to be re-ran (ie: add it back into the job queue)
  170. *
  171. * @param $job_id
  172. * The job_id of the job to be re-ran
  173. * @param $goto_jobs_page
  174. * If set to TRUE then after the re run job is added Drupal will redirect to the jobs page
  175. *
  176. * @ingroup tripal_jobs_api
  177. */
  178. function tripal_jobs_rerun($job_id, $goto_jobs_page = TRUE) {
  179. global $user;
  180. $user_id = $user->uid;
  181. $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = :job_id";
  182. $results = db_query($sql, array(':job_id' => $job_id));
  183. $job = $results->fetchObject();
  184. // arguments for jobs used to be stored as plain string with a double colon
  185. // separating them. But as of Tripal v2.0 the arguments are stored as
  186. // a serialized array. To be backwards compatible, we should check for serialization
  187. // and if not then we will use the old style
  188. $args = unserialize($job->arguments);
  189. if (!$args) {
  190. $args = explode("::", $job->arguments);
  191. }
  192. $job_id = tripal_add_job($job->job_name, $job->modulename, $job->callback, $args, $user_id, $job->priority);
  193. if ($goto_jobs_page) {
  194. drupal_goto("admin/tripal/tripal_jobs");
  195. }
  196. return $job_id;
  197. }
  198. /**
  199. * Cancel a Tripal Job currently waiting in the job queue
  200. *
  201. * @param $job_id
  202. * The job_id of the job to be cancelled
  203. *
  204. * @ingroup tripal_jobs_api
  205. */
  206. function tripal_jobs_cancel($job_id, $redirect = TRUE) {
  207. $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = :job_id";
  208. $results = db_query($sql, array(':job_id' => $job_id));
  209. $job = $results->fetchObject();
  210. // set the end time for this job
  211. if ($job->start_time == 0) {
  212. $record = new stdClass();
  213. $record->job_id = $job->job_id;
  214. $record->end_time = REQUEST_TIME;
  215. $record->status = 'Cancelled';
  216. $record->progress = '0';
  217. drupal_write_record('tripal_jobs', $record, 'job_id');
  218. drupal_set_message(t("Job #%job_id cancelled", array('%job_id' => $job_id)));
  219. }
  220. else {
  221. drupal_set_message(t("Job %job_id cannot be cancelled. It is in progress or has finished.", array('%job_id' => $job_id)));
  222. }
  223. if ($redirect) {
  224. drupal_goto("admin/tripal/tripal_jobs");
  225. }
  226. }
  227. /**
  228. * A function used to manually launch all queued tripal jobs
  229. *
  230. * @param $do_parallel
  231. * A boolean indicating whether jobs should be attempted to run in parallel
  232. *
  233. * @param $job_id
  234. * To launch a specific job provide the job id. This option should be
  235. * used sparingly as the jobs queue managment system should launch jobs
  236. * based on order and priority. However there are times when a specific
  237. * job needs to be launched and this argument will allow it. Only jobs
  238. * which have not been run previously will run.
  239. *
  240. * @ingroup tripal_jobs_api
  241. */
  242. function tripal_jobs_launch($do_parallel = 0, $job_id = NULL) {
  243. // first check if any jobs are currently running
  244. // if they are, don't continue, we don't want to have
  245. // more than one job script running at a time
  246. if (!$do_parallel and tripal_jobs_check_running()) {
  247. print "Jobs are still running. Use the --parallel=1 option with the Drush command to run jobs in parallel.";
  248. return;
  249. }
  250. // get all jobs that have not started and order them such that
  251. // they are processed in a FIFO manner.
  252. if ($job_id) {
  253. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  254. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = :job_id " .
  255. "ORDER BY priority ASC,job_id ASC";
  256. $job_res = db_query($sql, array(':job_id', $job_id));
  257. }
  258. else {
  259. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  260. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL " .
  261. "ORDER BY priority ASC,job_id ASC";
  262. $job_res = db_query($sql);
  263. }
  264. foreach ($job_res as $job) {
  265. // set the start time for this job
  266. $record = new stdClass();
  267. $record->job_id = $job->job_id;
  268. $record->start_time = REQUEST_TIME;
  269. $record->status = 'Running';
  270. $record->pid = getmypid();
  271. drupal_write_record('tripal_jobs', $record, 'job_id');
  272. // call the function provided in the callback column.
  273. // Add the job_id as the last item in the list of arguments. All
  274. // callback functions should support this argument.
  275. $callback = $job->callback;
  276. // arguments for jobs used to be stored as plain string with a double colon
  277. // separating them. But as of Tripal v2.0 the arguments are stored as
  278. // a serialized array. To be backwards compatible, we should check for serialization
  279. // and if not then we will use the old style
  280. $args = unserialize($job->arguments);
  281. if (!$args) {
  282. $args = explode("::", $job->arguments);
  283. }
  284. $args[] = $job->job_id;
  285. print "Calling: $callback(" . implode(", ", $args) . ")\n";
  286. call_user_func_array($callback, $args);
  287. // set the end time for this job
  288. $record->end_time = REQUEST_TIME;
  289. $record->status = 'Completed';
  290. $record->progress = '100';
  291. drupal_write_record('tripal_jobs', $record, 'job_id');
  292. // send an email to the user advising that the job has finished
  293. }
  294. }
  295. /**
  296. * An internal function for setting the progress for a current job
  297. *
  298. * @param $job_id
  299. * The job_id to set the progress for
  300. * @param $percentage
  301. * The progress to set the job to
  302. *
  303. * @return
  304. * True on success and False otherwise
  305. *
  306. * @ingroup tripal_core
  307. */
  308. function tripal_job_set_progress($job_id, $percentage) {
  309. if (preg_match("/^(\d+|100)$/", $percentage)) {
  310. $record = new stdClass();
  311. $record->job_id = $job_id;
  312. $record->progress = $percentage;
  313. if (drupal_write_record('tripal_jobs', $record, 'job_id')) {
  314. return TRUE;
  315. }
  316. }
  317. return FALSE;
  318. }
  319. /**
  320. * Returns a list of jobs associated with the given module
  321. *
  322. * @param $modulename
  323. * The module to return a list of jobs for
  324. *
  325. * @return
  326. * An array of objects where each object describes a tripal job
  327. *
  328. * @ingroup tripal_jobs_api
  329. */
  330. function tripal_get_module_active_jobs($modulename) {
  331. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  332. "WHERE TJ.end_time IS NULL and TJ.modulename = :modulename ";
  333. $results = db_query($sql, array(':modulename' => $modulename));
  334. return $results->fetchObject();
  335. }
  336. /**
  337. * Returns the date the job was added to the queue
  338. *
  339. * @param $job
  340. * An object describing the job
  341. *
  342. * @return
  343. * The date teh job was submitted
  344. *
  345. * @ingroup tripal_jobs_api
  346. */
  347. function tripal_jobs_get_submit_date($job) {
  348. return format_date($job->submit_date);
  349. }