TripalDaemon.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // When sorting the job list we want to use version specific SQL and thus
  38. // need to know the postgreSQL version to determine what SQL to execute.
  39. $version_string = db_query('SELECT version()')->fetchField();
  40. if (preg_match('/PostgreSQL (\d+)\.(\d+)/', $version_string, $matches)) {
  41. $version = array('major' => $matches[1], 'minor' => $matches[2]);
  42. }
  43. // If we can't determine the version then use the deprecated method.
  44. else {
  45. $version = array('major' => 8, 'minor' => 4);
  46. }
  47. // First check to see if there are any tripal jobs to be run.
  48. if ($version['major'] >= 9 ) {
  49. $waiting_jobs = db_query(
  50. "SELECT
  51. count(*) as count,
  52. array_to_string(array_agg(j.job_id ORDER BY j.priority ASC, j.job_id ASC),'|') as jobs
  53. FROM {tripal_jobs} j
  54. WHERE j.pid IS NULL AND j.end_time IS NULL"
  55. )->fetchObject();
  56. }
  57. else {
  58. $waiting_jobs = db_query(
  59. "SELECT
  60. count(*) as count,
  61. array_to_string(array_agg(j.job_id),'|') as jobs
  62. FROM (SELECT * FROM {tripal_jobs} WHERE pid IS NULL AND end_time IS NULL ORDER BY priority ASC, job_id ASC) as j"
  63. )->fetchObject();
  64. }
  65. $num_waiting_jobs = $waiting_jobs->count;
  66. $job_ids = explode('|', $waiting_jobs->jobs);
  67. // If there are then run them and log the output.
  68. if ($num_waiting_jobs > 0) {
  69. $this->log($num_waiting_jobs . ' Waiting Tripal Jobs... '
  70. . 'Running waiting job(s) now.');
  71. // Launch all tripal jobs :) Yay for bootstrapping!!
  72. foreach ($job_ids as $id) {
  73. $this->log('Starting Job (ID=' . $id . ')', '', 1);
  74. tripal_launch_job(FALSE, $id);
  75. // Report job details.
  76. $job = db_query(
  77. "SELECT j.*
  78. FROM {tripal_jobs} j
  79. WHERE j.job_id = :jid",
  80. array(':jid' => $id)
  81. )->fetchObject();
  82. $this->log("Job completed at "
  83. . date('d M Y H:i:s', $job->end_time) . " with a status of '"
  84. . $job->status . "'", "", 1);
  85. }
  86. }
  87. else {
  88. $this->log('There are no Tripal Jobs to run');
  89. }
  90. }
  91. }