Initial Drupal 11 with DDEV setup
This commit is contained in:
6
web/core/modules/automated_cron/automated_cron.info.yml
Normal file
6
web/core/modules/automated_cron/automated_cron.info.yml
Normal file
@ -0,0 +1,6 @@
|
||||
name: 'Automated Cron'
|
||||
type: module
|
||||
description: 'Provides an automated way to run cron jobs, by executing them at the end of a server response.'
|
||||
package: Core
|
||||
version: VERSION
|
||||
configure: system.cron_settings
|
||||
16
web/core/modules/automated_cron/automated_cron.module
Normal file
16
web/core/modules/automated_cron/automated_cron.module
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
*/
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Form submission handler for system_cron_settings().
|
||||
*/
|
||||
function automated_cron_settings_submit(array $form, FormStateInterface $form_state): void {
|
||||
\Drupal::configFactory()->getEditable('automated_cron.settings')
|
||||
->set('interval', $form_state->getValue('interval'))
|
||||
->save();
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
parameters:
|
||||
automated_cron.skip_procedural_hook_scan: true
|
||||
|
||||
services:
|
||||
_defaults:
|
||||
autoconfigure: true
|
||||
autowire: true
|
||||
automated_cron.subscriber:
|
||||
class: Drupal\automated_cron\EventSubscriber\AutomatedCron
|
||||
@ -0,0 +1 @@
|
||||
interval: 10800
|
||||
@ -0,0 +1,14 @@
|
||||
# Schema for the configuration files of the Automated cron module.
|
||||
|
||||
automated_cron.settings:
|
||||
type: config_object
|
||||
label: 'Automated cron settings'
|
||||
constraints:
|
||||
FullyValidatable: ~
|
||||
mapping:
|
||||
interval:
|
||||
type: integer
|
||||
label: 'Run cron every'
|
||||
constraints:
|
||||
Range:
|
||||
min: 0
|
||||
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\automated_cron\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\State\StateInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\TerminateEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
|
||||
/**
|
||||
* A subscriber running cron after a response is sent.
|
||||
*/
|
||||
class AutomatedCron implements EventSubscriberInterface {
|
||||
|
||||
public function __construct(
|
||||
#[AutowireServiceClosure('cron')]
|
||||
protected readonly \Closure $cron,
|
||||
protected readonly ConfigFactoryInterface $configFactory,
|
||||
protected StateInterface $state,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Run the automated cron if enabled.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
|
||||
* The Event to process.
|
||||
*/
|
||||
public function onTerminate(TerminateEvent $event): void {
|
||||
$interval = $this->configFactory->get('automated_cron.settings')->get('interval');
|
||||
if ($interval > 0) {
|
||||
$cron_next = $this->state->get('system.cron_last', 0) + $interval;
|
||||
if ((int) $event->getRequest()->server->get('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(): array {
|
||||
return [KernelEvents::TERMINATE => [['onTerminate', 100]]];
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\automated_cron\Hook;
|
||||
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Hook\Attribute\Hook;
|
||||
|
||||
/**
|
||||
* Hook implementations for automated_cron.
|
||||
*/
|
||||
class AutomatedCronHooks {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
#[Hook('help')]
|
||||
public function help($route_name, RouteMatchInterface $route_match): ?string {
|
||||
switch ($route_name) {
|
||||
case 'help.page.automated_cron':
|
||||
$output = '';
|
||||
$output .= '<h2>' . $this->t('About') . '</h2>';
|
||||
$output .= '<p>' . $this->t('The Automated Cron module runs cron operations for your site using normal browser/page requests instead of having to set up a separate cron job. The Automated Cron module checks at the end of each server response when cron operation was last ran and, if it has been too long since last run, it executes the cron tasks after sending a server response. For more information, see the <a href=":automated_cron-documentation">online documentation for the Automated Cron module</a>.', [
|
||||
':automated_cron-documentation' => 'https://www.drupal.org/documentation/modules/automated_cron',
|
||||
]) . '</p>';
|
||||
$output .= '<h2>' . $this->t('Uses') . '</h2>';
|
||||
$output .= '<dl>';
|
||||
$output .= '<dt>' . $this->t('Configuring Automated Cron') . '</dt>';
|
||||
$output .= '<dd>' . $this->t('On the <a href=":cron-settings">Cron page</a>, you can set the frequency (time interval) for running cron jobs.', [
|
||||
':cron-settings' => Url::fromRoute('system.cron_settings')->toString(),
|
||||
]) . '</dd>';
|
||||
$output .= '<dt>' . $this->t('Disabling Automated Cron') . '</dt>';
|
||||
$output .= '<dd>' . $this->t('To disable automated cron, the recommended method is to uninstall the module, to reduce site overhead. If you only want to disable it temporarily, you can set the frequency to Never on the Cron page, and then change the frequency back when you want to start it up again.') . '</dd>';
|
||||
$output .= '</dl>';
|
||||
return $output;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter() for the system_cron_settings() form.
|
||||
*/
|
||||
#[Hook('form_system_cron_settings_alter')]
|
||||
public function formSystemCronSettingsAlter(&$form, &$form_state) : void {
|
||||
$automated_cron_settings = \Drupal::config('automated_cron.settings');
|
||||
$options = [3600, 10800, 21600, 43200, 86400, 604800];
|
||||
$form['cron']['interval'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Run cron every'),
|
||||
'#description' => $this->t('More information about setting up scheduled tasks can be found by <a href=":url">reading the cron tutorial on drupal.org</a>.', [
|
||||
':url' => 'https://www.drupal.org/docs/8/administering-a-drupal-8-site/cron-automated-tasks',
|
||||
]),
|
||||
'#default_value' => $automated_cron_settings->get('interval'),
|
||||
'#options' => [
|
||||
0 => $this->t('Never'),
|
||||
] + array_map([
|
||||
\Drupal::service('date.formatter'),
|
||||
'formatInterval',
|
||||
], array_combine($options, $options)),
|
||||
];
|
||||
// Add submit callback.
|
||||
$form['#submit'][] = 'automated_cron_settings_submit';
|
||||
// Theme this form as a config form.
|
||||
$form['#theme'] = 'system_config_form';
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\automated_cron\Functional;
|
||||
|
||||
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
|
||||
|
||||
/**
|
||||
* Generic module test for automated_cron.
|
||||
*
|
||||
* @group automated_cron
|
||||
*/
|
||||
class GenericTest extends GenericModuleTestBase {}
|
||||
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\automated_cron\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Tests for automated_cron.
|
||||
*
|
||||
* @group automated_cron
|
||||
*/
|
||||
class AutomatedCronTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['automated_cron'];
|
||||
|
||||
/**
|
||||
* Tests that automated cron runs cron on an HTTP request.
|
||||
*
|
||||
* @covers \Drupal\automated_cron\EventSubscriber\AutomatedCron::onTerminate
|
||||
*/
|
||||
public function testRunsCronOnHttpRequest(): void {
|
||||
// Set automated_cron interval and times.
|
||||
// Any interval > 0 should work.
|
||||
$this->config('automated_cron.settings')->set('interval', 10800)->save();
|
||||
$request = new Request();
|
||||
|
||||
// Cron uses `$_SERVER['REQUEST_TIME']` to set `system.cron_last`
|
||||
// because there is no request stack, so we set the request time
|
||||
// to the same.
|
||||
$expected = $_SERVER['REQUEST_TIME'];
|
||||
$request->server->set('REQUEST_TIME', $expected);
|
||||
|
||||
// Invoke `AutomatedCron::onTerminate` and check result.
|
||||
$this->assertNull($this->container->get('state')->get('system.cron_last'));
|
||||
$this->container->get('kernel')->terminate($request, new Response());
|
||||
$this->assertEquals($expected, $this->container->get('state')->get('system.cron_last'));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user