TripalDaemon.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $do_parallel = FALSE;
  42. $max_jobs = 1;
  43. // First check if any jobs are currently running if they are, don't continue,
  44. // we don't want to have more than one job script running at a time.
  45. if (!$do_parallel and tripal_is_job_running()) {
  46. $this->log("Jobs are still running. Use the --parallel=1 option with the Drush command to run jobs in parallel.\n");
  47. }
  48. elseif ($do_parallel && tripal_max_jobs_exceeded($max_jobs)) {
  49. $this->log("At least $max_jobs jobs are still running. At least one of these jobs much complete before a new job can start.\n");
  50. }
  51. else {
  52. // First check to see if there are any tripal jobs to be run.
  53. $sql = "
  54. SELECT TJ.job_id
  55. FROM {tripal_jobs} TJ
  56. WHERE
  57. TJ.start_time IS NULL AND
  58. TJ.end_time IS NULL AND
  59. NOT TJ.status = 'Cancelled'
  60. ORDER BY priority ASC,job_id ASC";
  61. $job_ids = db_query($sql)->fetchCol();
  62. $num_waiting_jobs = sizeof($job_ids);
  63. // If there are then run them and log the output.
  64. if ($num_waiting_jobs > 0) {
  65. $this->log($num_waiting_jobs . ' Waiting Tripal Jobs... '
  66. . 'Running waiting job(s) now.');
  67. // Launch all tripal jobs :) Yay for bootstrapping!!
  68. foreach ($job_ids as $id) {
  69. $this->log('Starting Job (ID=' . $id . ')', '', 1);
  70. // Tell admins we are running a job.
  71. $this->tripal_jobs[$id] = $id;
  72. $this->setStatus();
  73. // Launch Tripal Job.
  74. $job = new TripalJob();
  75. $job->load($id);
  76. try {
  77. $job->run();
  78. }
  79. catch (Exception $e) {
  80. $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
  81. }
  82. // Tell admins that we're done :-).
  83. unset($this->tripal_jobs[$id]);
  84. $this->setStatus();
  85. // Report job details.
  86. $job = db_query(
  87. "SELECT j.*
  88. FROM {tripal_jobs} j
  89. WHERE j.job_id = :jid",
  90. array(':jid' => $id)
  91. )->fetchObject();
  92. $this->log("Job (ID=" . $id . ") completed at "
  93. . date('d M Y H:i:s', $job->end_time) . " with a status of '"
  94. . $job->status . "'", "", 1);
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * Override to include additional daemon-specific settings for use in reports.
  101. *
  102. * @return array
  103. * An array of status details where the key should be a human-readable
  104. * name for your detail and the value should be the current state.
  105. */
  106. protected function getStatusDetails() {
  107. $status_details = parent::getStatusDetails();
  108. $status_details['Running Job'] = (empty($this->tripal_jobs)) ? FALSE : TRUE;
  109. $status_details['Current Jobs'] = $this->tripal_jobs;
  110. return $status_details;
  111. }
  112. }