Skip to content

Commit 07a910e

Browse files
Issue #2021933 by Crell, David_Rothstein, joachim, lz1irq: Catch exceptions from queue workers.
1 parent 808a61a commit 07a910e

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
Drupal 7.28, xxxx-xx-xx (development version)
33
-----------------------
4+
- Made the cron queue system log any exceptions that are thrown while an item
5+
in the queue is being processed, rather than stopping the entire PHP request.
46
- Improved screen reader support by adding an aria-live HTML attribute to file
57
upload fields when there is an error uploading the file (minor markup
68
change).

includes/common.inc

+9-2
Original file line numberDiff line numberDiff line change
@@ -5294,8 +5294,15 @@ function drupal_cron_run() {
52945294
$end = time() + (isset($info['time']) ? $info['time'] : 15);
52955295
$queue = DrupalQueue::get($queue_name);
52965296
while (time() < $end && ($item = $queue->claimItem())) {
5297-
$function($item->data);
5298-
$queue->deleteItem($item);
5297+
try {
5298+
$function($item->data);
5299+
$queue->deleteItem($item);
5300+
}
5301+
catch (Exception $e) {
5302+
// In case of exception log it and leave the item in the queue
5303+
// to be processed again later.
5304+
watchdog_exception('cron', $e);
5305+
}
52995306
}
53005307
}
53015308
// Restore the user.

modules/system/system.test

+38
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,44 @@ class CronRunTestCase extends DrupalWebTestCase {
867867
}
868868
}
869869

870+
/**
871+
* Test execution of the cron queue.
872+
*/
873+
class CronQueueTestCase extends DrupalWebTestCase {
874+
/**
875+
* Implement getInfo().
876+
*/
877+
public static function getInfo() {
878+
return array(
879+
'name' => 'Cron queue functionality',
880+
'description' => 'Tests the cron queue runner.',
881+
'group' => 'System'
882+
);
883+
}
884+
885+
function setUp() {
886+
parent::setUp(array('common_test', 'common_test_cron_helper'));
887+
}
888+
889+
/**
890+
* Tests that exceptions thrown by workers are handled properly.
891+
*/
892+
function testExceptions() {
893+
$queue = DrupalQueue::get('cron_queue_test_exception');
894+
895+
// Enqueue an item for processing.
896+
$queue->createItem(array($this->randomName() => $this->randomName()));
897+
898+
// Run cron; the worker for this queue should throw an exception and handle
899+
// it.
900+
$this->cronRun();
901+
902+
// The item should be left in the queue.
903+
$this->assertEqual($queue->numberOfItems(), 1, 'Failing item still in the queue after throwing an exception.');
904+
}
905+
906+
}
907+
870908
class AdminMetaTagTestCase extends DrupalWebTestCase {
871909
/**
872910
* Implement getInfo().
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name = Cron Queue test
2+
description = 'Support module for the cron queue runner.'
3+
package = Testing
4+
version = VERSION
5+
core = 7.x
6+
hidden = TRUE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Implements hook_cron_queue_info().
5+
*/
6+
function cron_queue_test_cron_queue_info() {
7+
$queues['cron_queue_test_exception'] = array(
8+
'worker callback' => 'cron_queue_test_exception',
9+
);
10+
return $queues;
11+
}
12+
13+
function cron_queue_test_exception($item) {
14+
throw new Exception('That is not supposed to happen.');
15+
}

0 commit comments

Comments
 (0)