jobs.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions related to the display of Tripal jobs in a Tripal website.
  5. *
  6. */
  7. /**
  8. * Provides a landing page for tripal jobs admin
  9. */
  10. function tripal_jobs_admin_view() {
  11. $output = '';
  12. // set the breadcrumb
  13. $breadcrumb = array();
  14. $breadcrumb[] = l('Home', '<front>');
  15. $breadcrumb[] = l('Administration', 'admin');
  16. $breadcrumb[] = l('Tripal', 'admin/tripal');
  17. $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
  18. drupal_set_breadcrumb($breadcrumb);
  19. // Add the view
  20. $view = views_embed_view('tripal_core_admin_jobs','default');
  21. if (isset($view)) {
  22. $output .= $view;
  23. }
  24. else {
  25. $output .= '<p>The Tripal Jobs management system uses primarily views to provide an '
  26. . 'administrative interface. Currently one or more views needed for this '
  27. . 'administrative interface are disabled. <strong>Click each of the following links to '
  28. . 'enable the pertinent views</strong>:</p>';
  29. $output .= '<ul>';
  30. $output .= '<li>'.l('Jobs View', 'admin/tripal/tripal_jobs/views/jobs/enable').'</li>';
  31. $output .= '</ul>';
  32. }
  33. return $output;
  34. }
  35. /**
  36. * NO LONGER USED: REPLACED BY VIEW
  37. * @ingroup tripal_core
  38. */
  39. function tripal_jobs_report_form($form, &$form_state = NULL) {
  40. $form = array();
  41. // set the default values
  42. $default_status = '';
  43. $default_job_name = '';
  44. if (array_key_exists('values', $form_state)) {
  45. $default_status = array_key_exists('job_status', $form_state['values']) ? $form_state['values']['job_status'] : '';
  46. $default_job_name = array_key_exists('job_name', $form_state['values']) ? $form_state['values']['job_name'] : '';
  47. }
  48. if (!$default_status and array_key_exists('tripal_job_filter', $_SESSION)) {
  49. $job_status = array_key_exists('job_status', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_status'] : '';
  50. }
  51. if (!$default_job_name and array_key_exists('tripal_job_filter', $_SESSION)) {
  52. $default_job_name = array_key_exists('job_name', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_name'] : '';
  53. }
  54. $form['job_status'] = array(
  55. '#type' => 'select',
  56. '#title' => t('Filter by Job Status'),
  57. '#default_value' => $default_status,
  58. '#options' => array(
  59. 0 => 'All Jobs',
  60. 'Running' => 'Running',
  61. 'Waiting' => 'Waiting',
  62. 'Completed' => 'Completed',
  63. 'Cancelled' => 'Cancelled',
  64. 'Error' => 'Error',
  65. ),
  66. );
  67. $form['job_name'] = array(
  68. '#type' => 'textfield',
  69. '#title' => t('Filter by Job Name'),
  70. '#description' => t('The jobs will be filtered if text provided is contained in the job name'),
  71. '#default_value' => $default_job_name,
  72. );
  73. $form['submit'] = array(
  74. '#type' => 'submit',
  75. '#value' => t('Filter'),
  76. );
  77. return $form;
  78. }
  79. /**
  80. *
  81. * @ingroup tripal_core
  82. */
  83. function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
  84. $job_status = $form_state['values']['job_status'];
  85. $job_name = $form_state['values']['job_name'];
  86. $_SESSION['tripal_job_filter']['job_status'] = $job_status;
  87. $_SESSION['tripal_job_filter']['job_name'] = $job_name;
  88. }
  89. /**
  90. * Returns the Tripal Job Report
  91. *
  92. * @return
  93. * The HTML to be rendered which describes the job report
  94. *
  95. * @ingroup tripal_core
  96. */
  97. function tripal_jobs_report() {
  98. // run the following function which will
  99. // change the status of jobs that have errored out
  100. tripal_jobs_check_running();
  101. $job_status = '';
  102. $job_name = '';
  103. if (array_key_exists('tripal_job_filter', $_SESSION)) {
  104. $job_status = array_key_exists('job_status', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_status'] : '';
  105. $job_name = array_key_exists('job_name', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_name'] : '';
  106. }
  107. // build the SQL for getting the jobs
  108. $sql = "
  109. SELECT
  110. TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  111. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  112. TJ.end_time,TJ.priority,U.name as username
  113. FROM {tripal_jobs} TJ
  114. INNER JOIN {users} U on TJ.uid = U.uid
  115. WHERE 1=1
  116. ";
  117. $args = array();
  118. if ($job_status) {
  119. $sql .= "AND TJ.status = :status ";
  120. $args[':status'] = $job_status;
  121. }
  122. if ($job_name) {
  123. $sql .= "AND TJ.job_name like :job_name";
  124. $args[':job_name'] = "%$job_name%";
  125. }
  126. $sql .= " ORDER BY job_id DESC ";
  127. // create the SQL that returns the total number of records
  128. $count_sql = "SELECT count(*) as total FROM ($sql) as t1";
  129. $result = db_query($count_sql, $args);
  130. $total_jobs = $result->fetchObject();
  131. // initialize the pager
  132. $num_per_page = 25;
  133. $page = pager_find_page();
  134. pager_default_initialize($total_jobs->total, $num_per_page);
  135. // get results
  136. $pager_sql = "$sql LIMIT :number OFFSET :offset";
  137. $args[':number'] = $num_per_page;
  138. $args[':offset'] = $num_per_page * $page;
  139. $jobs = db_query($pager_sql, $args);
  140. // iterate through the jobs and build the table rows
  141. $rows = array();
  142. foreach ($jobs as $job) {
  143. $submit = tripal_jobs_get_submit_date($job);
  144. $start = tripal_jobs_get_start_time($job);
  145. $end = tripal_jobs_get_end_time($job);
  146. $cancel_link = '';
  147. if ($job->start_time == 0 and $job->end_time == 0) {
  148. $cancel_link = "<a href=\"" . url("admin/tripal/tripal_jobs/cancel/" . $job->job_id) . "\">Cancel</a><br />";
  149. }
  150. $rerun_link = "<a href=\"" . url("admin/tripal/tripal_jobs/rerun/" . $job->job_id) . "\">Re-run</a><br />";
  151. $view_link ="<a href=\"" . url("admin/tripal/tripal_jobs/view/" . $job->job_id) . "\">View</a>";
  152. $rows[] = array(
  153. $job->job_id,
  154. $job->username,
  155. $job->job_name,
  156. "Submit Date: $submit<br>Start Time: $start<br>End Time: $end",
  157. $job->priority,
  158. $job->progress . '%',
  159. $job->job_status,
  160. "$cancel_link $rerun_link $view_link",
  161. );
  162. }
  163. // the header for the jobs table
  164. $header = array(
  165. 'Job ID',
  166. 'User',
  167. 'Job Name',
  168. array('data' => 'Dates', 'style' => "white-space: nowrap"),
  169. 'Priority',
  170. 'Progress',
  171. 'Status',
  172. 'Action'
  173. );
  174. $table = array(
  175. 'header' => $header,
  176. 'rows' => $rows,
  177. 'attributes' => array(),
  178. 'sticky' => FALSE,
  179. 'caption' => '',
  180. 'colgroups' => array(),
  181. 'empty' => 'No jobs have been submitted',
  182. );
  183. // create the report page
  184. $output = "Waiting jobs are executed first by priority level (the lower the " .
  185. "number the higher the priority) and second by the order they " .
  186. "were entered";
  187. $report_form = drupal_get_form('tripal_jobs_report_form');
  188. $output .= drupal_render($report_form);
  189. $output .= theme_table($table);
  190. $output .= theme('pager');
  191. return $output;
  192. }
  193. /**
  194. * Returns the HTML code to display a given job
  195. *
  196. * @param $job_id
  197. * The job_id of the job to display
  198. *
  199. * @return
  200. * The HTML describing the indicated job
  201. * @ingroup tripal_core
  202. */
  203. function tripal_jobs_view($job_id) {
  204. // get the job record
  205. $sql =
  206. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  207. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  208. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  209. TJ.callback,TJ.error_msg,TJ.pid
  210. FROM {tripal_jobs} TJ
  211. INNER JOIN users U on TJ.uid = U.uid
  212. WHERE TJ.job_id = :job_id";
  213. $results = db_query($sql, array(':job_id' => $job_id));
  214. $job = $results->fetchObject();
  215. // we do not know what the arguments are for and we want to provide a
  216. // meaningful description to the end-user. So we use a callback function
  217. // deinfed in the module that created the job to describe in an array
  218. // the arguments provided. If the callback fails then just use the
  219. // arguments as they are
  220. $args = preg_split("/::/", $job->arguments);
  221. $arg_hook = $job->modulename . "_job_describe_args";
  222. if (is_callable($arg_hook)) {
  223. $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
  224. if (is_array($new_args) and count($new_args)) {
  225. $job->arguments = $new_args;
  226. }
  227. else {
  228. $job->arguments = $args;
  229. }
  230. }
  231. else {
  232. $job->arguments = $args;
  233. }
  234. // generate the list of arguments for display
  235. $arguments = '';
  236. foreach ($job->arguments as $key => $value) {
  237. $arguments .= "$key: $value<br>";
  238. }
  239. // build the links
  240. $links = l('Return to jobs list', "admin/tripal/tripal_jobs/") . ' | ' .
  241. $links .= l('Re-run this job', "admin/tripal/tripal_jobs/rerun/" . $job->job_id) . ' | ';
  242. if ($job->start_time == 0 and $job->end_time == 0) {
  243. $links .= l('Cancel this job', "admin/tripal/tripal_jobs/cancel/" . $job->job_id) . ' | ';
  244. }
  245. // make our start and end times more legible
  246. $job->submit_date = tripal_jobs_get_submit_date($job);
  247. $job->start_time = tripal_jobs_get_start_time($job);
  248. $job->end_time = tripal_jobs_get_end_time($job);
  249. // construct the table headers
  250. $header = array('Detail', 'Value');
  251. // construct the table rows
  252. $rows[] = array('Job Description', $job->job_name);
  253. $rows[] = array('Submitting Module', $job->modulename);
  254. $rows[] = array('Callback function', $job->callback);
  255. $rows[] = array('Arguments', $arguments);
  256. $rows[] = array('Progress', $job->progress . "%");
  257. $rows[] = array('Status', $job->job_status);
  258. $rows[] = array('Process ID', $job->pid);
  259. $rows[] = array('Submit Date', $job->submit_date);
  260. $rows[] = array('Start time', $job->start_time);
  261. $rows[] = array('End time', $job->end_time);
  262. $rows[] = array('Error Message', $job->error_msg);
  263. $rows[] = array('Priority', $job->priority);
  264. $rows[] = array('Submitting User', $job->username);
  265. $table = array(
  266. 'header' => $header,
  267. 'rows' => $rows,
  268. 'attributes' => array(),
  269. 'sticky' => FALSE,
  270. 'caption' => '',
  271. 'colgroups' => array(),
  272. 'empty' => '',
  273. );
  274. $output = '<p>' . substr($links, 0, -2) . '</p>'; // remove trailing |
  275. $output .= theme_table($table);
  276. return $output;
  277. }