jobs.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // get the job record
  176. $sql =
  177. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  178. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  179. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  180. TJ.callback,TJ.error_msg,TJ.pid
  181. FROM {tripal_jobs} TJ
  182. INNER JOIN users U on TJ.uid = U.uid
  183. WHERE TJ.job_id = :job_id";
  184. $results = db_query($sql, array(':job_id' => $job_id));
  185. $job = $results->fetchObject();
  186. // we do not know what the arguments are for and we want to provide a
  187. // meaningful description to the end-user. So we use a callback function
  188. // deinfed in the module that created the job to describe in an array
  189. // the arguments provided. If the callback fails then just use the
  190. // arguments as they are
  191. $args = preg_split("/::/", $job->arguments);
  192. $arg_hook = $job->modulename . "_job_describe_args";
  193. if (is_callable($arg_hook)) {
  194. $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
  195. if (is_array($new_args) and count($new_args)) {
  196. $job->arguments = $new_args;
  197. }
  198. else {
  199. $job->arguments = $args;
  200. }
  201. }
  202. else {
  203. $job->arguments = $args;
  204. }
  205. // generate the list of arguments for display
  206. $arguments = '';
  207. foreach ($job->arguments as $key => $value) {
  208. $arguments .= "$key: $value<br>";
  209. }
  210. // build the links
  211. $links = l('Return to jobs list', "admin/tripal/tripal_jobs/") . ' | ' .
  212. $links .= l('Re-run this job', "admin/tripal/tripal_jobs/rerun/" . $job->job_id) . ' | ';
  213. if ($job->start_time == 0 and $job->end_time == 0) {
  214. $links .= l('Cancel this job', "admin/tripal/tripal_jobs/cancel/" . $job->job_id) . ' | ';
  215. }
  216. // make our start and end times more legible
  217. $job->submit_date = tripal_jobs_get_submit_date($job);
  218. $job->start_time = tripal_jobs_get_start_time($job);
  219. $job->end_time = tripal_jobs_get_end_time($job);
  220. // construct the table headers
  221. $header = array('Detail', 'Value');
  222. // construct the table rows
  223. $rows[] = array('Job Description', $job->job_name);
  224. $rows[] = array('Submitting Module', $job->modulename);
  225. $rows[] = array('Callback function', $job->callback);
  226. $rows[] = array('Arguments', $arguments);
  227. $rows[] = array('Progress', $job->progress . "%");
  228. $rows[] = array('Status', $job->job_status);
  229. $rows[] = array('Process ID', $job->pid);
  230. $rows[] = array('Submit Date', $job->submit_date);
  231. $rows[] = array('Start time', $job->start_time);
  232. $rows[] = array('End time', $job->end_time);
  233. $rows[] = array('Error Message', $job->error_msg);
  234. $rows[] = array('Priority', $job->priority);
  235. $rows[] = array('Submitting User', $job->username);
  236. $table = array(
  237. 'header' => $header,
  238. 'rows' => $rows,
  239. 'attributes' => array(),
  240. 'sticky' => FALSE,
  241. 'caption' => '',
  242. 'colgroups' => array(),
  243. 'empty' => '',
  244. );
  245. $output = '<p>' . substr($links, 0, -2) . '</p>'; // remove trailing |
  246. $output .= theme_table($table);
  247. return $output;
  248. }