Commit a8650951 authored by catch's avatar catch
Browse files

Issue #1790920 by alexpott, japicoder, LinL, heyrocker: Move cron_last(),...

Issue #1790920 by alexpott, japicoder, LinL, heyrocker: Move cron_last(), node_cron_last() and common_test_cron() to state system.
parent 76ea2781
......@@ -5055,7 +5055,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.
......
......@@ -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');
}
/**
......@@ -728,6 +730,15 @@ function node_update_8010() {
update_variables_to_state(array('node_access_needs_rebuild' => 'node.node_access_needs_rebuild'));
}
/**
* Moves node cron last run time from variable to state.
*
* @ingroup config_upgrade
*/
function node_update_8011() {
update_variables_to_state(array('node_cron_last' =>'node.cron_last'));
}
/**
* @} End of "addtogroup updates-7.x-to-8.x"
* The next series of updates should start at 9000.
......
......@@ -1453,7 +1453,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
......@@ -2477,7 +2477,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();
......
......@@ -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');
......
......@@ -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.');
}
}
......@@ -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',
......
......@@ -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)));
......
......@@ -272,7 +272,7 @@ function system_requirements($phase) {
$help = $t('For more information, see the online handbook entry for <a href="@cron-handbook">configuring cron jobs</a>.', 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);
}
......@@ -2199,6 +2199,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.
......
......@@ -3702,7 +3702,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();
}
......
......@@ -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');
}
......@@ -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();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment