tripal.jobs.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 = [];
  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 = [];
  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'] = [
  53. '#type' => 'select',
  54. '#title' => t('Filter by Job Status'),
  55. '#default_value' => $default_status,
  56. '#options' => [
  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'] = [
  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'] = [
  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 = [];
  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 = [];
  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[] = [
  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 = [
  162. 'Job ID',
  163. 'User',
  164. 'Job Name',
  165. ['data' => 'Dates', 'style' => "white-space: nowrap"],
  166. 'Priority',
  167. 'Progress',
  168. 'Status',
  169. 'Action',
  170. ];
  171. $table = [
  172. 'header' => $header,
  173. 'rows' => $rows,
  174. 'attributes' => ['class' => 'tripal-data-table'],
  175. 'sticky' => FALSE,
  176. 'caption' => '',
  177. 'colgroups' => [],
  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 = [];
  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, [':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, [$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. $items = [];
  254. $items[] = l('Return to jobs list', "admin/tripal/tripal_jobs/");
  255. $items[] = l('Re-run this job', "admin/tripal/tripal_jobs/rerun/" . $job->job_id);
  256. if ($job->start_time == 0 and $job->end_time == 0) {
  257. $items[] = l('Cancel this job', "admin/tripal/tripal_jobs/cancel/" . $job->job_id);
  258. $items[] = l('Execute this job', "admin/tripal/tripal_jobs/execute/" . $job->job_id);
  259. }
  260. $links = theme_item_list([
  261. 'items' => $items,
  262. 'title' => '',
  263. 'type' => 'ul',
  264. 'attributes' => [
  265. 'class' => ['action-links'],
  266. ],
  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 = ['Detail', 'Value'];
  274. // construct the table rows
  275. $rows[] = ['Job Description', $job->job_name];
  276. $rows[] = ['Submitting Module', $job->modulename];
  277. $rows[] = ['Callback function', $job->callback];
  278. $rows[] = ['Arguments', $arguments];
  279. $rows[] = ['Progress', $job->progress . "%"];
  280. $rows[] = ['Status', $job->job_status];
  281. $rows[] = ['Process ID', $job->pid];
  282. $rows[] = ['Submit Date', $job->submit_date];
  283. $rows[] = ['Start time', $job->start_time];
  284. $rows[] = ['End time', $job->end_time];
  285. $rows[] = ['Priority', $job->priority];
  286. $rows[] = ['Submitting User', $job->username];
  287. $table = [
  288. 'header' => $header,
  289. 'rows' => $rows,
  290. 'attributes' => ['class' => 'tripal-data-table'],
  291. 'sticky' => FALSE,
  292. 'caption' => '',
  293. 'colgroups' => [],
  294. 'empty' => '',
  295. ];
  296. $content['links'] = [
  297. '#type' => 'markup',
  298. '#markup' => $links,
  299. ];
  300. $content['job_title'] = [
  301. '#type' => 'item',
  302. '#title' => t('Job Title'),
  303. '#markup' => $job->job_name,
  304. ];
  305. $content['job_status'] = [
  306. '#type' => 'item',
  307. '#title' => t('Status'),
  308. '#markup' => $job->job_status,
  309. ];
  310. $content['details_fset'] = [
  311. '#type' => 'fieldset',
  312. '#title' => t('Job Details'),
  313. '#collapsed' => TRUE,
  314. '#collapsible' => TRUE,
  315. '#attributes' => [
  316. 'class' => ['collapsible'],
  317. ],
  318. '#attached' => [
  319. 'js' => ['misc/collapse.js', 'misc/form.js'],
  320. ],
  321. ];
  322. $content['details_fset']['job_details'] = [
  323. '#type' => 'markup',
  324. '#markup' => theme_table($table),
  325. ];
  326. $content['log_fset'] = [
  327. '#type' => 'fieldset',
  328. '#title' => t('Job Logs'),
  329. '#collapsed' => TRUE,
  330. '#collapsible' => TRUE,
  331. '#attributes' => [
  332. 'class' => ['collapsible'],
  333. ],
  334. '#attached' => [
  335. 'js' => ['misc/collapse.js', 'misc/form.js'],
  336. ],
  337. ];
  338. $content['log_fset']['job_logs'] = [
  339. '#type' => 'markup',
  340. '#markup' => '<pre class="tripal-job-logs">' . $job->error_msg . '</pre>',
  341. ];
  342. return $content;
  343. }
  344. /**
  345. * Runs a Tripal job from within the request.
  346. *
  347. * @param $job_id
  348. */
  349. function tripal_jobs_status_view($job_id) {
  350. // set the breadcrumb
  351. $breadcrumb = [];
  352. $breadcrumb[] = l('Home', '<front>');
  353. $breadcrumb[] = l('Administration', 'admin');
  354. $breadcrumb[] = l('Tripal', 'admin/tripal');
  355. $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
  356. drupal_set_breadcrumb($breadcrumb);
  357. $job = tripal_get_job($job_id);
  358. drupal_add_css(drupal_get_path('module', 'tripal') . '/theme/css/tripal_jobs.css');
  359. drupal_add_js(drupal_get_path('module', 'tripal') . '/theme/js/tripal_jobs.js');
  360. $markup = "<h2>Job Progress</h2>";
  361. $markup .= "<p>Job: " . $job->job_name . ' (' . $job->progress . '% Complete)';
  362. $markup .= '<br>Status: ' . $job->status . '</p>';
  363. $markup .= '<div id="tripal-jobs-progress-bar"><div></div></div>';
  364. $markup .= '<p>' . l('Refresh Page', 'admin/tripal/tripal_jobs/status/' . $job_id) . '</p>';
  365. drupal_add_js('var progress_percent = ' . $job->progress . ';', ['type' => 'inline']);
  366. // Reload the page every 30 seconds.
  367. $meta = [
  368. '#tag' => 'meta',
  369. '#attributes' => [
  370. 'http-equiv' => 'refresh',
  371. 'content' => '30',
  372. ],
  373. ];
  374. drupal_add_html_head($meta, 'tripal_job_status_page');
  375. $page = [
  376. '#type' => 'markup',
  377. '#markup' => $markup,
  378. ];
  379. return $page;
  380. }