jobs.inc 7.6 KB

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