Verified Commit 367742e4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3230541 by cliddell, jday, yogeshmpawar, neclimdul, cmlara, Charlie ChX...

Issue #3230541 by cliddell, jday, yogeshmpawar, neclimdul, cmlara, Charlie ChX Negyesi: Queue items only reserved by cron for 1 second
parent 5b0f126b
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -174,10 +174,10 @@ protected function processQueues() {
        $this->queueFactory->get($queue_name)->createQueue();

        $queue_worker = $this->queueManager->createInstance($queue_name);
        $end = time() + ($info['cron']['time'] ?? 15);
        $end = $this->time->getCurrentTime() + $info['cron']['time'];
        $queue = $this->queueFactory->get($queue_name);
        $lease_time = isset($info['cron']['time']) ?: NULL;
        while (time() < $end && ($item = $queue->claimItem($lease_time))) {
        $lease_time = $info['cron']['time'];
        while ($this->time->getCurrentTime() < $end && ($item = $queue->claimItem($lease_time))) {
          try {
            $queue_worker->processItem($item->data);
            $queue->deleteItem($item);
+2 −2
Original line number Diff line number Diff line
@@ -241,7 +241,7 @@ public function garbageCollection() {
    try {
      // Clean up the queue for failed batches.
      $this->connection->delete(static::TABLE_NAME)
        ->condition('created', REQUEST_TIME - 864000, '<')
        ->condition('created', \Drupal::time()->getRequestTime() - 864000, '<')
        ->condition('name', 'drupal_batch:%', 'LIKE')
        ->execute();

@@ -252,7 +252,7 @@ public function garbageCollection() {
          'expire' => 0,
        ])
        ->condition('expire', 0, '<>')
        ->condition('expire', REQUEST_TIME, '<')
        ->condition('expire', \Drupal::time()->getRequestTime(), '<')
        ->execute();
    }
    catch (\Exception $e) {
+9 −4
Original line number Diff line number Diff line
@@ -40,11 +40,16 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
  public function processDefinition(&$definition, $plugin_id) {
    parent::processDefinition($definition, $plugin_id);

    // Assign a default time if a cron is specified.
    // Safeguard to ensure the default lease time is used in the case of a
    // malformed queue worker annotation where cron is specified without a time,
    // or an invalid time is provided.
    //
    // @see \Drupal\Core\Cron::processQueues()
    if (isset($definition['cron'])) {
      $definition['cron'] += [
        'time' => 15,
      ];
      $time = $definition['cron']['time'] ?? 0;
      if ($time <= 0) {
        $definition['cron']['time'] = self::DEFAULT_QUEUE_CRON_TIME;
      }
    }
  }

+7 −0
Original line number Diff line number Diff line
@@ -9,4 +9,11 @@
 */
interface QueueWorkerManagerInterface extends PluginManagerInterface {

  /**
   * The default time duration in seconds spent calling a queue worker.
   *
   * @var int
   */
  public const DEFAULT_QUEUE_CRON_TIME = 15;

}
+27 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\cron_queue_test\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;

/**
 * @QueueWorker(
 *   id = "cron_queue_test_lease_time",
 *   title = @Translation("Lease time test"),
 *   cron = {"time" = 100}
 * )
 */
class CronQueueTestLeaseTime extends QueueWorkerBase {

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {
    $state = \Drupal::state();
    $count = $state->get('cron_queue_test_lease_time', 0);
    $count++;
    $state->set('cron_queue_test_lease_time', $count);
    throw new \Exception('Leave me queued and leased!');
  }

}
Loading