TripalDaemon.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /**
  23. * Implements DaemonAPIDaemon::executeTask() function.
  24. *
  25. * This gets executed once per loop iteration & does the following:
  26. * 1. Checks to see if there are any Tripal Jobs waiting to be executed.
  27. * 2. If there are then they are run (jobs with a higher priority and higher
  28. * job_id are run first.
  29. *
  30. * This function will log how many jobs have been found and when each one was
  31. * started/completed, as well as, it's status upon completion.
  32. *
  33. * @param int $iteration_number
  34. * This is an integer stating the current iteration of the loop you are on.
  35. */
  36. protected function executeTask($iteration_number) {
  37. // First check to see if there are any tripal jobs to be run.
  38. $waiting_jobs = db_query(
  39. "SELECT
  40. count(*) as count,
  41. array_to_string(array_agg(j.job_id ORDER BY j.priority ASC, j.job_id ASC),'|') as jobs
  42. FROM {tripal_jobs} j
  43. WHERE j.pid IS NULL AND j.end_time IS NULL"
  44. )->fetchObject();
  45. $num_waiting_jobs = $waiting_jobs->count;
  46. $job_ids = explode('|', $waiting_jobs->jobs);
  47. // If there are then run them and log the output.
  48. if ($num_waiting_jobs > 0) {
  49. $this->log($num_waiting_jobs . ' Waiting Tripal Jobs... '
  50. . 'Running waiting job(s) now.');
  51. // Launch all tripal jobs :) Yay for bootstrapping!!
  52. foreach ($job_ids as $id) {
  53. $this->log('Starting Job (ID=' . $id . ')', '', 1);
  54. tripal_launch_job(FALSE, $id);
  55. // Report job details.
  56. $job = db_query(
  57. "SELECT j.*
  58. FROM {tripal_jobs} j
  59. WHERE j.job_id = :jid",
  60. array(':jid' => $id)
  61. )->fetchObject();
  62. $this->log("Job completed at "
  63. . date('d M Y H:i:s', $job->end_time) . " with a status of '"
  64. . $job->status . "'", "", 1);
  65. }
  66. }
  67. else {
  68. $this->log('There are no Tripal Jobs to run');
  69. }
  70. }
  71. }