tripal_core_jobs.api.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 arguements 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 = implode("::", $arguments);
  66. $record = new stdClass();
  67. $record->job_name = $job_name;
  68. $record->modulename = $modulename;
  69. $record->callback = $callback;
  70. $record->status = 'Waiting';
  71. $record->submit_date = REQUEST_TIME;
  72. $record->uid = $uid;
  73. $record->priority = $priority; # the lower the number the higher the priority
  74. if ($args) {
  75. $record->arguments = $args;
  76. }
  77. if (drupal_write_record('tripal_jobs', $record)) {
  78. $jobs_url = url("admin/tripal/tripal_jobs");
  79. 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)));
  80. }
  81. else {
  82. drupal_set_message(t("Failed to add job %job_name.", array('%job_name' => $job_name)), 'error');
  83. }
  84. return $record->job_id;
  85. }
  86. /**
  87. * Returns a list of running tripal jobs
  88. *
  89. * @return
  90. * and array of objects where each object describes a running job or FALSE if no jobs are running
  91. *
  92. * @ingroup tripal_jobs_api
  93. */
  94. function tripal_jobs_check_running() {
  95. // iterate through each job that has not ended
  96. // and see if it is still running. If it is not
  97. // running but does not have an end_time then
  98. // set the end time and set the status to 'Error'
  99. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  100. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  101. $jobs = db_query($sql);
  102. foreach ($jobs as $job) {
  103. $status = `ps -p $job->pid -o pid=`;
  104. if ($job->pid && $status) {
  105. // the job is still running so let it go
  106. // we return 1 to indicate that a job is running
  107. return TRUE;
  108. }
  109. else {
  110. // the job is not running so terminate it
  111. $record = new stdClass();
  112. $record->job_id = $job->job_id;
  113. $record->end_time = REQUEST_TIME;
  114. $record->status = 'Error';
  115. $record->error_msg = 'Job has terminated unexpectedly.';
  116. drupal_write_record('tripal_jobs', $record, 'job_id');
  117. }
  118. }
  119. // return 1 to indicate that no jobs are currently running.
  120. return FALSE;
  121. }
  122. /**
  123. * Returns the start time for a given job
  124. *
  125. * @param $job
  126. * An object describing the job
  127. *
  128. * @return
  129. * The start time of the job if it was already run and either "Cancelled" or "Not Yet Started" otherwise
  130. *
  131. * @ingroup tripal_jobs_api
  132. */
  133. function tripal_jobs_get_start_time($job) {
  134. if ($job->start_time > 0) {
  135. $start = format_date($job->start_time);
  136. }
  137. else {
  138. if (strcmp($job->job_status, 'Cancelled')==0) {
  139. $start = 'Cancelled';
  140. }
  141. else {
  142. $start = 'Not Yet Started';
  143. }
  144. }
  145. return $start;
  146. }
  147. /**
  148. * Returns the end time for a given job
  149. *
  150. * @param $job
  151. * An object describing the job
  152. *
  153. * @return
  154. * The end time of the job if it was already run and empty otherwise
  155. *
  156. * @ingroup tripal_jobs_api
  157. */
  158. function tripal_jobs_get_end_time($job) {
  159. if ($job->end_time > 0) {
  160. $end = format_date($job->end_time);
  161. }
  162. else {
  163. $end = '';
  164. }
  165. return $end;
  166. }
  167. /**
  168. * Set a job to be re-ran (ie: add it back into the job queue)
  169. *
  170. * @param $job_id
  171. * The job_id of the job to be re-ran
  172. *
  173. * @ingroup tripal_jobs_api
  174. */
  175. function tripal_jobs_rerun($job_id, $goto_jobs_page = TRUE) {
  176. global $user;
  177. $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = :job_id";
  178. $results = db_query($sql, array(':job_id' => $job_id));
  179. $job = $results->fetchObject();
  180. $args = explode("::", $job->arguments);
  181. $job_id = tripal_add_job($job->job_name, $job->modulename, $job->callback, $args, $user->uid, $job->priority);
  182. if ($goto_jobs_page) {
  183. drupal_goto("admin/tripal/tripal_jobs");
  184. }
  185. return $job_id;
  186. }
  187. /**
  188. * Cancel a Tripal Job currently waiting in the job queue
  189. *
  190. * @param $job_id
  191. * The job_id of the job to be cancelled
  192. *
  193. * @ingroup tripal_jobs_api
  194. */
  195. function tripal_jobs_cancel($job_id, $redirect = TRUE) {
  196. $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = :job_id";
  197. $results = db_query($sql, array(':job_id' => $job_id));
  198. $job = $results->fetchObject();
  199. // set the end time for this job
  200. if ($job->start_time == 0) {
  201. $record = new stdClass();
  202. $record->job_id = $job->job_id;
  203. $record->end_time = REQUEST_TIME;
  204. $record->status = 'Cancelled';
  205. $record->progress = '0';
  206. drupal_write_record('tripal_jobs', $record, 'job_id');
  207. drupal_set_message(t("Job #%job_id cancelled", array('%job_id' => $job_id)));
  208. }
  209. else {
  210. drupal_set_message(t("Job %job_id cannot be cancelled. It is in progress or has finished.", array('%job_id' => $job_id)));
  211. }
  212. if ($redirect) {
  213. drupal_goto("admin/tripal/tripal_jobs");
  214. }
  215. }
  216. /**
  217. * A function used to manually launch all queued tripal jobs
  218. *
  219. * @param $do_parallel
  220. * A boolean indicating whether jobs should be attempted to run in parallel
  221. *
  222. * @param $job_id
  223. * To launch a specific job provide the job id. This option should be
  224. * used sparingly as the jobs queue managment system should launch jobs
  225. * based on order and priority. However there are times when a specific
  226. * job needs to be launched and this argument will allow it. Only jobs
  227. * which have not been run previously will run.
  228. *
  229. * @ingroup tripal_jobs_api
  230. */
  231. function tripal_jobs_launch($do_parallel = 0, $job_id = NULL) {
  232. // first check if any jobs are currently running
  233. // if they are, don't continue, we don't want to have
  234. // more than one job script running at a time
  235. if (!$do_parallel and tripal_jobs_check_running()) {
  236. print "Jobs are still running. Use the --parallel=1 option with the Drush command to run jobs in parallel.";
  237. return;
  238. }
  239. // get all jobs that have not started and order them such that
  240. // they are processed in a FIFO manner.
  241. if ($job_id) {
  242. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  243. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = :job_id " .
  244. "ORDER BY priority ASC,job_id ASC";
  245. $job_res = db_query($sql, array(':job_id', $job_id));
  246. }
  247. else {
  248. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  249. "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL " .
  250. "ORDER BY priority ASC,job_id ASC";
  251. $job_res = db_query($sql);
  252. }
  253. foreach ($job_res as $job) {
  254. // set the start time for this job
  255. $record = new stdClass();
  256. $record->job_id = $job->job_id;
  257. $record->start_time = REQUEST_TIME;
  258. $record->status = 'Running';
  259. $record->pid = getmypid();
  260. drupal_write_record('tripal_jobs', $record, 'job_id');
  261. // call the function provided in the callback column.
  262. // Add the job_id as the last item in the list of arguments. All
  263. // callback functions should support this argument.
  264. $callback = $job->callback;
  265. $args = split("::", $job->arguments);
  266. $args[] = $job->job_id;
  267. print "Calling: $callback(" . implode(", ", $args) . ")\n";
  268. call_user_func_array($callback, $args);
  269. // set the end time for this job
  270. $record->end_time = REQUEST_TIME;
  271. $record->status = 'Completed';
  272. $record->progress = '100';
  273. drupal_write_record('tripal_jobs', $record, 'job_id');
  274. // send an email to the user advising that the job has finished
  275. }
  276. }
  277. /**
  278. * An internal function for setting the progress for a current job
  279. *
  280. * @param $job_id
  281. * The job_id to set the progress for
  282. * @param $percentage
  283. * The progress to set the job to
  284. *
  285. * @return
  286. * True on success and False otherwise
  287. *
  288. * @ingroup tripal_core
  289. */
  290. function tripal_job_set_progress($job_id, $percentage) {
  291. if (preg_match("/^(\d+|100)$/", $percentage)) {
  292. $record = new stdClass();
  293. $record->job_id = $job_id;
  294. $record->progress = $percentage;
  295. if (drupal_write_record('tripal_jobs', $record, 'job_id')) {
  296. return TRUE;
  297. }
  298. }
  299. return FALSE;
  300. }
  301. /**
  302. * Returns a list of jobs associated with the given module
  303. *
  304. * @param $modulename
  305. * The module to return a list of jobs for
  306. *
  307. * @return
  308. * An array of objects where each object describes a tripal job
  309. *
  310. * @ingroup tripal_jobs_api
  311. */
  312. function tripal_get_module_active_jobs($modulename) {
  313. $sql = "SELECT * FROM {tripal_jobs} TJ " .
  314. "WHERE TJ.end_time IS NULL and TJ.modulename = :modulename ";
  315. $results = db_query($sql, array(':modulename' => $modulename));
  316. return $results->fetchObject();
  317. }
  318. /**
  319. * Returns the date the job was added to the queue
  320. *
  321. * @param $job
  322. * An object describing the job
  323. *
  324. * @return
  325. * The date teh job was submitted
  326. *
  327. * @ingroup tripal_jobs_api
  328. */
  329. function tripal_jobs_get_submit_date($job) {
  330. return format_date($job->submit_date);
  331. }