diff --git a/core/includes/common.inc b/core/includes/common.inc index 9601c76..aeb76c8 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -5045,7 +5045,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 570cb26..36f1f9a 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -487,9 +487,11 @@ function node_uninstall() { // Delete remaining general module variables. variable_del('node_access_needs_rebuild'); variable_del('node_admin_theme'); - variable_del('node_cron_last'); variable_del('node_recent_block_count'); variable_del('default_nodes_main'); + + // Delete any stored state. + state()->delete('node.cron_last'); } /** @@ -712,6 +714,19 @@ function node_update_8008() { } /** + * Moves node cron last run time from variable to state. + * + * @ingroup state_upgrade + */ +function node_update_8009() { + // Only migrate the variable if it exists and is not the default value. + if ($cron_last = update_variable_get('node_cron_last', 0)) { + state()->set('node.cron_last', $cron_last); + } + update_variable_del('node_cron_last'); +} + +/** * @} End of "addtogroup updates-7.x-to-8.x" * The next series of updates should start at 9000. */ diff --git a/core/modules/node/node.module b/core/modules/node/node.module index a09aee1..ee6d2ac 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1488,7 +1488,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 @@ -2512,7 +2512,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 = array_merge(array(language_load($node->langcode)), $node->translations()); 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/system.api.php b/core/modules/system/system.api.php index 2d09534..b319727 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -128,11 +128,14 @@ 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); - db_delete('mymodule_table') + $expires = state()->get('mymodule.cron_last_run'); + if (!$expires) { + $expires = 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 +2548,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 e938128..dd3da76 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 = state()->get('system.install_time') ?: 0; } @@ -2215,6 +2215,19 @@ function system_update_8032() { } /** + * Moves cron last run time from variable to state. + * + * @ingroup state_upgrade + */ +function system_update_8033() { + // Only migrate the variable if it exists and is not the default value. + if ($cron_last = update_variable_get('cron_last', 0)) { + state()->set('system.cron_last', $cron_last); + } + update_variable_del('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 7f9f33a..b4a8559 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -3679,7 +3679,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 && state()->get('system.install_task') == 'done') { - $cron_last = variable_get('cron_last', NULL); + $cron_last = state()->get('system.cron_last'); 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'); }