Issue #2823543 by Dane Powell, dawehner, dhendriks: Make cron execution logging optional

8.3.x
Nathaniel Catchpole 2016-11-24 10:45:13 +00:00
parent 1d4ffea34a
commit f96cbb29eb
12 changed files with 84 additions and 26 deletions

View File

@ -13,6 +13,7 @@ use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Session\AccountSwitcherInterface; use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Queue\SuspendQueueException; use Drupal\Core\Queue\SuspendQueueException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/** /**
* The Drupal core Cron service. * The Drupal core Cron service.
@ -196,16 +197,20 @@ class Cron implements CronInterface {
protected function invokeCronHandlers() { protected function invokeCronHandlers() {
$module_previous = ''; $module_previous = '';
// If detailed logging isn't enabled, don't log individual execution times.
$time_logging_enabled = \Drupal::config('system.cron')->get('logging');
$logger = $time_logging_enabled ? $this->logger : new NullLogger();
// Iterate through the modules calling their cron handlers (if any): // Iterate through the modules calling their cron handlers (if any):
foreach ($this->moduleHandler->getImplementations('cron') as $module) { foreach ($this->moduleHandler->getImplementations('cron') as $module) {
if (!$module_previous) { if (!$module_previous) {
$this->logger->notice('Starting execution of @module_cron().', [ $logger->notice('Starting execution of @module_cron().', [
'@module' => $module, '@module' => $module,
]); ]);
} }
else { else {
$this->logger->notice('Starting execution of @module_cron(), execution of @module_previous_cron() took @time.', [ $logger->notice('Starting execution of @module_cron(), execution of @module_previous_cron() took @time.', [
'@module' => $module, '@module' => $module,
'@module_previous' => $module_previous, '@module_previous' => $module_previous,
'@time' => Timer::read('cron_' . $module_previous) . 'ms', '@time' => Timer::read('cron_' . $module_previous) . 'ms',
@ -225,7 +230,7 @@ class Cron implements CronInterface {
$module_previous = $module; $module_previous = $module;
} }
if ($module_previous) { if ($module_previous) {
$this->logger->notice('Execution of @module_previous_cron() took @time.', [ $logger->notice('Execution of @module_previous_cron() took @time.', [
'@module_previous' => $module_previous, '@module_previous' => $module_previous,
'@time' => Timer::read('cron_' . $module_previous) . 'ms', '@time' => Timer::read('cron_' . $module_previous) . 'ms',
]); ]);

View File

@ -34,14 +34,8 @@ function automated_cron_help($route_name, RouteMatchInterface $route_match) {
function automated_cron_form_system_cron_settings_alter(&$form, &$form_state) { function automated_cron_form_system_cron_settings_alter(&$form, &$form_state) {
$automated_cron_settings = \Drupal::config('automated_cron.settings'); $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]; $options = [3600, 10800, 21600, 43200, 86400, 604800];
$form['automated_cron']['interval'] = [ $form['cron']['interval'] = [
'#type' => 'select', '#type' => 'select',
'#title' => t('Run cron every'), '#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']), '#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']),
@ -49,13 +43,6 @@ function automated_cron_form_system_cron_settings_alter(&$form, &$form_state) {
'#options' => [0 => t('Never')] + array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($options, $options)), '#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. // Add submit callback.
$form['#submit'][] = 'automated_cron_settings_submit'; $form['#submit'][] = 'automated_cron_settings_submit';
@ -70,5 +57,4 @@ function automated_cron_settings_submit(array $form, FormStateInterface $form_st
\Drupal::configFactory()->getEditable('automated_cron.settings') \Drupal::configFactory()->getEditable('automated_cron.settings')
->set('interval', $form_state->getValue('interval')) ->set('interval', $form_state->getValue('interval'))
->save(); ->save();
drupal_set_message(t('The configuration options have been saved.'));
} }

View File

@ -36,6 +36,7 @@ class ConfigInstallProfileOverrideTest extends WebTestBase {
'requirements_warning' => 172800, 'requirements_warning' => 172800,
'requirements_error' => 1209600, 'requirements_error' => 1209600,
), ),
'logging' => 1,
); );
// The expected active configuration altered by the install profile. // The expected active configuration altered by the install profile.
$expected_profile_data = array( $expected_profile_data = array(
@ -43,6 +44,7 @@ class ConfigInstallProfileOverrideTest extends WebTestBase {
'requirements_warning' => 259200, 'requirements_warning' => 259200,
'requirements_error' => 1209600, 'requirements_error' => 1209600,
), ),
'logging' => 1,
); );
$expected_profile_data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($expected_profile_data)); $expected_profile_data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($expected_profile_data));

View File

@ -1,4 +1,4 @@
threshold: threshold:
requirements_warning: 172800 requirements_warning: 172800
requirements_error: 1209600 requirements_error: 1209600
logging: 1

View File

@ -147,6 +147,25 @@ class DbLogTest extends WebTestBase {
$count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField(); $count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
$this->assertTrue($count > $row_limit, format_string('Dblog row count of @count exceeds row limit of @limit', array('@count' => $count, '@limit' => $row_limit))); $this->assertTrue($count > $row_limit, format_string('Dblog row count of @count exceeds row limit of @limit', array('@count' => $count, '@limit' => $row_limit)));
// Get the number of enabled modules. Cron adds a log entry for each module.
$list = \Drupal::moduleHandler()->getImplementations('cron');
$module_count = count($list);
$cron_detailed_count = $this->runCron();
$this->assertTrue($cron_detailed_count == $module_count + 2, format_string('Cron added @count of @expected new log entries', array('@count' => $cron_detailed_count, '@expected' => $module_count + 2)));
// Test disabling of detailed cron logging.
$this->config('system.cron')->set('logging', 0)->save();
$cron_count = $this->runCron();
$this->assertTrue($cron_count = 1, format_string('Cron added @count of @expected new log entries', array('@count' => $cron_count, '@expected' => 1)));
}
/**
* Runs cron and returns number of new log entries.
*
* @return int
* Number of new watchdog entries.
*/
private function runCron() {
// Get last ID to compare against; log entries get deleted, so we can't // Get last ID to compare against; log entries get deleted, so we can't
// reliably add the number of newly created log entries to the current count // reliably add the number of newly created log entries to the current count
// to measure number of log entries created by cron. // to measure number of log entries created by cron.
@ -158,12 +177,7 @@ class DbLogTest extends WebTestBase {
// Get last ID after cron was run. // Get last ID after cron was run.
$current_id = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); $current_id = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
// Get the number of enabled modules. Cron adds a log entry for each module. return $current_id - $last_id;
$list = \Drupal::moduleHandler()->getImplementations('cron');
$module_count = count($list);
$count = $current_id - $last_id;
$this->assertTrue(($current_id - $last_id) == $module_count + 2, format_string('Cron added @count of @expected new log entries', array('@count' => $count, '@expected' => $module_count + 2)));
} }
/** /**

View File

@ -1,3 +1,4 @@
threshold: threshold:
requirements_warning: 172800 requirements_warning: 172800
requirements_error: 1209600 requirements_error: 1209600
logging: 1

View File

@ -72,6 +72,9 @@ system.cron:
requirements_error: requirements_error:
type: integer type: integer
label: 'Requirements error period' label: 'Requirements error period'
logging:
type: integer
label: 'Detailed cron logging'
system.date: system.date:
type: config_object type: config_object

View File

@ -10,11 +10,13 @@ use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface; use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
/** /**
* Configure cron settings for this site. * Configure cron settings for this site.
*/ */
class CronForm extends FormBase { class CronForm extends FormBase {
use ConfigFormBaseTrait;
/** /**
* Stores the state storage service. * Stores the state storage service.
@ -65,6 +67,13 @@ class CronForm extends FormBase {
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
} }
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['system.cron'];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -107,11 +116,31 @@ class CronForm extends FormBase {
); );
if (!$this->moduleHandler->moduleExists('automated_cron')) { if (!$this->moduleHandler->moduleExists('automated_cron')) {
$form['cron'] = array( $form['automated_cron'] = array(
'#markup' => $this->t('Enable the <em>Automated Cron</em> module to allow cron execution at the end of a server response.'), '#markup' => $this->t('Enable the <em>Automated Cron</em> module to allow cron execution at the end of a server response.'),
); );
} }
$form['cron'] = [
'#title' => t('Cron settings'),
'#type' => 'details',
'#open' => TRUE,
];
$form['cron']['logging'] = array(
'#type' => 'checkbox',
'#title' => t('Detailed cron logging'),
'#default_value' => $this->config('system.cron')->get('logging'),
'#description' => 'Run times of individual cron jobs will be written to watchdog',
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save configuration'),
'#button_type' => 'primary',
];
return $form; return $form;
} }
@ -119,6 +148,11 @@ class CronForm extends FormBase {
* Runs cron and reloads the page. * Runs cron and reloads the page.
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('system.cron')
->set('logging', $form_state->getValue('logging'))
->save();
drupal_set_message(t('The configuration options have been saved.'));
// Run cron manually from Cron form. // Run cron manually from Cron form.
if ($this->cron->run()) { if ($this->cron->run()) {
drupal_set_message(t('Cron ran successfully.')); drupal_set_message(t('Cron ran successfully.'));

View File

@ -1784,3 +1784,13 @@ function system_update_8202() {
/** /**
* @} End of "addtogroup updates-8.2.3". * @} End of "addtogroup updates-8.2.3".
*/ */
/**
* Add detailed cron logging configuration.
*/
function system_update_8203() {
$config_factory = \Drupal::configFactory();
$system_cron_config = $config_factory->getEditable('system.cron')
->set('logging', 1)
->save(TRUE);
}

View File

@ -24,6 +24,7 @@ class MigrateSystemConfigurationTest extends MigrateDrupal7TestBase {
'requirements_warning' => 172800, 'requirements_warning' => 172800,
'requirements_error' => 1209600, 'requirements_error' => 1209600,
], ],
'logging' => 1,
], ],
'system.date' => [ 'system.date' => [
'country' => [ 'country' => [

View File

@ -1,3 +1,4 @@
threshold: threshold:
requirements_warning: 172800 requirements_warning: 172800
requirements_error: 1209600 requirements_error: 1209600
logging: 1

View File

@ -1,3 +1,4 @@
threshold: threshold:
requirements_warning: 259200 requirements_warning: 259200
requirements_error: 1209600 requirements_error: 1209600
logging: 1