protected function TripalDaemon::getTripalJobID

3.x TripalDaemon.inc protected TripalDaemon::getTripalJobID()

Get job_id of Tripal Job to run.

NOTE: This function should only return a job_id if we are aloud to run it.

1 call to TripalDaemon::getTripalJobID()
TripalDaemon::executeTask in tripal_daemon/TripalDaemon.inc
Implements DaemonAPIDaemon::executeTask() function.

File

tripal_daemon/TripalDaemon.inc, line 90
Implements the Tripal Daemon functionality by using the Daemon API.

Class

TripalDaemon
This is the main class for the Tripal Daemon.

Code

protected function getTripalJobID() {

  // First we need to determine if we are in sequenctial mode or parallel mode.
  // Parallel:
  if ($this->do_parallel) {

    // Check that we arn't already running the maximum number of jobs.
    if (tripal_max_jobs_exceeded($this->max_num_jobs)) {
      $this->log('Already running the maximum number of jobs.');
      return FALSE;
    }
    // Also check based on our list of running jobs just in case they haven't yet registered in the db.
    if (sizeof($this->tripal_jobs) >= $this->max_num_jobs) {
      $this->log('Already running the maximum number of jobs.');
      return FALSE;
    }
  }
  // Sequential:
  else {

    // Check that we arn't already running a job.
    if (tripal_is_job_running()) {
      $this->log('Job is still running. Waiting until it completes before starting another one.');
      return FALSE;
    }
  }

  // If we reach this point then we're aloud to run a job! :-D.
  //-----------------------------------------------------------

  // We would like to use a queue to keep track of Tripal Jobs to be run.
  // This will cut down on the number of queries and help ensure that the same job is
  // not run repeatedly with parallel processing.
  // First step, fill the queue if it's empty.
  if (empty($this->queue)) {
    $this->queue = db_query(
    "SELECT job_id FROM {tripal_jobs} TJ
         WHERE TJ.start_time IS NULL AND TJ.end_time IS NULL AND TJ.status != 'Cancelled'
         ORDER BY priority ASC, job_id ASC"
    )->fetchCol();
  }

  // If the queue is still empty then there are no jobs waiting.
  if (empty($this->queue)) {
    return FALSE;
  }

  // Return the next job in line.
  $job_id = array_shift($this->queue);

  // But only if it wasn't already run.
  if (!isset($this->tripal_jobs[$job_id])) {
    return $job_id;
  }
}