Issue #2220687 by znerol: Convert system_run_automated_cron into a subscriber for KernelEvents::TERMINATE.

8.0.x
catch 2014-03-25 20:13:11 +01:00
parent 13f5d8389a
commit 906f131c59
4 changed files with 97 additions and 16 deletions

View File

@ -45,7 +45,6 @@ class RequestCloseSubscriber implements EventSubscriberInterface {
if ($this->moduleHandler instanceof CachedModuleHandlerInterface) { if ($this->moduleHandler instanceof CachedModuleHandlerInterface) {
$this->moduleHandler->writeCache(); $this->moduleHandler->writeCache();
} }
system_run_automated_cron();
} }
/** /**

View File

@ -0,0 +1,92 @@
<?php
/**
* @file
* Contains Drupal\system\EventSubscriber\AutomaticCron.
*/
namespace Drupal\system\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\CronInterface;
use Drupal\Core\KeyValueStore\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* A subscriber running cron when a request terminates.
*/
class AutomaticCron implements EventSubscriberInterface {
/**
* The cron service.
*
* @var \Drupal\Core\CronInterface
*/
protected $cron;
/**
* The cron configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The state key value store.
*
* Drupal\Core\KeyValueStore\StateInterface;
*/
protected $state;
/**
* Construct a new automatic cron runner.
*
* @param \Drupal\Core\CronInterface $cron
* The cron service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\KeyValueStore\StateInterface $state
* The state key value store.
*/
public function __construct(CronInterface $cron, ConfigFactoryInterface $config_factory, StateInterface $state) {
$this->cron = $cron;
$this->config = $config_factory->get('system.cron');
$this->state = $state;
}
/**
* Run the automated cron if enabled.
*
* @param Symfony\Component\HttpKernel\Event\PostResponseEvent $event
* The Event to process.
*/
public function onTerminate(PostResponseEvent $event) {
// If the site is not fully installed, suppress the automated cron run.
// Otherwise it could be triggered prematurely by Ajax requests during
// installation.
if ($this->state->get('install_task') == 'done') {
$threshold = $this->config->get('threshold.autorun');
if ($threshold > 0) {
$cron_next = $this->state->get('system.cron_last', 0) + $threshold;
if (REQUEST_TIME > $cron_next) {
$this->cron->run();
}
}
}
}
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
public static function getSubscribedEvents() {
$events[KernelEvents::TERMINATE][] = array('onTerminate', 100);
return $events;
}
}

View File

@ -1939,21 +1939,6 @@ function system_block_view_system_help_block_alter(array &$build, BlockPluginInt
unset($build['#contextual_links']); unset($build['#contextual_links']);
} }
/**
* Run the automated cron if enabled.
*/
function system_run_automated_cron() {
// If the site is not fully installed, suppress the automated cron run.
// Otherwise it could be triggered prematurely by Ajax requests during
// installation.
if (($threshold = \Drupal::config('system.cron')->get('threshold.autorun')) > 0 && \Drupal::state()->get('install_task') == 'done') {
$cron_last = \Drupal::state()->get('system.cron_last') ?: NULL;
if (!isset($cron_last) || (REQUEST_TIME - $cron_last > $threshold)) {
\Drupal::service('cron')->run();
}
}
}
/** /**
* Returns HTML for a confirmation form. * Returns HTML for a confirmation form.
* *

View File

@ -30,3 +30,8 @@ services:
class: Drupal\system\SystemConfigSubscriber class: Drupal\system\SystemConfigSubscriber
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
system.automatic_cron:
class: Drupal\system\EventSubscriber\AutomaticCron
arguments: ['@cron', '@config.factory', '@state']
tags:
- { name: event_subscriber }