Commit b686f057 authored by catch's avatar catch

Issue #2705809 by juampynr, dawehner, damiankloip, lahoosascoots: Queue...

Issue #2705809 by juampynr, dawehner, damiankloip, lahoosascoots: Queue garbage collection is not correctly run on cron
parent 77bb3d1b
......@@ -160,7 +160,8 @@ protected function processQueues() {
$queue_worker = $this->queueManager->createInstance($queue_name);
$end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
$queue = $this->queueFactory->get($queue_name);
while (time() < $end && ($item = $queue->claimItem())) {
$lease_time = isset($info['cron']['time']) ?: NULL;
while (time() < $end && ($item = $queue->claimItem($lease_time))) {
try {
$queue_worker->processItem($item->data);
$queue->deleteItem($item);
......
......@@ -31,10 +31,22 @@ public function testExceptions() {
// Run cron; the worker for this queue should throw an exception and handle
// it.
$this->cronRun();
$this->assertEqual(\Drupal::state()->get('cron_queue_test_exception'), 1);
// The item should be left in the queue.
$this->assertEqual($queue->numberOfItems(), 1, 'Failing item still in the queue after throwing an exception.');
// Garbage collection should set the expire flag back to 0, making the queue
// item "claimable" again. We have to wait for 2 seconds because
// CronQueueTestException has a "cron" time of 1 second. The test runs so
// fast that if we don't wait, the item won't be cleared by Garbage
// Collection so it won't become claimable.
// @see \Drupal\Core\Cron::processQueues()
sleep(2);
$this->cronRun();
$this->assertEqual(\Drupal::state()->get('cron_queue_test_exception'), 2);
$this->assertEqual($queue->numberOfItems(), 0, 'Item was processed and removed from the queue.');
// Get the queue to test the specific SuspendQueueException.
$queue = $this->container->get('queue')->get('cron_queue_test_broken_queue');
......
......@@ -1286,8 +1286,15 @@ function system_cron() {
}
// Clean up any garbage in the queue service.
if (\Drupal::service('queue') instanceof QueueGarbageCollectionInterface) {
\Drupal::service('queue')->garbageCollection();
$queue_worker_manager = \Drupal::service('plugin.manager.queue_worker');
$queue_factory = \Drupal::service('queue');
foreach (array_keys($queue_worker_manager->getDefinitions()) as $queue_name) {
$queue = $queue_factory->get($queue_name);
if ($queue instanceof QueueGarbageCollectionInterface) {
$queue->garbageCollection();
}
}
// Clean up PHP storage.
......
......@@ -8,7 +8,7 @@
* @QueueWorker(
* id = "cron_queue_test_exception",
* title = @Translation("Exception test"),
* cron = {"time" = 60}
* cron = {"time" = 1}
* )
*/
class CronQueueTestException extends QueueWorkerBase {
......@@ -17,7 +17,14 @@ class CronQueueTestException extends QueueWorkerBase {
* {@inheritdoc}
*/
public function processItem($data) {
throw new \Exception('That is not supposed to happen.');
$state = \Drupal::state();
if (!$state->get('cron_queue_test_exception')) {
$state->set('cron_queue_test_exception', 1);
throw new \Exception('That is not supposed to happen.');
}
else {
$state->set('cron_queue_test_exception', 2);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment