Move into nested docroot

This commit is contained in:
Rob Davies 2017-02-13 15:31:17 +00:00
parent 83a0d3a149
commit c8b70abde9
13405 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,7 @@
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
core: 8.x
configure: system.cron_settings

View file

@ -0,0 +1,74 @@
<?php
/**
* @file
* Provides an automated cron by executing it at the end of a response.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function automated_cron_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.automated_cron':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . 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 .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Configuring Automated Cron') . '</dt>';
$output .= '<dd>' . t('On the <a href=":cron-settings">Cron page</a>, you can set the frequency (time interval) for running cron jobs.', [':cron-settings' => \Drupal::url('system.cron_settings')]) . '</dd>';
$output .= '<dt>' . t('Disabling Automated Cron') . '</dt>';
$output .= '<dd>' . 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;
}
}
/**
* Implements hook_form_FORM_ID_alter() for the system_cron_settings() form.
*/
function automated_cron_form_system_cron_settings_alter(&$form, &$form_state) {
$automated_cron_settings = \Drupal::config('automated_cron.settings');
// Add automated cron settings.
$form['automated_cron'] = [
'#title' => t('Cron settings'),
'#type' => 'details',
'#open' => TRUE,
];
$options = [3600, 10800, 21600, 43200, 86400, 604800];
$form['automated_cron']['interval'] = [
'#type' => 'select',
'#title' => t('Run cron every'),
'#description' => 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/cron']),
'#default_value' => $automated_cron_settings->get('interval'),
'#options' => [0 => t('Never')] + array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($options, $options)),
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save configuration'),
'#button_type' => 'primary',
];
// Add submit callback.
$form['#submit'][] = 'automated_cron_settings_submit';
// Theme this form as a config form.
$form['#theme'] = 'system_config_form';
}
/**
* Form submission handler for system_cron_settings().
*/
function automated_cron_settings_submit(array $form, FormStateInterface $form_state) {
\Drupal::configFactory()->getEditable('automated_cron.settings')
->set('interval', $form_state->getValue('interval'))
->save();
drupal_set_message(t('The configuration options have been saved.'));
}

View file

@ -0,0 +1,6 @@
services:
automated_cron.subscriber:
class: Drupal\automated_cron\EventSubscriber\AutomatedCron
arguments: ['@cron', '@config.factory', '@state']
tags:
- { name: event_subscriber }

View file

@ -0,0 +1 @@
interval: 10800

View file

@ -0,0 +1,9 @@
# Schema for the configuration files of the Automated cron module.
automated_cron.settings:
type: config_object
label: 'Automated cron settings'
mapping:
interval:
type: integer
label: 'Run cron every'

View file

@ -0,0 +1,80 @@
<?php
namespace Drupal\automated_cron\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\CronInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* A subscriber running cron after a response is sent.
*/
class AutomatedCron 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.
*
* @var \Drupal\Core\State\StateInterface;
*/
protected $state;
/**
* Constructs a new automated cron runner.
*
* @param \Drupal\Core\CronInterface $cron
* The cron service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\State\StateInterface $state
* The state key-value store service.
*/
public function __construct(CronInterface $cron, ConfigFactoryInterface $config_factory, StateInterface $state) {
$this->cron = $cron;
$this->config = $config_factory->get('automated_cron.settings');
$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) {
$interval = $this->config->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() {
return [KernelEvents::TERMINATE => [['onTerminate', 100]]];
}
}