tripal.jobs.inc

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

File

tripal/includes/tripal.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. * Provides a landing page for tripal jobs admin
  8. *
  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_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>Go to '
  28. . l('Administration > Structure > Views', 'admin/structure/views')
  29. . ' and enable the view titled "Tripal Jobs (Admin)"';
  30. }
  31. return $output;
  32. }
  33. /**
  34. * NO LONGER USED: REPLACED BY VIEW
  35. *
  36. */
  37. function tripal_jobs_report_form($form, &$form_state = NULL) {
  38. $form = array();
  39. // set the default values
  40. $default_status = '';
  41. $default_job_name = '';
  42. if (array_key_exists('values', $form_state)) {
  43. $default_status = array_key_exists('job_status', $form_state['values']) ? $form_state['values']['job_status'] : '';
  44. $default_job_name = array_key_exists('job_name', $form_state['values']) ? $form_state['values']['job_name'] : '';
  45. }
  46. if (!$default_status and array_key_exists('tripal_job_filter', $_SESSION)) {
  47. $job_status = array_key_exists('job_status', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_status'] : '';
  48. }
  49. if (!$default_job_name and array_key_exists('tripal_job_filter', $_SESSION)) {
  50. $default_job_name = array_key_exists('job_name', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_name'] : '';
  51. }
  52. $form['job_status'] = array(
  53. '#type' => 'select',
  54. '#title' => t('Filter by Job Status'),
  55. '#default_value' => $default_status,
  56. '#options' => array(
  57. 0 => 'All Jobs',
  58. 'Running' => 'Running',
  59. 'Waiting' => 'Waiting',
  60. 'Completed' => 'Completed',
  61. 'Cancelled' => 'Cancelled',
  62. 'Error' => 'Error',
  63. ),
  64. );
  65. $form['job_name'] = array(
  66. '#type' => 'textfield',
  67. '#title' => t('Filter by Job Name'),
  68. '#description' => t('The jobs will be filtered if text provided is contained in the job name'),
  69. '#default_value' => $default_job_name,
  70. );
  71. $form['submit'] = array(
  72. '#type' => 'submit',
  73. '#value' => t('Filter'),
  74. );
  75. return $form;
  76. }
  77. /**
  78. * NO LONGER USED: REPLACED BY VIEW
  79. */
  80. function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
  81. $job_status = $form_state['values']['job_status'];
  82. $job_name = $form_state['values']['job_name'];
  83. $_SESSION['tripal_job_filter']['job_status'] = $job_status;
  84. $_SESSION['tripal_job_filter']['job_name'] = $job_name;
  85. }
  86. /**
  87. * Returns the Tripal Job Report
  88. *
  89. * @return
  90. * The HTML to be rendered which describes the job report
  91. */
  92. function tripal_jobs_report() {
  93. // run the following function which will
  94. // change the status of jobs that have errored out
  95. tripal_get_running_jobs();
  96. $job_status = '';
  97. $job_name = '';
  98. if (array_key_exists('tripal_job_filter', $_SESSION)) {
  99. $job_status = array_key_exists('job_status', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_status'] : '';
  100. $job_name = array_key_exists('job_name', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_name'] : '';
  101. }
  102. // build the SQL for getting the jobs
  103. $sql = "
  104. SELECT
  105. TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  106. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  107. TJ.end_time,TJ.priority,U.name as username
  108. FROM {tripal_jobs} TJ
  109. INNER JOIN {users} U on TJ.uid = U.uid
  110. WHERE 1=1
  111. ";
  112. $args = array();
  113. if ($job_status) {
  114. $sql .= "AND TJ.status = :status ";
  115. $args[':status'] = $job_status;
  116. }
  117. if ($job_name) {
  118. $sql .= "AND TJ.job_name like :job_name";
  119. $args[':job_name'] = "%$job_name%";
  120. }
  121. $sql .= " ORDER BY job_id DESC ";
  122. // create the SQL that returns the total number of records
  123. $count_sql = "SELECT count(*) as total FROM ($sql) as t1";
  124. $result = db_query($count_sql, $args);
  125. $total_jobs = $result->fetchObject();
  126. // initialize the pager
  127. $num_per_page = 25;
  128. $page = pager_find_page();
  129. pager_default_initialize($total_jobs->total, $num_per_page);
  130. // get results
  131. $pager_sql = "$sql LIMIT :number OFFSET :offset";
  132. $args[':number'] = $num_per_page;
  133. $args[':offset'] = $num_per_page * $page;
  134. $jobs = db_query($pager_sql, $args);
  135. // iterate through the jobs and build the table rows
  136. $rows = array();
  137. foreach ($jobs as $job) {
  138. $submit = tripal_get_job_submit_date($job);
  139. $start = tripal_get_job_start($job);
  140. $end = tripal_get_job_end($job);
  141. $cancel_link = '';
  142. $execute_link = '';
  143. if ($job->start_time == 0 and $job->end_time == 0) {
  144. $cancel_link = "<a href=\"" . url("admin/tripal/tripal_jobs/cancel/" . $job->job_id) . "\">Cancel</a><br />";
  145. $execute_link = "<a href=\"" . url("admin/tripal/tripal_jobs/execute/" . $job->job_id) . "\">Execute</a><br />";
  146. }
  147. $rerun_link = "<a href=\"" . url("admin/tripal/tripal_jobs/rerun/" . $job->job_id) . "\">Re-run</a><br />";
  148. $view_link ="<a href=\"" . url("admin/tripal/tripal_jobs/view/" . $job->job_id) . "\">View</a>";
  149. $rows[] = array(
  150. $job->job_id,
  151. $job->username,
  152. $job->job_name,
  153. "Submit Date: $submit<br>Start Time: $start<br>End Time: $end",
  154. $job->priority,
  155. $job->progress . '%',
  156. $job->job_status,
  157. "$execute_link $cancel_link $rerun_link $view_link",
  158. );
  159. }
  160. // the header for the jobs table
  161. $header = array(
  162. 'Job ID',
  163. 'User',
  164. 'Job Name',
  165. array('data' => 'Dates', 'style' => "white-space: nowrap"),
  166. 'Priority',
  167. 'Progress',
  168. 'Status',
  169. 'Action'
  170. );
  171. $table = array(
  172. 'header' => $header,
  173. 'rows' => $rows,
  174. 'attributes' => array('class' => 'tripal-data-table'),
  175. 'sticky' => FALSE,
  176. 'caption' => '',
  177. 'colgroups' => array(),
  178. 'empty' => 'No jobs have been submitted',
  179. );
  180. // create the report page
  181. $output = "Waiting jobs are executed first by priority level (the lower the " .
  182. "number the higher the priority) and second by the order they " .
  183. "were entered";
  184. $report_form = drupal_get_form('tripal_jobs_report_form');
  185. $output .= drupal_render($report_form);
  186. $output .= theme_table($table);
  187. $output .= theme('pager');
  188. return $output;
  189. }
  190. /**
  191. * Returns the HTML code to display a given job
  192. *
  193. * @param $job_id
  194. * The job_id of the job to display
  195. *
  196. * @return
  197. * The HTML describing the indicated job
  198. */
  199. function tripal_jobs_view($job_id) {
  200. // set the breadcrumb
  201. $breadcrumb = array();
  202. $breadcrumb[] = l('Home', '<front>');
  203. $breadcrumb[] = l('Administration', 'admin');
  204. $breadcrumb[] = l('Tripal', 'admin/tripal');
  205. $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
  206. drupal_set_breadcrumb($breadcrumb);
  207. // get the job record
  208. $sql =
  209. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  210. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  211. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  212. TJ.callback,TJ.error_msg,TJ.pid
  213. FROM {tripal_jobs} TJ
  214. INNER JOIN users U on TJ.uid = U.uid
  215. WHERE TJ.job_id = :job_id";
  216. $results = db_query($sql, array(':job_id' => $job_id));
  217. $job = $results->fetchObject();
  218. // We do not know what the arguments are for and we want to provide a
  219. // meaningful description to the end-user. So we use a callback function
  220. // defined in the module that created the job to describe in an array
  221. // the arguments provided. If the callback fails then just use the
  222. // arguments as they are. Historically, job arguments were separated with
  223. // two colon. We now store them as a serialized array. So, we need to handle
  224. // both cases.
  225. if (preg_match("/::/", $job->arguments)) {
  226. $args = preg_split("/::/", $job->arguments);
  227. }
  228. else {
  229. $args = unserialize($job->arguments);
  230. }
  231. $arg_hook = $job->modulename . "_job_describe_args";
  232. if (is_callable($arg_hook)) {
  233. $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
  234. if (is_array($new_args) and count($new_args)) {
  235. $job->arguments = $new_args;
  236. }
  237. else {
  238. $job->arguments = $args;
  239. }
  240. }
  241. else {
  242. $job->arguments = $args;
  243. }
  244. // generate the list of arguments for display
  245. $arguments = '';
  246. foreach ($job->arguments as $key => $value) {
  247. if (is_array($value)) {
  248. $value = print_r($value,TRUE);
  249. }
  250. $arguments .= "$key: $value<br>";
  251. }
  252. // build the links
  253. $links = l('Return to jobs list', "admin/tripal/tripal_jobs/") . ' | ';
  254. $links .= l('Re-run this job', "admin/tripal/tripal_jobs/rerun/" . $job->job_id) . ' | ';
  255. if ($job->start_time == 0 and $job->end_time == 0) {
  256. $links .= l('Cancel this job', "admin/tripal/tripal_jobs/cancel/" . $job->job_id) . ' | ';
  257. $links .= l('Execute this job', "admin/tripal/tripal_jobs/execute/".$job->job_id);
  258. }
  259. // make our start and end times more legible
  260. $job->submit_date = tripal_get_job_submit_date($job);
  261. $job->start_time = tripal_get_job_start($job);
  262. $job->end_time = tripal_get_job_end($job);
  263. // construct the table headers
  264. $header = array('Detail', 'Value');
  265. // construct the table rows
  266. $rows[] = array('Job Description', $job->job_name);
  267. $rows[] = array('Submitting Module', $job->modulename);
  268. $rows[] = array('Callback function', $job->callback);
  269. $rows[] = array('Arguments', $arguments);
  270. $rows[] = array('Progress', $job->progress . "%");
  271. $rows[] = array('Status', $job->job_status);
  272. $rows[] = array('Process ID', $job->pid);
  273. $rows[] = array('Submit Date', $job->submit_date);
  274. $rows[] = array('Start time', $job->start_time);
  275. $rows[] = array('End time', $job->end_time);
  276. $rows[] = array('Priority', $job->priority);
  277. $rows[] = array('Submitting User', $job->username);
  278. $table = array(
  279. 'header' => $header,
  280. 'rows' => $rows,
  281. 'attributes' => array('class' => 'tripal-data-table'),
  282. 'sticky' => FALSE,
  283. 'caption' => '',
  284. 'colgroups' => array(),
  285. 'empty' => '',
  286. );
  287. $content['links'] = array(
  288. '#type' => 'markup',
  289. '#markup' => '<p>' . substr($links, 0, -2) . '</p>',
  290. );
  291. $content['job_title'] = array(
  292. '#type' => 'item',
  293. '#title' => t('Job Title'),
  294. '#markup' => $job->job_name,
  295. );
  296. $content['job_status'] = array(
  297. '#type' => 'item',
  298. '#title' => t('Status'),
  299. '#markup' => $job->job_status,
  300. );
  301. $content['details_fset'] = array(
  302. '#type' => 'fieldset',
  303. '#title' => t('Job Details'),
  304. '#collapsed' => TRUE,
  305. '#collapsible' => TRUE,
  306. '#attributes' => array(
  307. 'class' => array('collapsible', 'collapsed'),
  308. ),
  309. '#attached' => array(
  310. 'js' => array('misc/collapse.js', 'misc/form.js')
  311. ),
  312. );
  313. $content['details_fset']['job_details'] = array(
  314. '#type' => 'markup',
  315. '#markup' => theme_table($table),
  316. );
  317. $content['log_fset'] = array(
  318. '#type' => 'fieldset',
  319. '#title' => t('Job Logs'),
  320. '#collapsed' => TRUE,
  321. '#collapsible' => TRUE,
  322. '#attributes' => array(
  323. 'class' => array('collapsible', 'collapsed'),
  324. ),
  325. '#attached' => array(
  326. 'js' => array('misc/collapse.js', 'misc/form.js')
  327. ),
  328. );
  329. $content['log_fset']['job_logs'] = array(
  330. '#type' => 'markup',
  331. '#markup' => '<pre>' . $job->error_msg . '</pre>',
  332. );
  333. return $content;
  334. }
  335. /**
  336. * Runs a Tripal job from within the request.
  337. *
  338. * @param $job_id
  339. */
  340. function tripal_jobs_status_view($job_id) {
  341. // set the breadcrumb
  342. $breadcrumb = array();
  343. $breadcrumb[] = l('Home', '<front>');
  344. $breadcrumb[] = l('Administration', 'admin');
  345. $breadcrumb[] = l('Tripal', 'admin/tripal');
  346. $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
  347. drupal_set_breadcrumb($breadcrumb);
  348. $job = tripal_get_job($job_id);
  349. drupal_add_css(drupal_get_path ('module', 'tripal') . '/theme/css/tripal_jobs.css');
  350. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal_jobs.js');
  351. $markup = "<h2>Job Progress</h2>";
  352. $markup .= "<p>Job: " . $job->job_name . ' (' . $job->progress . '% Complete)';
  353. $markup .= '<br>Status: ' . $job->status . '</p>';
  354. $markup .= '<div id="tripal-jobs-progress-bar"><div></div></div>';
  355. $markup .= '<p>' . l('Refresh Page', 'admin/tripal/tripal_jobs/status/' . $job_id) . '</p>';
  356. drupal_add_js('var progress_percent = ' . $job->progress . ';', array('type' => 'inline'));
  357. // Reload the page every 30 seconds.
  358. $meta = array(
  359. '#tag' => 'meta',
  360. '#attributes' => array(
  361. 'http-equiv' => 'refresh',
  362. 'content' => '30',
  363. )
  364. );
  365. drupal_add_html_head($meta, 'tripal_job_status_page');
  366. $page = array(
  367. '#type' => 'markup',
  368. '#markup' => $markup,
  369. );
  370. return $page;
  371. }