tripal_core.jobs.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_core
  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_core_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>Click each of the following links to '
  36. . 'enable the pertinent views</strong>:</p>';
  37. $output .= '<ul>';
  38. $output .= '<li>'.l('Jobs View', 'admin/tripal/tripal_jobs/views/jobs/enable').'</li>';
  39. $output .= '</ul>';
  40. }
  41. return $output;
  42. }
  43. /**
  44. * NO LONGER USED: REPLACED BY VIEW
  45. *
  46. * @ingroup tripal_jobs
  47. */
  48. function tripal_jobs_report_form($form, &$form_state = NULL) {
  49. $form = array();
  50. // set the default values
  51. $default_status = '';
  52. $default_job_name = '';
  53. if (array_key_exists('values', $form_state)) {
  54. $default_status = array_key_exists('job_status', $form_state['values']) ? $form_state['values']['job_status'] : '';
  55. $default_job_name = array_key_exists('job_name', $form_state['values']) ? $form_state['values']['job_name'] : '';
  56. }
  57. if (!$default_status and array_key_exists('tripal_job_filter', $_SESSION)) {
  58. $job_status = array_key_exists('job_status', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_status'] : '';
  59. }
  60. if (!$default_job_name and array_key_exists('tripal_job_filter', $_SESSION)) {
  61. $default_job_name = array_key_exists('job_name', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_name'] : '';
  62. }
  63. $form['job_status'] = array(
  64. '#type' => 'select',
  65. '#title' => t('Filter by Job Status'),
  66. '#default_value' => $default_status,
  67. '#options' => array(
  68. 0 => 'All Jobs',
  69. 'Running' => 'Running',
  70. 'Waiting' => 'Waiting',
  71. 'Completed' => 'Completed',
  72. 'Cancelled' => 'Cancelled',
  73. 'Error' => 'Error',
  74. ),
  75. );
  76. $form['job_name'] = array(
  77. '#type' => 'textfield',
  78. '#title' => t('Filter by Job Name'),
  79. '#description' => t('The jobs will be filtered if text provided is contained in the job name'),
  80. '#default_value' => $default_job_name,
  81. );
  82. $form['submit'] = array(
  83. '#type' => 'submit',
  84. '#value' => t('Filter'),
  85. );
  86. return $form;
  87. }
  88. /**
  89. * NO LONGER USED: REPLACED BY VIEW
  90. *
  91. * @ingroup tripal_jobs
  92. */
  93. function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
  94. $job_status = $form_state['values']['job_status'];
  95. $job_name = $form_state['values']['job_name'];
  96. $_SESSION['tripal_job_filter']['job_status'] = $job_status;
  97. $_SESSION['tripal_job_filter']['job_name'] = $job_name;
  98. }
  99. /**
  100. * Returns the Tripal Job Report
  101. *
  102. * @return
  103. * The HTML to be rendered which describes the job report
  104. *
  105. * @ingroup tripal_jobs
  106. */
  107. function tripal_jobs_report() {
  108. // run the following function which will
  109. // change the status of jobs that have errored out
  110. tripal_is_job_running();
  111. $job_status = '';
  112. $job_name = '';
  113. if (array_key_exists('tripal_job_filter', $_SESSION)) {
  114. $job_status = array_key_exists('job_status', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_status'] : '';
  115. $job_name = array_key_exists('job_name', $_SESSION['tripal_job_filter']) ? $_SESSION['tripal_job_filter']['job_name'] : '';
  116. }
  117. // build the SQL for getting the jobs
  118. $sql = "
  119. SELECT
  120. TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  121. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  122. TJ.end_time,TJ.priority,U.name as username
  123. FROM {tripal_jobs} TJ
  124. INNER JOIN {users} U on TJ.uid = U.uid
  125. WHERE 1=1
  126. ";
  127. $args = array();
  128. if ($job_status) {
  129. $sql .= "AND TJ.status = :status ";
  130. $args[':status'] = $job_status;
  131. }
  132. if ($job_name) {
  133. $sql .= "AND TJ.job_name like :job_name";
  134. $args[':job_name'] = "%$job_name%";
  135. }
  136. $sql .= " ORDER BY job_id DESC ";
  137. // create the SQL that returns the total number of records
  138. $count_sql = "SELECT count(*) as total FROM ($sql) as t1";
  139. $result = db_query($count_sql, $args);
  140. $total_jobs = $result->fetchObject();
  141. // initialize the pager
  142. $num_per_page = 25;
  143. $page = pager_find_page();
  144. pager_default_initialize($total_jobs->total, $num_per_page);
  145. // get results
  146. $pager_sql = "$sql LIMIT :number OFFSET :offset";
  147. $args[':number'] = $num_per_page;
  148. $args[':offset'] = $num_per_page * $page;
  149. $jobs = db_query($pager_sql, $args);
  150. // iterate through the jobs and build the table rows
  151. $rows = array();
  152. foreach ($jobs as $job) {
  153. $submit = tripal_get_job_submit_date($job);
  154. $start = tripal_get_job_start($job);
  155. $end = tripal_get_job_end($job);
  156. $cancel_link = '';
  157. if ($job->start_time == 0 and $job->end_time == 0) {
  158. $cancel_link = "<a href=\"" . url("admin/tripal/tripal_jobs/cancel/" . $job->job_id) . "\">Cancel</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. "$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_core
  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. $arguments .= "$key: $value<br>";
  257. }
  258. // build the links
  259. $links = l('Return to jobs list', "admin/tripal/tripal_jobs/") . ' | ' .
  260. $links .= l('Re-run this job', "admin/tripal/tripal_jobs/rerun/" . $job->job_id) . ' | ';
  261. if ($job->start_time == 0 and $job->end_time == 0) {
  262. $links .= l('Cancel this job', "admin/tripal/tripal_jobs/cancel/" . $job->job_id) . ' | ';
  263. }
  264. // make our start and end times more legible
  265. $job->submit_date = tripal_get_job_submit_date($job);
  266. $job->start_time = tripal_get_job_start($job);
  267. $job->end_time = tripal_get_job_end($job);
  268. // construct the table headers
  269. $header = array('Detail', 'Value');
  270. // construct the table rows
  271. $rows[] = array('Job Description', $job->job_name);
  272. $rows[] = array('Submitting Module', $job->modulename);
  273. $rows[] = array('Callback function', $job->callback);
  274. $rows[] = array('Arguments', $arguments);
  275. $rows[] = array('Progress', $job->progress . "%");
  276. $rows[] = array('Status', $job->job_status);
  277. $rows[] = array('Process ID', $job->pid);
  278. $rows[] = array('Submit Date', $job->submit_date);
  279. $rows[] = array('Start time', $job->start_time);
  280. $rows[] = array('End time', $job->end_time);
  281. $rows[] = array('Error Message', $job->error_msg);
  282. $rows[] = array('Priority', $job->priority);
  283. $rows[] = array('Submitting User', $job->username);
  284. $table = array(
  285. 'header' => $header,
  286. 'rows' => $rows,
  287. 'attributes' => array('class' => 'tripal-data-table'),
  288. 'sticky' => FALSE,
  289. 'caption' => '',
  290. 'colgroups' => array(),
  291. 'empty' => '',
  292. );
  293. $output = '<p>' . substr($links, 0, -2) . '</p>'; // remove trailing |
  294. $output .= theme_table($table);
  295. return $output;
  296. }