You are here

class Job in Advanced Queue 8

Represents a job.

Hierarchy

  • class \Drupal\advancedqueue\Job

Expanded class hierarchy of Job

19 files declare their use of Job
AdvancedQueueCommands.php in src/Commands/AdvancedQueueCommands.php
BackendInterface.php in src/Plugin/AdvancedQueue/Backend/BackendInterface.php
Database.php in src/Plugin/AdvancedQueue/Backend/Database.php
DatabaseBackendTest.php in tests/src/Kernel/DatabaseBackendTest.php
Flexible.php in tests/modules/advancedqueue_test/src/Plugin/AdvancedQueue/JobType/Flexible.php

... See full list

File

src/Job.php, line 8

Namespace

Drupal\advancedqueue
View source
class Job {

  /**
   * Available states.
   */
  const STATE_QUEUED = 'queued';
  const STATE_PROCESSING = 'processing';
  const STATE_SUCCESS = 'success';
  const STATE_FAILURE = 'failure';

  /**
   * The job ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The queue ID.
   *
   * @var string
   */
  protected $queueId;

  /**
   * The job type.
   *
   * Represents the ID of the plugin called to process the job.
   *
   * @var string
   */
  protected $type;

  /**
   * The job payload.
   *
   * @var array
   */
  protected $payload;

  /**
   * The job state.
   *
   * @var string
   */
  protected $state = self::STATE_QUEUED;

  /**
   * The job message.
   *
   * @var array
   */
  protected $message;

  /**
   * The number of retries.
   *
   * @var int
   */
  protected $numRetries = 0;

  /**
   * The availability timestamp.
   *
   * @var int
   */
  protected $available;

  /**
   * The processing timestamp.
   *
   * @var int
   */
  protected $processed;

  /**
   * The lease expiration timestamp.
   *
   * @var int
   */
  protected $expires;

  /**
   * Constructs a new Job object.
   *
   * @param array $definition
   *   The job definition.
   */
  public function __construct(array $definition) {
    foreach ([
      'type',
      'payload',
      'state',
    ] as $required_property) {
      if (empty($definition[$required_property])) {
        throw new \InvalidArgumentException(sprintf('Missing property "%s"', $required_property));
      }
    }
    $this
      ->assertState($definition['state']);
    $this->id = !empty($definition['id']) ? $definition['id'] : '';
    $this->queueId = !empty($definition['queue_id']) ? $definition['queue_id'] : '';
    $this->type = $definition['type'];
    $this->payload = $definition['payload'];
    $this->state = $definition['state'];
    $this->message = !empty($definition['message']) ? $definition['message'] : NULL;
    $this->numRetries = !empty($definition['num_retries']) ? $definition['num_retries'] : 0;
    $this->available = !empty($definition['available']) ? (int) $definition['available'] : 0;
    $this->processed = !empty($definition['processed']) ? (int) $definition['processed'] : 0;
    $this->expires = !empty($definition['expires']) ? (int) $definition['expires'] : 0;
  }

  /**
   * Creates a new job, ready to be queued.
   *
   * @param string $type
   *   The job type.
   * @param array $payload
   *   The payload.
   *
   * @return static
   */
  public static function create($type, array $payload) {
    return new static([
      'type' => $type,
      'payload' => $payload,
      'state' => self::STATE_QUEUED,
    ]);
  }

  /**
   * Asserts that the given job state is valid.
   *
   * @param string $state
   *   The job state, a Job::STATE_ constant.
   *
   * @throws \InvalidArgumentException
   *   Thrown if the job state is invalid.
   */
  protected function assertState($state) {
    $states = [
      self::STATE_QUEUED,
      self::STATE_PROCESSING,
      self::STATE_SUCCESS,
      self::STATE_FAILURE,
    ];
    if (!in_array($state, $states)) {
      throw new \InvalidArgumentException(sprintf('Invalid state "%s" given.', $state));
    }
  }

  /**
   * Gets the job ID.
   *
   * Assigned to the job during queueing.
   *
   * @return string
   *   The job ID.
   */
  public function getId() {
    return $this->id;
  }

  /**
   * Sets the job ID.
   *
   * @param string $id
   *   The job ID.
   *
   * @return $this
   */
  public function setId($id) {
    $this->id = $id;
    return $this;
  }

  /**
   * Gets the queue ID.
   *
   * Assigned to the job during queueing.
   *
   * @return string
   *   The queue ID.
   */
  public function getQueueId() {
    return $this->queueId;
  }

  /**
   * Sets the queue ID.
   *
   * @param string $queue_id
   *   The queue ID.
   *
   * @return $this
   */
  public function setQueueId($queue_id) {
    $this->queueId = $queue_id;
    return $this;
  }

  /**
   * Gets the job type.
   *
   * @return string
   *   The job type.
   */
  public function getType() {
    return $this->type;
  }

  /**
   * Sets the job type.
   *
   * @param string $type
   *   The job type.
   *
   * @return $this
   */
  public function setType($type) {
    $this->type = $type;
    return $this;
  }

  /**
   * Gets the job payload.
   *
   * @return array
   *   The job payload.
   */
  public function getPayload() {
    return $this->payload;
  }

