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\Queue\SuspendQueueException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* The Drupal core Cron service.
@ -196,16 +197,20 @@ class Cron implements CronInterface {
protected function invokeCronHandlers() {
$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):
foreach ($this->moduleHandler->getImplementations('cron') as $module) {
if (!$module_previous) {
$this->logger->notice('Starting execution of @module_cron().', [
$logger->notice('Starting execution of @module_cron().', [
'@module' => $module,
]);
}
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_previous' => $module_previous,
'@time' => Timer::read('cron_' . $module_previous) . 'ms',
@ -225,7 +230,7 @@ class Cron implements CronInterface {
$module_previous = $module;
}
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,
'@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) {
$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'] = [
$form['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']),
@ -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)),
];
$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';
@ -70,5 +57,4 @@ function automated_cron_settings_submit(array $form, FormStateInterface $form_st
\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

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

View File

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

View File

@ -147,6 +147,25 @@ class DbLogTest extends WebTestBase {
$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)));
// 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
// reliably add the number of newly created log entries to the current count
// to measure number of log entries created by cron.
@ -158,12 +177,7 @@ class DbLogTest extends WebTestBase {
// Get last ID after cron was run.
$current_id = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
// Get the number of enabled modules. Cron adds a log entry for each module.
$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)));
return $current_id - $last_id;
}
/**

View File

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

View File

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

View File

@ -10,11 +10,13 @@ use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
/**
* Configure cron settings for this site.
*/
class CronForm extends FormBase {
use ConfigFormBaseTrait;
/**
* Stores the state storage service.
@ -65,6 +67,13 @@ class CronForm extends FormBase {
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['system.cron'];
}
/**
* {@inheritdoc}
*/
@ -107,11 +116,31 @@ class CronForm extends FormBase {
);
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.'),
);
}
$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;
}
@ -119,6 +148,11 @@ class CronForm extends FormBase {
* Runs cron and reloads the page.
*/
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.
if ($this->cron->run()) {
drupal_set_message(t('Cron ran successfully.'));

View File

@ -1784,3 +1784,13 @@ function system_update_8202() {
/**
* @} 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_error' => 1209600,
],
'logging' => 1,
],
'system.date' => [
'country' => [

View File

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

View File

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