TripalDaemon.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Implements the Tripal Daemon functionality by using the Daemon API.
  5. */
  6. /**
  7. * This is the main class for the Tripal Daemon.
  8. *
  9. * It extends the DaemonAPIDaemon class provided by the Daemon API in order
  10. * to implement tripal job checking and execution functionality.
  11. */
  12. class TripalDaemon extends DrushDaemon {
  13. // OPTIONAL: Set how often in seconds your executeTask() should be called.
  14. // Keep in mind that this time does not include the amount of time spent
  15. // executing your tasks. For example, if you set this to 5 seconds and you
  16. // have 2 tasks in your execute_tasks() function, each of which take 15
  17. // seconds, then your loop will iterate (and thus your execute_task()
  18. // function will be called again) before your tasks finish.
  19. // CODING STANDARDS: Can't change this variable to lowerCamel since it
  20. // inherits from a library class.
  21. protected $loop_interval = 20;
  22. // Keep track of whether we are running a Tripal job or not
  23. // and if so, which Tripal jobs are we running.
  24. // If this array is empty then we are waiting for jobs ;-).
  25. protected $tripal_jobs = array();
  26. /**
  27. * Implements DaemonAPIDaemon::executeTask() function.
  28. *
  29. * This gets executed once per loop iteration & does the following:
  30. * 1. Checks to see if there are any Tripal Jobs waiting to be executed.
  31. * 2. If there are then they are run (jobs with a higher priority and higher
  32. * job_id are run first.
  33. *
  34. * This function will log how many jobs have been found and when each one was
  35. * started/completed, as well as, it's status upon completion.
  36. *
  37. * @param int $iteration_number
  38. * This is an integer stating the current iteration of the loop you are on.
  39. */
  40. protected function executeTask($iteration_number) {
  41. // When sorting the job list we want to use version specific SQL and thus
  42. // need to know the postgreSQL version to determine what SQL to execute.
  43. $version_string = db_query('SELECT version()')->fetchField();
  44. if (preg_match('/PostgreSQL (\d+)\.(\d+)/', $version_string, $matches)) {
  45. $version = array('major' => $matches[1], 'minor' => $matches[2]);
  46. }
  47. // If we can't determine the version then use the deprecated method.
  48. else {
  49. $version = array('major' => 8, 'minor' => 4);
  50. }
  51. // First check to see if there are any tripal jobs to be run.
  52. if ($version['major'] >= 9 ) {
  53. $waiting_jobs = db_query(
  54. "SELECT
  55. count(*) as count,
  56. array_to_string(array_agg(j.job_id ORDER BY j.priority ASC, j.job_id ASC),'|') as jobs
  57. FROM {tripal_jobs} j
  58. WHERE j.status = 'Waiting'"
  59. )->fetchObject();
  60. }
  61. else {
  62. $waiting_jobs = db_query(
  63. "SELECT
  64. count(*) as count,
  65. array_to_string(array_agg(j.job_id),'|') as jobs
  66. FROM (SELECT * FROM {tripal_jobs} WHERE j.status = 'Waiting' ORDER BY priority ASC, job_id ASC) as j"
  67. )->fetchObject();
  68. }
  69. $num_waiting_jobs = $waiting_jobs->count;
  70. $job_ids = explode('|', $waiting_jobs->jobs);
  71. // If there are then run them and log the output.
  72. if ($num_waiting_jobs > 0) {
  73. $this->log($num_waiting_jobs . ' Waiting Tripal Jobs... '
  74. . 'Running waiting job(s) now.');
  75. // Launch all tripal jobs :) Yay for bootstrapping!!
  76. foreach ($job_ids as $id) {
  77. $this->log('Starting Job (ID=' . $id . ')', '', 1);
  78. // We would like to log the output from the job.
  79. // However, most tripal jobs simply print to the screen :-(
  80. // Thus we have to use output buffering to capture the output.
  81. // Start Buffering.
  82. ob_start();
  83. // Tell admins we are running a job.
  84. $this->tripal_jobs[$id] = $id;
  85. $this->setStatus();
  86. // Launch Tripal Job.
  87. tripal_launch_job(FALSE, $id);
  88. // Tell admins that we're done :-).
  89. unset($this->tripal_jobs[$id]);
  90. $this->setStatus();
  91. // Save the buffer to the log and stop buffering.
  92. $this->log(str_repeat('=', 80));
  93. $this->log(ob_get_clean());
  94. $this->log(str_repeat('=', 80));
  95. // Report job details.
  96. $job = db_query(
  97. "SELECT j.*
  98. FROM {tripal_jobs} j
  99. WHERE j.job_id = :jid",
  100. array(':jid' => $id)
  101. )->fetchObject();
  102. $this->log("Job completed at "
  103. . date('d M Y H:i:s', $job->end_time) . " with a status of '"
  104. . $job->status . "'", "", 1);
  105. }
  106. }
  107. else {
  108. $this->log('There are no Tripal Jobs to run');
  109. }
  110. }
  111. /**
  112. * Override to include additional daemon-specific settings for use in reports.
  113. *
  114. * @return array
  115. * An array of status details where the key should be a human-readable
  116. * name for your detail and the value should be the current state.
  117. */
  118. protected function getStatusDetails() {
  119. $status_details = parent::getStatusDetails();
  120. $status_details['Running Job'] = (empty($this->tripal_jobs)) ? FALSE : TRUE;
  121. $status_details['Current Jobs'] = $this->tripal_jobs;
  122. return $status_details;
  123. }
  124. }