Skip to content
Snippets Groups Projects
Commit e467b20d authored by Lucas Hedding's avatar Lucas Hedding Committed by Lucas Hedding
Browse files

Issue #3058250 by heddn: Cron frequency

parent 650696ac
No related branches found
No related tags found
No related merge requests found
......@@ -97,3 +97,14 @@ services:
- '@state'
tags:
- { name: readiness_checker, category: warning}
automatic_updates.cron_frequency:
class: Drupal\automatic_updates\ReadinessChecker\CronFrequency
arguments:
- '@config.factory'
- '@module_handler'
tags:
- { name: readiness_checker, category: warning}
automatic_updates.cron_override:
class: Drupal\automatic_updates\EventSubscriber\CronOverride
tags:
- { name: config.factory.override }
<?php
namespace Drupal\automatic_updates\EventSubscriber;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
/**
* This class overrides the default system warning and error limits for cron.
*
* It notifies site administrators on a more strict time frame if cron has not
* recently run.
*/
class CronOverride implements ConfigFactoryOverrideInterface {
/**
* Warn at 3 hours.
*/
const WARNING_THRESHOLD = 10800;
/**
* Error at 6 hours.
*/
const ERROR_THRESHOLD = 21600;
/**
* {@inheritdoc}
*/
public function loadOverrides($names) {
$overrides = [];
if (in_array('system.cron', $names, TRUE)) {
$overrides['system.cron']['threshold'] = [
'requirements_warning' => $this::WARNING_THRESHOLD,
'requirements_error' => $this::ERROR_THRESHOLD,
];
}
return $overrides;
}
/**
* {@inheritdoc}
*/
public function getCacheSuffix() {
return 'CronOverride';
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($name) {
return new CacheableMetadata();
}
/**
* {@inheritdoc}
*/
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
}
<?php
namespace Drupal\automatic_updates\ReadinessChecker;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
/**
* Cron frequency checker.
*/
class CronFrequency implements ReadinessCheckerInterface {
use StringTranslationTrait;
/**
* Minimum cron threshold is 3 hours.
*/
const MINIMUM_CRON_INTERVAL = 10800;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* CronFrequency constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The state service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public function run() {
$messages = [];
if ($this->moduleHandler->moduleExists('automated_cron') && $this->configFactory->get('automated_cron.settings')->get('interval') > $this::MINIMUM_CRON_INTERVAL) {
$messages[] = $this->t('Cron is not set to run frequently enough. <a href="@configure">Configure it</a> to run at least every 3 hours or disable automated cron and run it via an external scheduling system.', [
'@configure' => Url::fromRoute('system.cron_settings')->toString(),
]);
}
return $messages;
}
}
<?php
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessChecker;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests what happens when cron has frequency of greater than 3 hours.
*
* @group automatic_updates
*/
class CronFrequencyTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'automated_cron',
'automatic_updates',
'system',
];
/**
* Tests the functionality of supported PHP version readiness checks.
*/
public function testSupportedPhpVersion() {
// Module automated_cron is disabled.
$messages = $this->container->get('automatic_updates.cron_frequency')->run();
$this->assertEmpty($messages);
// Module automated_cron has default configuration.
$this->enableModules(['automated_cron']);
$messages = $this->container->get('automatic_updates.cron_frequency')->run();
$this->assertEmpty($messages);
// Module automated_cron has 6 hour configuration.
$this->container->get('config.factory')
->getEditable('automated_cron.settings')
->set('interval', 21600)
->save();
$messages = $this->container->get('automatic_updates.cron_frequency')->run();
$this->assertEquals('Cron is not set to run frequently enough. <a href="/admin/config/system/cron">Configure it</a> to run at least every 3 hours or disable automated cron and run it via an external scheduling system.', $messages[0]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment