tripal.jobs.inc 12 KB

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