Issue #2249899 by damiankloip: Refactor Drupal\Core\Cron.

8.0.x
Nathaniel Catchpole 2014-04-30 12:02:36 +01:00
parent 367e1a8823
commit 463bf4269d
1 changed files with 49 additions and 31 deletions

View File

@ -107,9 +107,6 @@ class Cron implements CronInterface {
drupal_set_time_limit(240);
$return = FALSE;
// Grab the defined cron queues.
$queues = $this->moduleHandler->invokeAll('queue_info');
$this->moduleHandler->alter('queue_info', $queues);
// Try to acquire cron lock.
if (!$this->lock->acquire('cron', 240.0)) {
@ -117,28 +114,8 @@ class Cron implements CronInterface {
watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
}
else {
// Make sure every queue exists. There is no harm in trying to recreate an
// existing queue.
foreach ($queues as $queue_name => $info) {
if (isset($info['cron'])) {
$this->queueFactory->get($queue_name)->createQueue();
}
}
// Iterate through the modules calling their cron handlers (if any):
foreach ($this->moduleHandler->getImplementations('cron') as $module) {
// Do not let an exception thrown by one module disturb another.
try {
$this->moduleHandler->invoke($module, 'cron');
}
catch (\Exception $e) {
watchdog_exception('cron', $e);
}
}
// Record cron time.
$this->state->set('system.cron_last', REQUEST_TIME);
watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
$this->invokeCronHandlers();
$this->setCronLastTime();
// Release cron lock.
$this->lock->release('cron');
@ -147,8 +124,41 @@ class Cron implements CronInterface {
$return = TRUE;
}
// Process cron queues.
$this->processQueues();
// Restore the user.
$this->currentUser->setAccount($original_user);
if ($original_session_saving) {
$this->sessionManager->enable();
}
return $return;
}
/**
* Records and logs the request time for this cron invocation.
*/
protected function setCronLastTime() {
// Record cron time.
$this->state->set('system.cron_last', REQUEST_TIME);
watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
}
/**
* Processes cron queues.
*/
protected function processQueues() {
// Grab the defined cron queues.
$queues = $this->moduleHandler->invokeAll('queue_info');
$this->moduleHandler->alter('queue_info', $queues);
foreach ($queues as $queue_name => $info) {
if (isset($info['cron'])) {
// Make sure every queue exists. There is no harm in trying to recreate
// an existing queue.
$this->queueFactory->get($queue_name)->createQueue();
$callback = $info['worker callback'];
$end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
$queue = $this->queueFactory->get($queue_name);
@ -165,14 +175,22 @@ class Cron implements CronInterface {
}
}
}
}
// Restore the user.
$this->currentUser->setAccount($original_user);
if ($original_session_saving) {
$this->sessionManager->enable();
/**
* Invokes any cron handlers implementing hook_cron.
*/
protected function invokeCronHandlers() {
// Iterate through the modules calling their cron handlers (if any):
foreach ($this->moduleHandler->getImplementations('cron') as $module) {
// Do not let an exception thrown by one module disturb another.
try {
$this->moduleHandler->invoke($module, 'cron');
}
catch (\Exception $e) {
watchdog_exception('cron', $e);
}
}
return $return;
}
}