Issue #2249899 by damiankloip: Refactor Drupal\Core\Cron.
parent
367e1a8823
commit
463bf4269d
|
@ -107,9 +107,6 @@ class Cron implements CronInterface {
|
||||||
drupal_set_time_limit(240);
|
drupal_set_time_limit(240);
|
||||||
|
|
||||||
$return = FALSE;
|
$return = FALSE;
|
||||||
// Grab the defined cron queues.
|
|
||||||
$queues = $this->moduleHandler->invokeAll('queue_info');
|
|
||||||
$this->moduleHandler->alter('queue_info', $queues);
|
|
||||||
|
|
||||||
// Try to acquire cron lock.
|
// Try to acquire cron lock.
|
||||||
if (!$this->lock->acquire('cron', 240.0)) {
|
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);
|
watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Make sure every queue exists. There is no harm in trying to recreate an
|
$this->invokeCronHandlers();
|
||||||
// existing queue.
|
$this->setCronLastTime();
|
||||||
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);
|
|
||||||
|
|
||||||
// Release cron lock.
|
// Release cron lock.
|
||||||
$this->lock->release('cron');
|
$this->lock->release('cron');
|
||||||
|
@ -147,8 +124,41 @@ class Cron implements CronInterface {
|
||||||
$return = TRUE;
|
$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) {
|
foreach ($queues as $queue_name => $info) {
|
||||||
if (isset($info['cron'])) {
|
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'];
|
$callback = $info['worker callback'];
|
||||||
$end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
|
$end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
|
||||||
$queue = $this->queueFactory->get($queue_name);
|
$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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $return;
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue