tripal.jobs.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. // set the breadcrumb
  217. $breadcrumb = array();
  218. $breadcrumb[] = l('Home', '<front>');
  219. $breadcrumb[] = l('Administration', 'admin');
  220. $breadcrumb[] = l('Tripal', 'admin/tripal');
  221. $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
  222. drupal_set_breadcrumb($breadcrumb);
  223. // get the job record
  224. $sql =
  225. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  226. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  227. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  228. TJ.callback,TJ.error_msg,TJ.pid
  229. FROM {tripal_jobs} TJ
  230. INNER JOIN users U on TJ.uid = U.uid
  231. WHERE TJ.job_id = :job_id";
  232. $results = db_query($sql, array(':job_id' => $job_id));
  233. $job = $results->fetchObject();
  234. // We do not know what the arguments are for and we want to provide a
  235. // meaningful description to the end-user. So we use a callback function
  236. // defined in the module that created the job to describe in an array
  237. // the arguments provided. If the callback fails then just use the
  238. // arguments as they are. Historically, job arguments were separated with
  239. // two colon. We now store them as a serialized array. So, we need to handle
  240. // both cases.
  241. if (preg_match("/::/", $job->arguments)) {
  242. $args = preg_split("/::/", $job->arguments);
  243. }
  244. else {
  245. $args = unserialize($job->arguments);
  246. }
  247. $arg_hook = $job->modulename . "_job_describe_args";
  248. if (is_callable($arg_hook)) {
  249. $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
  250. if (is_array($new_args) and count($new_args)) {
  251. $job->arguments = $new_args;
  252. }
  253. else {
  254. $job->arguments = $args;
  255. }
  256. }
  257. else {
  258. $job->arguments = $args;
  259. }
  260. // generate the list of arguments for display
  261. $arguments = '';
  262. foreach ($job->arguments as $key => $value) {
  263. if (is_array($value)) {
  264. $value = print_r($value,TRUE);
  265. }
  266. $arguments .= "$key: $value<br>";
  267. }
  268. // build the links
  269. $links = l('Return to jobs list', "admin/tripal/tripal_jobs/") . ' | ';
  270. $links .= l('Re-run this job', "admin/tripal/tripal_jobs/rerun/" . $job->job_id) . ' | ';
  271. if ($job->start_time == 0 and $job->end_time == 0) {
  272. $links .= l('Cancel this job', "admin/tripal/tripal_jobs/cancel/" . $job->job_id) . ' | ';
  273. $links .= l('Execute this job', "admin/tripal/tripal_jobs/execute/".$job->job_id);
  274. }
  275. // make our start and end times more legible
  276. $job->submit_date = tripal_get_job_submit_date($job);
  277. $job->start_time = tripal_get_job_start($job);
  278. $job->end_time = tripal_get_job_end($job);
  279. // construct the table headers
  280. $header = array('Detail', 'Value');
  281. // construct the table rows
  282. $rows[] = array('Job Description', $job->job_name);
  283. $rows[] = array('Submitting Module', $job->modulename);
  284. $rows[] = array('Callback function', $job->callback);
  285. $rows[] = array('Arguments', $arguments);
  286. $rows[] = array('Progress', $job->progress . "%");
  287. $rows[] = array('Status', $job->job_status);
  288. $rows[] = array('Process ID', $job->pid);
  289. $rows[] = array('Submit Date', $job->submit_date);
  290. $rows[] = array('Start time', $job->start_time);
  291. $rows[] = array('End time', $job->end_time);
  292. $rows[] = array('Priority', $job->priority);
  293. $rows[] = array('Submitting User', $job->username);
  294. $table = array(
  295. 'header' => $header,
  296. 'rows' => $rows,
  297. 'attributes' => array('class' => 'tripal-data-table'),
  298. 'sticky' => FALSE,
  299. 'caption' => '',
  300. 'colgroups' => array(),
  301. 'empty' => '',
  302. );
  303. $content['links'] = array(
  304. '#type' => 'markup',
  305. '#markup' => '<p>' . substr($links, 0, -2) . '</p>',
  306. );
  307. $content['job_title'] = array(
  308. '#type' => 'item',
  309. '#title' => t('Job Title'),
  310. '#markup' => $job->job_name,
  311. );
  312. $content['job_status'] = array(
  313. '#type' => 'item',
  314. '#title' => t('Status'),
  315. '#markup' => $job->job_status,
  316. );
  317. $content['details_fset'] = array(
  318. '#type' => 'fieldset',
  319. '#title' => t('Job Details'),
  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['details_fset']['job_details'] = array(
  330. '#type' => 'markup',
  331. '#markup' => theme_table($table),
  332. );
  333. $content['log_fset'] = array(
  334. '#type' => 'fieldset',
  335. '#title' => t('Job Logs'),
  336. '#collapsed' => TRUE,
  337. '#collapsible' => TRUE,
  338. '#attributes' => array(
  339. 'class' => array('collapsible', 'collapsed'),
  340. ),
  341. '#attached' => array(
  342. 'js' => array('misc/collapse.js', 'misc/form.js')
  343. ),
  344. );
  345. $content['log_fset']['job_logs'] = array(
  346. '#type' => 'markup',
  347. '#markup' => '<pre>' . $job->error_msg . '</pre>',
  348. );
  349. return $content;
  350. }
  351. /**
  352. * Runs a Tripal job from within the request.
  353. *
  354. * @param $job_id
  355. */
  356. function tripal_jobs_status_view($job_id) {
  357. // set the breadcrumb
  358. $breadcrumb = array();
  359. $breadcrumb[] = l('Home', '<front>');
  360. $breadcrumb[] = l('Administration', 'admin');
  361. $breadcrumb[] = l('Tripal', 'admin/tripal');
  362. $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
  363. drupal_set_breadcrumb($breadcrumb);
  364. $job = tripal_get_job($job_id);
  365. drupal_add_css(drupal_get_path ('module', 'tripal') . '/theme/css/tripal_jobs.css');
  366. drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal_jobs.js');
  367. $markup = "<h2>Job Progress</h2>";
  368. $markup .= "<p>Job: " . $job->job_name . ' (' . $job->progress . '% Complete)';
  369. $markup .= '<br>Status: ' . $job->status . '</p>';
  370. $markup .= '<div id="tripal-jobs-progress-bar"><div></div></div>';
  371. $markup .= '<p>' . l('Refresh Page', 'admin/tripal/tripal_jobs/status/' . $job_id) . '</p>';
  372. drupal_add_js('var progress_percent = ' . $job->progress . ';', array('type' => 'inline'));
  373. // Reload the page every 30 seconds.
  374. $meta = array(
  375. '#tag' => 'meta',
  376. '#attributes' => array(
  377. 'http-equiv' => 'refresh',
  378. 'content' => '30',
  379. )
  380. );
  381. drupal_add_html_head($meta, 'tripal_job_status_page');
  382. $page = array(
  383. '#type' => 'markup',
  384. '#markup' => $markup,
  385. );
  386. return $page;
  387. }