You are here

public function JobExecutor::select in Apigee Edge 8

Claims a job if one is available.

Parameters

null|string $tag: Optional tag to filter with.

Return value

\Drupal\apigee_edge\Job\Job|null Job object or null if there is no available.

Overrides JobExecutorInterface::select

File

src/JobExecutor.php, line 121

Class

JobExecutor
Job executor service.

Namespace

Drupal\apigee_edge

Code

public function select(?string $tag = NULL) : ?Job {

  // TODO handle race conditions.
  $query = $this->connection
    ->select('apigee_edge_job', 'j')
    ->fields('j', [
    'job',
  ])
    ->orderBy('updated')
    ->range(0, 1);
  $query
    ->condition('status', [
    Job::IDLE,
    Job::RESCHEDULED,
  ], 'IN');
  if ($tag !== NULL) {
    $query
      ->condition('tag', $tag);
  }
  $jobdata = $query
    ->execute()
    ->fetchField();
  if ($jobdata) {

    /** @var \Drupal\apigee_edge\Job\Job $job */
    $job = unserialize($jobdata);
    $this
      ->ensure($job, Job::SELECTED);
    return $job;
  }
  return NULL;
}