Problem/Motivation

There's no way to specify a different lease time for queue items than the lease time used for the whole queue. There's a bug in the Drupal Cron service currently which results in queue items always being reserved for only 1 second (see #3230541). Once this bug has been fixed, the cron-time specified for a queue should be used for total queue lease times and queue item lease times.

This is, however, less than ideal since there are cases where a developer may want to reserve queue items for longer than the over-all queue. For example, if someone had a queue worker which might process an item for up to 5 minutes, but didn't want to continue picking up new items after 3 minutes.

Proposed resolution

Separate queue worker annotations should be used for for a queue's time-limit and for the lease time for queue items.
A new "lease_time" annotation could be created which allows for specifying queue item lease times.

Use of the new annotation would look something like this:

<?php

namespace Drupal\slow_queue\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;

/**
 * Sleep for 5 minutes and then log a message to watchdog.
 *
 * @QueueWorker(
 *   id = "slow_queue",
 *   title = @Translation("This queue worker processes items slowly and logs a message in watchdog when it's finished"),
 *   cron = {
 *     "time" = 30,
 *     "lease_time"=60
 *   }
 * )
 */
class SlowQueueWorker extends QueueWorkerBase {

  /**
   * {@inheritdoc}
   */
  public function processItem($item) {
    \Drupal::logger('slow_queue')->notice('Starting: ' . $data);
    sleep(60);
    \Drupal::logger('slow_queue')->notice('Done: ' . $item);
  }

}

Issue fork drupal-3274931

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cliddell created an issue. See original summary.

yogeshmpawar made their first commit to this issue’s fork.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

cliddell’s picture

StatusFileSize
new7.56 KB

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jastraat’s picture

Status: Needs work » Needs review
StatusFileSize
new8.09 KB

Rolling a patch for Drupal 9.5.x.

jastraat’s picture

StatusFileSize
new8.09 KB

Re-rolling for syntax violation

_utsavsharma’s picture

StatusFileSize
new471 bytes
new8.09 KB

Fixed CCF for #8.
Please review.

Status: Needs review » Needs work

The last submitted patch, 9: 3274931-9.patch, failed testing. View results

mile23’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.