Commit 463bf426 authored by catch's avatar catch
Browse files

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

parent 367e1a88
......@@ -107,9 +107,6 @@ public function run() {
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 @@ public function run() {
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 @@ public function run() {
$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 @@ public function run() {
}
}
}
}
// 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;
}
}
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