tripal_core.jobs.inc

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

File

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