  /**
   * Sets the job payload.
   *
   * @param array $payload
   *   The job payload.
   *
   * @return $this
   */
  public function setPayload(array $payload) {
    $this->payload = $payload;
    return $this;
  }

  /**
   * Gets the job state.
   *
   * @return string
   *   The job state, a Job::STATE_ constant.
   */
  public function getState() {
    return $this->state;
  }

  /**
   * Sets the job state.
   *
   * @param string $state
   *   The job state, a Job::STATE_ constant.
   *
   * @return $this
   */
  public function setState($state) {
    $this
      ->assertState($state);
    $this->state = $state;
    if ($state != self::STATE_PROCESSING) {
      $this->expires = 0;
    }
    return $this;
  }

  /**
   * Gets the job message.
   *
   * Populated from the JobResult after the job has been processed.
   * Contains the error message if the job failed.
   *
   * @return string
   *   The job message.
   */
  public function getMessage() {
    return $this->message;
  }

  /**
   * Sets the job message.
   *
   * @param string $message
   *   The job message.
   *
   * @return $this
   */
  public function setMessage($message) {
    $this->message = $message;
    return $this;
  }

  /**
   * Gets the number of times this job was retried.
   *
   * @return int
   *   The number of retries.
   */
  public function getNumRetries() {
    return $this->numRetries;
  }

  /**
   * Sets the number of times this job was retried.
   *
   * @param int $num_retries
   *   The number of retries.
   *
   * @return $this
   */
  public function setNumRetries($num_retries) {
    $this->numRetries = $num_retries;
    return $this;
  }

  /**
   * Gets the availability timestamp.
   *
   * Determines when the job should become available to consumers.
   * Allows jobs to be scheduled in the future.
   *
   * @return string
   *   The availability timestamp.
   */
  public function getAvailableTime() {
    return $this->available;
  }

  /**
   * Sets the availability timestamp.
   *
   * @param int $available
   *   The availability timestamp.
   *
   * @return $this
   */
  public function setAvailableTime($available) {
    $this->available = $available;
    return $this;
  }

  /**
   * Gets the processing timestamp.
   *
   * Indicates when the job was last processed.
   *
   * @return int
   *   The processing timestamp.
   */
  public function getProcessedTime() {
    return $this->processed;
  }

  /**
   * Sets the processing timestamp.
   *
   * @param int $processed
   *   The processing timestamp.
   *
   * @return $this
   */
  public function setProcessedTime($processed) {
    $this->processed = $processed;
    return $this;
  }

  /**
   * Gets the lease expiration timestamp.
   *
   * @return int
   *   The lease expiration timestamp.
   */
  public function getExpiresTime() {
    return $this->expires;
  }

  /**
   * Sets the lease expiration timestamp.
   *
   * @param int $expires
   *   The lease expiration timestamp.
   *
   * @return $this
   */
  public function setExpiresTime($expires) {
    $this->expires = $expires;
    return $this;
  }

  /**
   * Returns the job as an array.
   *
   * @return array
   *   The job.
   */
  public function toArray() {
    return [
      'id' => $this->id,
      'queue_id' => $this->queueId,
      'type' => $this->type,
      'payload' => $this->payload,
      'state' => $this->state,
      'message' => $this->message,
      'available' => $this->available,
      'processed' => $this->processed,
      'expires' => $this->expires,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Job::$available protected property The availability timestamp.
Job::$expires protected property The lease expiration timestamp.
Job::$id protected property The job ID.
Job::$message protected property The job message.
Job::$numRetries protected property The number of retries.
Job::$payload protected property The job payload.
Job::$processed protected property The processing timestamp.
Job::$queueId protected property The queue ID.
Job::$state protected property The job state.
Job::$type protected property The job type.
Job::assertState protected function Asserts that the given job state is valid.
Job::create public static function Creates a new job, ready to be queued.
Job::getAvailableTime public function Gets the availability timestamp.
Job::getExpiresTime public function Gets the lease expiration timestamp.
Job::getId public function Gets the job ID.
Job::getMessage public function Gets the job message.
Job::getNumRetries public function Gets the number of times this job was retried.
Job::getPayload public function Gets the job payload.
Job::getProcessedTime public function Gets the processing timestamp.
Job::getQueueId public function Gets the queue ID.
Job::getState public function Gets the job state.
Job::getType public function Gets the job type.
Job::setAvailableTime public function Sets the availability timestamp.
Job::setExpiresTime public function Sets the lease expiration timestamp.
Job::setId public function Sets the job ID.
Job::setMessage public function Sets the job message.
Job::setNumRetries public function Sets the number of times this job was retried.
Job::setPayload public function Sets the job payload.
Job::setProcessedTime public function Sets the processing timestamp.
Job::setQueueId public function Sets the queue ID.
Job::setState public function Sets the job state.
Job::setType public function Sets the job type.
Job::STATE_FAILURE constant
Job::STATE_PROCESSING constant
Job::STATE_QUEUED constant Available states.
Job::STATE_SUCCESS constant
Job::toArray public function Returns the job as an array.
Job::__construct public function Constructs a new Job object.