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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
- class \Drupal\tmgmt_extension_suit\Plugin\QueueWorker\QueueWorkerLockedBase implements ContainerFactoryPluginInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
Expanded class hierarchy of QueueWorkerLockedBase
File
- src/
Plugin/ QueueWorker/ QueueWorkerLockedBase.php, line 20
Namespace
Drupal\tmgmt_extension_suit\Plugin\QueueWorkerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
QueueWorkerLockedBase:: |
protected | property | Lock service. | |
QueueWorkerLockedBase:: |
protected | property | Logger. | |
QueueWorkerLockedBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
QueueWorkerLockedBase:: |
protected | function | Hook method to be implemented in child classes. | 2 |
QueueWorkerLockedBase:: |
public | function |
Works on a single queue item. Overrides QueueWorkerInterface:: |
|
QueueWorkerLockedBase:: |
public | function |
Constructs a new LocaleTranslation object. Overrides PluginBase:: |