Cron.php in Drupal 8
File
core/lib/Drupal/Core/Cron.php
View source
<?php
namespace Drupal\Core;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Timer;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\Core\Queue\RequeueException;
use Drupal\Core\Queue\SuspendQueueException;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\State\StateInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class Cron implements CronInterface {
protected $moduleHandler;
protected $lock;
protected $queueFactory;
protected $state;
protected $accountSwitcher;
protected $logger;
protected $queueManager;
protected $time;
public function __construct(ModuleHandlerInterface $module_handler, LockBackendInterface $lock, QueueFactory $queue_factory, StateInterface $state, AccountSwitcherInterface $account_switcher, LoggerInterface $logger, QueueWorkerManagerInterface $queue_manager, TimeInterface $time = NULL) {
$this->moduleHandler = $module_handler;
$this->lock = $lock;
$this->queueFactory = $queue_factory;
$this->state = $state;
$this->accountSwitcher = $account_switcher;
$this->logger = $logger;
$this->queueManager = $queue_manager;
$this->time = $time ?: \Drupal::service('datetime.time');
}
public function run() {
@ignore_user_abort(TRUE);
$this->accountSwitcher
->switchTo(new AnonymousUserSession());
Environment::setTimeLimit(240);
$return = FALSE;
if (!$this->lock
->acquire('cron', 900.0)) {
$this->logger
->warning('Attempting to re-run cron while it is already running.');
}
else {
$this
->invokeCronHandlers();
$this
->setCronLastTime();
$this->lock
->release('cron');
$return = TRUE;
}
$this
->processQueues();
$this->accountSwitcher
->switchBack();
return $return;
}
protected function setCronLastTime() {
$request_time = $this->time
->getRequestTime();
$this->state
->set('system.cron_last', $request_time);
$this->logger
->notice('Cron run completed.');
}
protected function processQueues() {
foreach ($this->queueManager
->getDefinitions() as $queue_name => $info) {
if (isset($info['cron'])) {
$this->queueFactory
->get($queue_name)
->createQueue();
$queue_worker = $this->queueManager
->createInstance($queue_name);
$end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
$queue = $this->queueFactory
->get($queue_name);
$lease_time = isset($info['cron']['time']) ?: NULL;
while (time() < $end && ($item = $queue
->claimItem($lease_time))) {
try {
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
} catch (RequeueException $e) {
$queue
->releaseItem($item);
} catch (SuspendQueueException $e) {
$queue
->releaseItem($item);
watchdog_exception('cron', $e);
continue 2;
} catch (\Exception $e) {
watchdog_exception('cron', $e);
}
}
}
}
}
protected function invokeCronHandlers() {
$module_previous = '';
$time_logging_enabled = \Drupal::config('system.cron')
->get('logging');
$logger = $time_logging_enabled ? $this->logger : new NullLogger();
foreach ($this->moduleHandler
->getImplementations('cron') as $module) {
if (!$module_previous) {
$logger
->notice('Starting execution of @module_cron().', [
'@module' => $module,
]);
}
else {
$logger
->notice('Starting execution of @module_cron(), execution of @module_previous_cron() took @time.', [
'@module' => $module,
'@module_previous' => $module_previous,
'@time' => Timer::read('cron_' . $module_previous) . 'ms',
]);
}
Timer::start('cron_' . $module);
try {
$this->moduleHandler
->invoke($module, 'cron');
} catch (\Exception $e) {
watchdog_exception('cron', $e);
}
Timer::stop('cron_' . $module);
$module_previous = $module;
}
if ($module_previous) {
$logger
->notice('Execution of @module_previous_cron() took @time.', [
'@module_previous' => $module_previous,
'@time' => Timer::read('cron_' . $module_previous) . 'ms',
]);
}
}
}
Classes
Name |
Description |
Cron |
The Drupal core Cron service. |