You are here

abstract class QueueWorkerLockedBase in TMGMT Extension Suite 8.3

Class QueueWorkerLockedBase.

Base class for queue workers that need to guarantee that only one instance of queue worker processes queue at a time.

@package Drupal\tmgmt_extension_suit\Plugin\QueueWorker

Hierarchy

Expanded class hierarchy of QueueWorkerLockedBase

File

src/Plugin/QueueWorker/QueueWorkerLockedBase.php, line 20

Namespace

Drupal\tmgmt_extension_suit\Plugin\QueueWorker
View source
abstract class QueueWorkerLockedBase extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * Logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Lock service.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

  /**
   * Constructs a new LocaleTranslation object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   Lock service.
   * @param \Psr\Log\LoggerInterface $logger
   *   Logger.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, LockBackendInterface $lock, LoggerInterface $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->logger = $logger;
    $this->lock = $lock;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('lock.persistent'), $container
      ->get('logger.channel.tmgmt_extension_suit'));
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {
    $lockId = get_called_class() . ":processItem";

    // Lock ttl equals to time from queue worker plugin setup. There is no
    // need in locking more than required.
    $ttl = $this
      ->getPluginDefinition()["cron"]["time"];
    if (!$this->lock
      ->acquire($lockId, $ttl)) {

      // Do not remove item from queue if lock is already acquired.
      // It means current queue is being processed by another process.
      throw new RequeueException("Attempting to re-acquire {$lockId}.");
    }
    else {

      // Call hook method implemented by children.
      try {
        $this
          ->doProcessitem($data);
      } finally {

        // Release lock when work is done.
        $this->lock
          ->release($lockId);
      }
    }
  }

  /**
   * Hook method to be implemented in child classes.
   *
   * @param array $data
   *   Queue item data.
   */
  protected function doProcessItem(array $data) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
QueueWorkerLockedBase::$lock protected property Lock service.
QueueWorkerLockedBase::$logger protected property Logger.
QueueWorkerLockedBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
QueueWorkerLockedBase::doProcessItem protected function Hook method to be implemented in child classes. 2
QueueWorkerLockedBase::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
QueueWorkerLockedBase::__construct public function Constructs a new LocaleTranslation object. Overrides PluginBase::__construct