jobs.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions related to the display of Tripal jobs in a Tripal website.
  5. *
  6. */
  7. /**
  8. *
  9. * @ingroup tripal_core
  10. */
  11. function tripal_jobs_report_form($form, &$form_state = NULL) {
  12. $form = array();
  13. // set the default values
  14. $default_status = $form_state['values']['job_status'];
  15. if (!$default_status) {
  16. $default_status = $_SESSION['tripal_job_status_filter'];
  17. }
  18. $form['job_status'] = array(
  19. '#type' => 'select',
  20. '#title' => t('Filter by Job Status'),
  21. '#default_value' => $default_status,
  22. '#options' => array(
  23. 0 => 'All Jobs',
  24. 'Running' => 'Running',
  25. 'Waiting' => 'Waiting',
  26. 'Completed' => 'Completed',
  27. 'Cancelled' => 'Cancelled',
  28. 'Error' => 'Error',
  29. ),
  30. );
  31. $form['submit'] = array(
  32. '#type' => 'submit',
  33. '#value' => t('Filter'),
  34. );
  35. return $form;
  36. }
  37. /**
  38. *
  39. * @ingroup tripal_core
  40. */
  41. function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
  42. $job_status = $form_state['values']['job_status'];
  43. $_SESSION['tripal_job_status_filter'] = $job_status;
  44. }
  45. /**
  46. * Returns the Tripal Job Report
  47. *
  48. * @return
  49. * The HTML to be rendered which describes the job report
  50. *
  51. * @ingroup tripal_core
  52. */
  53. function tripal_jobs_report() {
  54. // run the following function which will
  55. // change the status of jobs that have errored out
  56. tripal_jobs_check_running();
  57. $jobs_status_filter = $_SESSION['tripal_job_status_filter'];
  58. $sql = "
  59. SELECT
  60. TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  61. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  62. TJ.end_time,TJ.priority,U.name as username
  63. FROM {tripal_jobs} TJ
  64. INNER JOIN {users} U on TJ.uid = U.uid ";
  65. if ($jobs_status_filter) {
  66. $sql .= "WHERE TJ.status = '%s' ";
  67. }
  68. $sql .= "ORDER BY job_id DESC";
  69. $jobs = pager_query($sql, 25, 0, "SELECT count(*) FROM ($sql) as t1", $jobs_status_filter);
  70. $header = array(
  71. 'Job ID',
  72. 'User',
  73. 'Job Name',
  74. array('data' => 'Dates', 'style'=> "white-space: nowrap"),
  75. 'Priority',
  76. 'Progress',
  77. 'Status',
  78. 'Action');
  79. $rows = array();
  80. // iterate through the jobs
  81. while ($job = db_fetch_object($jobs)) {
  82. $submit = tripal_jobs_get_submit_date($job);
  83. $start = tripal_jobs_get_start_time($job);
  84. $end = tripal_jobs_get_end_time($job);
  85. $cancel_link = '';
  86. if ($job->start_time == 0 and $job->end_time == 0) {
  87. $cancel_link = "<a href=\"" . url("admin/tripal/tripal_jobs/cancel/" . $job->job_id) . "\">Cancel</a><br />";
  88. }
  89. $rerun_link = "<a href=\"" . url("admin/tripal/tripal_jobs/rerun/" . $job->job_id) . "\">Re-run</a><br />";
  90. $view_link ="<a href=\"" . url("admin/tripal/tripal_jobs/view/" . $job->job_id) . "\">View</a>";
  91. $rows[] = array(
  92. $job->job_id,
  93. $job->username,
  94. $job->job_name,
  95. "Submit Date: $submit<br>Start Time: $start<br>End Time: $end",
  96. $job->priority,
  97. $job->progress . '%',
  98. $job->job_status,
  99. "$cancel_link $rerun_link $view_link",
  100. );
  101. }
  102. // create the report page
  103. $output .= "Waiting jobs are executed first by priority level (the lower the ".
  104. "number the higher the priority) and second by the order they ".
  105. "were entered";
  106. $output .= drupal_get_form('tripal_jobs_report_form');
  107. $output .= theme('table', $header, $rows);
  108. $output .= theme_pager();
  109. return $output;
  110. }
  111. /**
  112. * Returns the HTML code to display a given job
  113. *
  114. * @param $job_id
  115. * The job_id of the job to display
  116. *
  117. * @return
  118. * The HTML describing the indicated job
  119. * @ingroup tripal_core
  120. */
  121. function tripal_jobs_view($job_id) {
  122. return theme('tripal_core_job_view', $job_id);
  123. }
  124. /**
  125. * Registers variables for the tripal_core_job_view themeing function
  126. *
  127. * @param $variables
  128. * An array containing all variables supplied to this template
  129. *
  130. * @ingroup tripal_core
  131. */
  132. function tripal_core_preprocess_tripal_core_job_view(&$variables) {
  133. // get the job record
  134. $job_id = $variables['job_id'];
  135. $sql =
  136. "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
  137. TJ.status as job_status, TJ,submit_date,TJ.start_time,
  138. TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
  139. TJ.callback,TJ.error_msg,TJ.pid
  140. FROM {tripal_jobs} TJ
  141. INNER JOIN users U on TJ.uid = U.uid
  142. WHERE TJ.job_id = %d";
  143. $job = db_fetch_object(db_query($sql, $job_id));
  144. // we do not know what the arguments are for and we want to provide a
  145. // meaningful description to the end-user. So we use a callback function
  146. // deinfed in the module that created the job to describe in an array
  147. // the arguments provided. If the callback fails then just use the
  148. // arguments as they are
  149. $args = preg_split("/::/", $job->arguments);
  150. $arg_hook = $job->modulename . "_job_describe_args";
  151. if (is_callable($arg_hook)) {
  152. $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
  153. if (is_array($new_args) and count($new_args)) {
  154. $job->arguments = $new_args;
  155. }
  156. else {
  157. $job->arguments = $args;
  158. }
  159. }
  160. else {
  161. $job->arguments = $args;
  162. }
  163. // make our start and end times more legible
  164. $job->submit_date = tripal_jobs_get_submit_date($job);
  165. $job->start_time = tripal_jobs_get_start_time($job);
  166. $job->end_time = tripal_jobs_get_end_time($job);
  167. // add the job to the variables that get exported to the template
  168. $variables['job'] = $job;
  169. }