tripal.jobs.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. $execute_link = '';
  156. if ($job->start_time == 0 and $job->end_time == 0) {
  157. $cancel_link = "<a href=\"" . url("admin/tripal/tripal_jobs/cancel/" . $job->job_id) . "\">Cancel</a><br />";
  158. $execute_link = "<a href=\"" . url("admin/tripal/tripal_jobs/execute/" . $job->job_id) . "\">Execute</a><br />";
  159. }
  160. $rerun_link = "<a href=\"" . url("admin/tripal/tripal_jobs/rerun/" . $job->job_id) . "\">Re-run</a><br />";
  161. $view_link ="<a href=\"" . url("admin/tripal/tripal_jobs/view/" . $job->job_id) . "\">View</a>";
  162. $rows[] = array(
  163. $job->job_id,
  164. $job->username,
  165. $job->job_name,
  166. "Submit Date: $submit<br>Start Time: $start<br>End Time: $end",
  167. $job->priority,
  168. $job->progress . '%',
  169. $job->job_status,
  170. "$execute_link $cancel_link $rerun_link $view_link",
  171. );
  172. }
  173. // the header for the jobs table
  174. $header = array(
  175. 'Job ID',
  176. 'User',
  177. 'Job Name',
  178. array('data' => 'Dates', 'style' => "white-space: nowrap"),
  179. 'Priority',
  180. 'Progress',
  181. 'Status',
  182. 'Action'
  183. );
  184. $table = array(
  185. 'header' => $header,
  186. 'rows' => $rows,
  187. 'attributes' => array('class' => 'tripal-data-table'),
  188. 'sticky' => FALSE,
  189. 'caption' => '',
  190. 'colgroups' => array(),
  191. 'empty' => 'No jobs have been submitted',
  192. );
  193. // create the report page
  194. $output = "Waiting jobs are executed first by priority level (the lower the " .
  195. "number the higher the priority) and second by the order they " .
  196. "were entered";
  197. $report_form = drupal_get_form('tripal_jobs_report_form');
  198. $output .= drupal_render($report_form);
  199. $output .= theme_table($table);
  200. $output .= theme('pager');
  201. return $output;
  202. }
  203. /**
  204. * Returns the HTML code to display a given job
  205. *
  206. * @param $job_id
  207. * The job_id of the job to display
  208. *
  209. * @return
  210. * The HTML describing the indicated job
  211. * @ingroup tripal
  212. *
  213. * @ingroup tripal_jobs
  214. */
  215. function tripal_jobs_view($job_id) {
  216. // get the job record
  217. $sql =
  218. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  219. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  220. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  221. TJ.callback,TJ.error_msg,TJ.pid
  222. FROM {tripal_jobs} TJ
  223. INNER JOIN users U on TJ.uid = U.uid
  224. WHERE TJ.job_id = :job_id";
  225. $results = db_query($sql, array(':job_id' => $job_id));
  226. $job = $results->fetchObject();
  227. // We do not know what the arguments are for and we want to provide a
  228. // meaningful description to the end-user. So we use a callback function
  229. // defined in the module that created the job to describe in an array
  230. // the arguments provided. If the callback fails then just use the
  231. // arguments as they are. Historically, job arguments were separated with
  232. // two colon. We now store them as a serialized array. So, we need to handle
  233. // both cases.
  234. if (preg_match("/::/", $job->arguments)) {
  235. $args = preg_split("/::/", $job->arguments);
  236. }
  237. else {
  238. $args = unserialize($job->arguments);
  239. }
  240. $arg_hook = $job->modulename . "_job_describe_args";
  241. if (is_callable($arg_hook)) {
  242. $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
  243. if (is_array($new_args) and count($new_args)) {
  244. $job->arguments = $new_args;
  245. }
  246. else {
  247. $job->arguments = $args;
  248. }
  249. }
  250. else {
  251. $job->arguments = $args;
  252. }
  253. // generate the list of arguments for display
  254. $arguments = '';
  255. foreach ($job->arguments as $key => $value) {
  256. if (is_array($value)) {
  257. $value = print_r($value,TRUE);
  258. }
  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. }
  301. /**
  302. * Runs a Tripal job from within the request.
  303. *
  304. * @param $job_id
  305. */
  306. function tripal_jobs_status_view($job_id) {
  307. // set the breadcrumb
  308. $breadcrumb = array();
  309. $breadcrumb[] = l('Home', '<front>');
  310. $breadcrumb[] = l('Administration', 'admin');
  311. $breadcrumb[] = l('Tripal', 'admin/tripal');
  312. $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
  313. drupal_set_breadcrumb($breadcrumb);
  314. $job = tripal_get_job($job_id);
  315. drupal_add_css(drupal_get_path ('module', 'tripal') . '/theme/css/tripal_jobs.css');
  316. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal_jobs.js');
  317. $markup = "<h2>Job Progress</h2>";
  318. $markup .= "<p>Job: " . $job->job_name . ' (' . $job->progress . '% Complete)';
  319. $markup .= '<br>Status: ' . $job->status . '</p>';
  320. $markup .= '<div id="tripal-jobs-progress-bar"><div></div></div>';
  321. $markup .= '<p>' . l('Refresh Page', 'admin/tripal/tripal_jobs/status/' . $job_id) . '</p>';
  322. drupal_add_js('var progress_percent = ' . $job->progress . ';', array('type' => 'inline'));
  323. // Reload the page every 30 seconds.
  324. $meta = array(
  325. '#tag' => 'meta',
  326. '#attributes' => array(
  327. 'http-equiv' => 'refresh',
  328. 'content' => '30',
  329. )
  330. );
  331. drupal_add_html_head($meta, 'tripal_job_status_page');
  332. $page = array(
  333. '#type' => 'markup',
  334. '#markup' => $markup,
  335. );
  336. return $page;
  337. }