You are here

public function Processor::processJob in Advanced Queue 8

Processes the given job.

Parameters

\Drupal\advancedqueue\Job $job: The job.

\Drupal\advancedqueue\Entity\QueueInterface $queue: The parent queue.

Return value

\Drupal\advancedqueue\JobResult The job result.

Overrides ProcessorInterface::processJob

1 call to Processor::processJob()
Processor::processQueue in src/Processor.php
Processes the given queue.

File

src/Processor.php, line 90

Class

Processor
Provides the default queue processor.

Namespace

Drupal\advancedqueue

Code

public function processJob(Job $job, QueueInterface $queue) {
  $this->eventDispatcher
    ->dispatch(AdvancedQueueEvents::PRE_PROCESS, new JobEvent($job));
  try {
    $job_type = $this->jobTypeManager
      ->createInstance($job
      ->getType());
    $result = $job_type
      ->process($job);
  } catch (\Exception $e) {
    $job_type = NULL;
    $result = JobResult::failure($e
      ->getMessage());
    watchdog_exception('cron', $e);
  }

  // Update the job with the result.
  $job
    ->setState($result
    ->getState());
  $job
    ->setMessage($result
    ->getMessage());
  $this->eventDispatcher
    ->dispatch(AdvancedQueueEvents::POST_PROCESS, new JobEvent($job));

  // Pass the job back to the backend.
  $queue_backend = $queue
    ->getBackend();
  if ($job
    ->getState() == Job::STATE_SUCCESS) {
    $queue_backend
      ->onSuccess($job);
  }
  elseif ($job
    ->getState() == Job::STATE_FAILURE && !$job_type) {

    // The job failed because of an exception, no need to retry.
    $queue_backend
      ->onFailure($job);
  }
  elseif ($job
    ->getState() == Job::STATE_FAILURE && $job_type) {
    $max_retries = !is_null($result
      ->getMaxRetries()) ? $result
      ->getMaxRetries() : $job_type
      ->getMaxRetries();
    $retry_delay = !is_null($result
      ->getRetryDelay()) ? $result
      ->getRetryDelay() : $job_type
      ->getRetryDelay();
    if ($job
      ->getNumRetries() < $max_retries) {
      $queue_backend
        ->retryJob($job, $retry_delay);
    }
    else {
      $queue_backend
        ->onFailure($job);
    }
  }
  return $result;
}