diff --git a/core/includes/common.inc b/core/includes/common.inc index 540f42b..94ca423 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -5067,7 +5067,7 @@ function drupal_cron_run() { } // Record cron time. - variable_set('cron_last', REQUEST_TIME); + state()->set('system.cron_last', REQUEST_TIME); watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE); // Release cron lock. diff --git a/core/modules/node/node.install b/core/modules/node/node.install index 9e1ee1b..fe85059 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -487,8 +487,10 @@ function node_uninstall() { // Delete remaining general module variables. state()->delete('node.node_access_needs_rebuild'); variable_del('node_admin_theme'); - variable_del('node_cron_last'); variable_del('node_recent_block_count'); + + // Delete any stored state. + state()->delete('node.cron_last'); } /** @@ -721,11 +723,15 @@ function node_update_8009() { /** * Moves node_access_needs_rebuild from variable to state. + * Moves node cron last run time from variable to state. * * @ingroup config_upgrade */ function node_update_8010() { - update_variables_to_state(array('node_access_needs_rebuild' => 'node.node_access_needs_rebuild')); + update_variables_to_state(array( + 'node_access_needs_rebuild' => 'node.node_access_needs_rebuild', + 'node_cron_last' =>'node.cron_last', + )); } /** diff --git a/core/modules/node/node.module b/core/modules/node/node.module index fc53461..6bef373 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1445,7 +1445,7 @@ function node_ranking() { ); // Add relevance based on creation or changed date. - if ($node_cron_last = variable_get('node_cron_last', 0)) { + if ($node_cron_last = state()->get('node.cron_last')) { $ranking['recent'] = array( 'title' => t('Recently posted'), // Exponential decay with half-life of 6 months, starting at last indexed node @@ -2469,7 +2469,7 @@ function _node_index_node(Node $node) { // Save the changed time of the most recent indexed node, for the search // results half-life calculation. - variable_set('node_cron_last', $node->changed); + state()->set('node.cron_last', $node->changed); $languages = $node->getTranslationLanguages(); diff --git a/core/modules/search/search.api.php b/core/modules/search/search.api.php index 0dd0a84..fe65b33 100644 --- a/core/modules/search/search.api.php +++ b/core/modules/search/search.api.php @@ -359,7 +359,7 @@ function hook_update_index() { // Save the changed time of the most recent indexed node, for the search // results half-life calculation. - variable_set('node_cron_last', $node->changed); + state()->set('node.cron_last', $node->changed); // Render the node. $build = node_view($node, 'search_index'); 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 1bdb269..ebf6b5c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/CronRunTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/CronRunTest.php @@ -58,19 +58,19 @@ function testAutomaticCron() { // not passed. $cron_last = time(); $cron_safe_threshold = 100; - variable_set('cron_last', $cron_last); + state()->set('system.cron_last', $cron_last); config('system.cron') ->set('threshold.autorun', $cron_safe_threshold) ->save(); $this->drupalGet(''); - $this->assertTrue($cron_last == variable_get('cron_last', NULL), 'Cron does not run when the cron threshold is not passed.'); + $this->assertTrue($cron_last == state()->get('system.cron_last'), '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); + state()->set('system.cron_last', $cron_last); $this->drupalGet(''); sleep(1); - $this->assertTrue($cron_last < variable_get('cron_last', NULL), 'Cron runs when the cron threshold is passed.'); + $this->assertTrue($cron_last < state()->get('system.cron_last'), 'Cron runs when the cron threshold is passed.'); // Disable the cron threshold through the interface. $admin_user = $this->drupalCreateUser(array('administer site configuration')); @@ -81,21 +81,21 @@ function testAutomaticCron() { // Test if cron does not run when the cron threshold is disabled. $cron_last = time() - 200; - variable_set('cron_last', $cron_last); + state()->set('system.cron_last', $cron_last); $this->drupalGet(''); - $this->assertTrue($cron_last == variable_get('cron_last', NULL), 'Cron does not run when the cron threshold is disabled.'); + $this->assertTrue($cron_last == state()->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.'); } /** * Make sure exceptions thrown on hook_cron() don't affect other modules. */ function testCronExceptions() { - variable_del('common_test_cron'); + state()->delete('common_test.cron'); // The common_test module throws an exception. If it isn't caught, the tests // won't finish successfully. // The common_test_cron_helper module sets the 'common_test_cron' variable. $this->cronRun(); - $result = variable_get('common_test_cron'); + $result = state()->get('common_test.cron'); $this->assertEqual($result, 'success', 'Cron correctly handles exceptions thrown during hook_cron() invocations.'); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php index fbd754f..f2bebb0 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php @@ -39,6 +39,14 @@ public function testSystemVariableUpgrade() { 'value' => TRUE, 'variable_name' => 'node_access_needs_rebuild', ); + $expected_state['node.cron_last'] = array( + 'value' => 1304208001, + 'variable_name' => 'node_cron_last', + ); + $expected_state['system.cron_last'] = array( + 'value' => 1304208002, + 'variable_name' => 'cron_last', + ); $expected_state['update.last_check'] = array( 'value' => 1304208000, 'variable_name' => 'update_last_check', diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 2d09534..e4c4ce7 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -128,11 +128,11 @@ function hook_admin_paths_alter(&$paths) { function hook_cron() { // Short-running operation example, not using a queue: // Delete all expired records since the last cron run. - $expires = variable_get('mymodule_cron_last_run', REQUEST_TIME); + $expires = state()->get('mymodule.cron_last_run') ?: REQUEST_TIME; db_delete('mymodule_table') ->condition('expires', $expires, '>=') ->execute(); - variable_set('mymodule_cron_last_run', REQUEST_TIME); + state()->set('mymodule.cron_last_run', REQUEST_TIME); // Long-running operation example, leveraging a queue: // Fetch feeds from other sites. @@ -2545,7 +2545,7 @@ function hook_requirements($phase) { // Report cron status if ($phase == 'runtime') { - $cron_last = variable_get('cron_last'); + $cron_last = state()->get('system.cron_last'); 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 5a96500..55f20b5 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -272,7 +272,7 @@ 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 = state()->get('system.cron_last'); if (!is_numeric($cron_last)) { $cron_last = variable_get('install_time', 0); } @@ -2200,6 +2200,15 @@ function system_update_8033() { } /** + * Moves cron last run time from variable to state. + * + * @ingroup config_upgrade + */ +function system_update_8034() { + update_variables_to_state(array('cron_last' => 'system.cron_last')); +} + +/** * @} 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 b8fec5d..7de9394 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -3701,7 +3701,7 @@ function system_run_automated_cron() { // Otherwise it could be triggered prematurely by Ajax requests during // installation. if (($threshold = config('system.cron')->get('threshold.autorun')) > 0 && variable_get('install_task') == 'done') { - $cron_last = variable_get('cron_last', NULL); + $cron_last = state()->get('system.cron_last') ?: NULL; if (!isset($cron_last) || (REQUEST_TIME - $cron_last > $threshold)) { drupal_cron_run(); } diff --git a/core/modules/system/tests/modules/common_test_cron_helper/common_test_cron_helper.module b/core/modules/system/tests/modules/common_test_cron_helper/common_test_cron_helper.module index 5cb4186..e2154ed 100644 --- a/core/modules/system/tests/modules/common_test_cron_helper/common_test_cron_helper.module +++ b/core/modules/system/tests/modules/common_test_cron_helper/common_test_cron_helper.module @@ -14,5 +14,5 @@ * @see common_test_cron() */ function common_test_cron_helper_cron() { - variable_set('common_test_cron', 'success'); + state()->set('common_test.cron', 'success'); } diff --git a/core/modules/system/tests/upgrade/drupal-7.state.system.database.php b/core/modules/system/tests/upgrade/drupal-7.state.system.database.php index 47c86f2..08e0b5c 100644 --- a/core/modules/system/tests/upgrade/drupal-7.state.system.database.php +++ b/core/modules/system/tests/upgrade/drupal-7.state.system.database.php @@ -23,3 +23,11 @@ ->key(array('name' => 'node_access_needs_rebuild')) ->fields(array('value' => serialize(TRUE))) ->execute(); +db_merge('variable') + ->key(array('name' => 'node_cron_last')) + ->fields(array('value' => serialize(1304208001))) + ->execute(); +db_merge('variable') + ->key(array('name' => 'cron_last')) + ->fields(array('value' => serialize(1304208002))) + ->execute();