diff --git a/core/includes/common.inc b/core/includes/common.inc index 689d136..6a59408 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -5249,7 +5249,12 @@ function drupal_cron_run() { } // Record cron time. - variable_set('cron_last', REQUEST_TIME); + db_merge('state') + ->key(array( + 'name' => 'system.cron_last', + )) + ->fields(array('value' => REQUEST_TIME)) + ->execute(); watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE); // Release cron lock. diff --git a/core/modules/system/lib/Drupal/system/Tests/System/CronRunTest.php b/core/modules/system/lib/Drupal/system/Tests/System/CronRunTest.php index 601c5a8..a6282df 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/CronRunTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/CronRunTest.php @@ -54,19 +54,31 @@ class CronRunTest extends WebTestBase { // not passed. $cron_last = time(); $cron_safe_threshold = 100; - variable_set('cron_last', $cron_last); + db_merge('state') + ->key(array( + 'name' => 'system.cron_last', + )) + ->fields(array('value' => $cron_last)) + ->execute(); config('system.cron') ->set('cron_safe_threshold', $cron_safe_threshold) ->save(); $this->drupalGet(''); - $this->assertTrue($cron_last == variable_get('cron_last', NULL), t('Cron does not run when the cron threshold is not passed.')); + $new_cron_last = db_query('SELECT value FROM {state} WHERE name = :name', array(':name' => 'system.cron_last'))->fetchField(); + $this->assertTrue($cron_last == $new_cron_last, t('Cron does not run when the cron threshold is not passed.')); // Test if cron runs when the cron threshold was passed. $cron_last = time() - 200; - variable_set('cron_last', $cron_last); + db_update('state') + ->fields(array( + 'name' => 'system.cron_last', + 'value' => $cron_last, + )) + ->execute(); $this->drupalGet(''); sleep(1); - $this->assertTrue($cron_last < variable_get('cron_last', NULL), t('Cron runs when the cron threshold is passed.')); + $new_cron_last = db_query('SELECT value FROM {state} WHERE name = :name', array(':name' => 'system.cron_last'))->fetchField(); + $this->assertTrue($cron_last < $new_cron_last, t('Cron runs when the cron threshold is passed.')); // Disable the cron threshold through the interface. $admin_user = $this->drupalCreateUser(array('administer site configuration')); @@ -78,8 +90,15 @@ class CronRunTest extends WebTestBase { // Test if cron does not run when the cron threshold is disabled. $cron_last = time() - 200; variable_set('cron_last', $cron_last); + db_update('state') + ->fields(array( + 'name' => 'system.cron_last', + 'value' => $cron_last, + )) + ->execute(); $this->drupalGet(''); - $this->assertTrue($cron_last == variable_get('cron_last', NULL), t('Cron does not run when the cron threshold is disabled.')); + $new_cron_last = db_query('SELECT value FROM {state} WHERE name = :name', array(':name' => 'system.cron_last'))->fetchField(); + $this->assertTrue($cron_last == $new_cron_last, t('Cron does not run when the cron threshold is disabled.')); } /** diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index f64447e..e6e04bf 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -1596,7 +1596,8 @@ function system_cron_settings($form, &$form_state) { '#value' => t('Run cron'), '#submit' => array('system_run_cron_submit'), ); - $status = '

' . t('Last run: %cron-last ago.', array('%cron-last' => format_interval(REQUEST_TIME - variable_get('cron_last')),)) . '

'; + $cron_last = db_query('SELECT value FROM {state} WHERE name = :name', array(':name' => 'system.cron_last'))->fetchField(); + $status = '

' . t('Last run: %cron-last ago.', array('%cron-last' => format_interval(REQUEST_TIME - $cron_last))) . '

'; $form['status'] = array( '#markup' => $status, ); diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 12428fe..1d22e1e 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -2597,7 +2597,7 @@ function hook_requirements($phase) { // Report cron status if ($phase == 'runtime') { - $cron_last = variable_get('cron_last'); + $cron_last = db_query('SELECT value FROM {state} WHERE name = :name', array(':name' => 'system.cron_last'))->fetchField(); if (is_numeric($cron_last)) { $requirements['cron']['value'] = $t('Last run !time ago', array('!time' => format_interval(REQUEST_TIME - $cron_last))); diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 9a2217d..a127c4e 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -272,8 +272,9 @@ function system_requirements($phase) { $help = $t('For more information, see the online handbook entry for configuring cron jobs.', array('@cron-handbook' => 'http://drupal.org/cron')); // Determine when cron last ran. - $cron_last = variable_get('cron_last'); + $cron_last = db_query('SELECT value FROM {state} WHERE name = :name', array(':name' => 'system.cron_last'))->fetchField(); if (!is_numeric($cron_last)) { + // @todo And the next one... ;) $cron_last = variable_get('install_time', 0); } @@ -1581,6 +1582,26 @@ function system_schema() { ), ); + $schema['state'] = array( + 'description' => 'Stores persistent state data (not configuration) of modules.', + 'fields' => array( + 'name' => array( + 'description' => 'The state item name, prefixed with the module name (delimited by a dot).', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The state item value.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + ), + ), + 'primary key' => array('name'), + ); + $schema['system'] = array( 'description' => "A list of all modules, themes, and theme engines that are or have been installed in Drupal's file system.", 'fields' => array( @@ -1954,6 +1975,32 @@ function system_update_8012() { } /** + * Create the {state} table. + */ +function system_update_8013() { + $schema['state'] = array( + 'description' => 'Stores persistent state data (not configuration) of modules.', + 'fields' => array( + 'name' => array( + 'description' => 'The state item name, prefixed with the module name (delimited by a dot).', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The state item value.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + ), + ), + 'primary key' => array('name'), + ); + db_create_table('state', $schema['state']); +} + +/** * @} End of "defgroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 75b1fa2..7e4bba4 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -3784,8 +3784,8 @@ function system_run_automated_cron() { // Otherwise it could be triggered prematurely by Ajax requests during // installation. if (($threshold = config('system.cron')->get('cron_safe_threshold')) > 0 && variable_get('install_task') == 'done') { - $cron_last = variable_get('cron_last', NULL); - if (!isset($cron_last) || (REQUEST_TIME - $cron_last > $threshold)) { + $cron_last = db_query('SELECT value FROM {state} WHERE name = :name', array(':name' => 'system.cron_last'))->fetchField(); + if (empty($cron_last) || (REQUEST_TIME - $cron_last > $threshold)) { drupal_cron_run(); } }