jobs.inc

Contains functions related to the display of Tripal jobs in a Tripal website.

File

tripal_core/includes/jobs.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions related to the display of Tripal jobs in a Tripal website.
  5. *
  6. */
  7. /**
  8. *
  9. * @ingroup tripal_core
  10. */
  11. function tripal_jobs_report_form($form, &$form_state = NULL) {
  12. $form = array();
  13. // set the default values
  14. $default_status = $form_state['values']['job_status'];
  15. $default_job_name = $form_state['values']['job_name'];
  16. if (!$default_status) {
  17. $default_status = $_SESSION['tripal_job_filter']['job_status'];
  18. }
  19. if (!$default_job_name) {
  20. $default_job_name = $_SESSION['tripal_job_filter']['job_name'];
  21. }
  22. $form['job_status'] = array(
  23. '#type' => 'select',
  24. '#title' => t('Filter by Job Status'),
  25. '#default_value' => $default_status,
  26. '#options' => array(
  27. 0 => 'All Jobs',
  28. 'Running' => 'Running',
  29. 'Waiting' => 'Waiting',
  30. 'Completed' => 'Completed',
  31. 'Cancelled' => 'Cancelled',
  32. 'Error' => 'Error',
  33. ),
  34. );
  35. $form['job_name'] = array(
  36. '#type' => 'textfield',
  37. '#title' => t('Filter by Job Name'),
  38. '#description' => t('The jobs will be filtered if text provided is contained in the job name'),
  39. '#default_value' => $default_job_name,
  40. );
  41. $form['submit'] = array(
  42. '#type' => 'submit',
  43. '#value' => t('Filter'),
  44. );
  45. return $form;
  46. }
  47. /**
  48. *
  49. * @ingroup tripal_core
  50. */
  51. function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
  52. $job_status = $form_state['values']['job_status'];
  53. $job_name = $form_state['values']['job_name'];
  54. $_SESSION['tripal_job_filter']['job_status'] = $job_status;
  55. $_SESSION['tripal_job_filter']['job_name'] = $job_name;
  56. }
  57. /**
  58. * Returns the Tripal Job Report
  59. *
  60. * @return
  61. * The HTML to be rendered which describes the job report
  62. *
  63. * @ingroup tripal_core
  64. */
  65. function tripal_jobs_report() {
  66. // run the following function which will
  67. // change the status of jobs that have errored out
  68. tripal_jobs_check_running();
  69. $job_status = $_SESSION['tripal_job_filter']['job_status'];
  70. $job_name = $_SESSION['tripal_job_filter']['job_name'];
  71. $sql = "
  72. SELECT
  73. TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  74. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  75. TJ.end_time,TJ.priority,U.name as username
  76. FROM {tripal_jobs} TJ
  77. INNER JOIN {users} U on TJ.uid = U.uid
  78. WHERE 1=1
  79. ";
  80. $args = array();
  81. if ($job_status) {
  82. $sql .= "AND TJ.status = '%s' ";
  83. $args[] = $job_status;
  84. }
  85. if ($job_name) {
  86. $sql .= "AND TJ.job_name like '%%%s%%'";
  87. $args[] = $job_name;
  88. }
  89. $sql .= "ORDER BY job_id DESC";
  90. $jobs = pager_query($sql, 25, 0, "SELECT count(*) FROM ($sql) as t1", $args);
  91. $header = array(
  92. 'Job ID',
  93. 'User',
  94. 'Job Name',
  95. array('data' => 'Dates', 'style'=> "white-space: nowrap"),
  96. 'Priority',
  97. 'Progress',
  98. 'Status',
  99. 'Action');
  100. $rows = array();
  101. // iterate through the jobs
  102. while ($job = db_fetch_object($jobs)) {
  103. $submit = tripal_jobs_get_submit_date($job);
  104. $start = tripal_jobs_get_start_time($job);
  105. $end = tripal_jobs_get_end_time($job);
  106. $cancel_link = '';
  107. if ($job->start_time == 0 and $job->end_time == 0) {
  108. $cancel_link = "<a href=\"" . url("admin/tripal/tripal_jobs/cancel/" . $job->job_id) . "\">Cancel</a><br />";
  109. }
  110. $rerun_link = "<a href=\"" . url("admin/tripal/tripal_jobs/rerun/" . $job->job_id) . "\">Re-run</a><br />";
  111. $view_link ="<a href=\"" . url("admin/tripal/tripal_jobs/view/" . $job->job_id) . "\">View</a>";
  112. $rows[] = array(
  113. $job->job_id,
  114. $job->username,
  115. $job->job_name,
  116. "Submit Date: $submit<br>Start Time: $start<br>End Time: $end",
  117. $job->priority,
  118. $job->progress . '%',
  119. $job->job_status,
  120. "$cancel_link $rerun_link $view_link",
  121. );
  122. }
  123. // create the report page
  124. $output .= "Waiting jobs are executed first by priority level (the lower the ".
  125. "number the higher the priority) and second by the order they ".
  126. "were entered";
  127. $output .= drupal_get_form('tripal_jobs_report_form');
  128. $output .= theme('table', $header, $rows);
  129. $output .= theme_pager();
  130. return $output;
  131. }
  132. /**
  133. * Returns the HTML code to display a given job
  134. *
  135. * @param $job_id
  136. * The job_id of the job to display
  137. *
  138. * @return
  139. * The HTML describing the indicated job
  140. * @ingroup tripal_core
  141. */
  142. function tripal_jobs_view($job_id) {
  143. return theme('tripal_core_job_view', $job_id);
  144. }
  145. /**
  146. * Registers variables for the tripal_core_job_view themeing function
  147. *
  148. * @param $variables
  149. * An array containing all variables supplied to this template
  150. *
  151. * @ingroup tripal_core
  152. */
  153. function tripal_core_preprocess_tripal_core_job_view(&$variables) {
  154. // get the job record
  155. $job_id = $variables['job_id'];
  156. $sql =
  157. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  158. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  159. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  160. TJ.callback,TJ.error_msg,TJ.pid
  161. FROM {tripal_jobs} TJ
  162. INNER JOIN users U on TJ.uid = U.uid
  163. WHERE TJ.job_id = %d";
  164. $job = db_fetch_object(db_query($sql, $job_id));
  165. // we do not know what the arguments are for and we want to provide a
  166. // meaningful description to the end-user. So we use a callback function
  167. // deinfed in the module that created the job to describe in an array
  168. // the arguments provided. If the callback fails then just use the
  169. // arguments as they are
  170. $args = preg_split("/::/", $job->arguments);
  171. $arg_hook = $job->modulename . "_job_describe_args";
  172. if (is_callable($arg_hook)) {
  173. $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
  174. if (is_array($new_args) and count($new_args)) {
  175. $job->arguments = $new_args;
  176. }
  177. else {
  178. $job->arguments = $args;
  179. }
  180. }
  181. else {
  182. $job->arguments = $args;
  183. }
  184. // make our start and end times more legible
  185. $job->submit_date = tripal_jobs_get_submit_date($job);
  186. $job->start_time = tripal_jobs_get_start_time($job);
  187. $job->end_time = tripal_jobs_get_end_time($job);
  188. // add the job to the variables that get exported to the template
  189. $variables['job'] = $job;
  190. }