Skip to content
Snippets Groups Projects
Commit aae945c3 authored by Steven Wittens's avatar Steven Wittens
Browse files

#70995: Prevent cron re-runs

parent 4bd256a2
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -1698,24 +1698,56 @@ function drupal_cron_run() { ...@@ -1698,24 +1698,56 @@ function drupal_cron_run() {
set_time_limit(240); set_time_limit(240);
} }
// Check if the last cron run completed // Fetch the cron semaphore
if (variable_get('cron_busy', FALSE)) { $semaphore = variable_get('cron_semaphore', FALSE);
watchdog('cron', t('Last cron run did not complete.'), WATCHDOG_WARNING);
if ($semaphore) {
if (time() - $semaphore > 3600) {
// Either cron has been running for more than an hour or the semaphore
// was not reset due to a database error.
watchdog('cron', t('Cron has been running for more than an hour and is most likely stuck.'), WATCHDOG_ERROR);
// Release cron semaphore
variable_del('cron_semaphore');
}
else {
// Cron is still running normally.
watchdog('cron', t('Attempting to re-run cron while it is already running.'), WATCHDOG_WARNING);
}
} }
else { else {
variable_set('cron_busy', TRUE); // Register shutdown callback
} register_shutdown_function('drupal_cron_cleanup');
// Lock cron semaphore
variable_set('cron_semaphore', time());
// Iterate through the modules calling their cron handlers (if any):
module_invoke_all('cron');
// Iterate through the modules calling their cron handlers (if any): // Record cron time
module_invoke_all('cron'); variable_set('cron_last', time());
watchdog('cron', t('Cron run completed.'), WATCHDOG_NOTICE);
// Clean up // Release cron semaphore
variable_set('cron_busy', FALSE); variable_del('cron_semaphore');
variable_set('cron_last', time());
watchdog('cron', t('Cron run completed.'), WATCHDOG_NOTICE);
// Return TRUE so other functions can check if it did run successfully // Return TRUE so other functions can check if it did run successfully
return TRUE; return TRUE;
}
}
/**
* Shutdown function for cron cleanup.
*/
function drupal_cron_cleanup() {
// See if the semaphore is still locked.
if (variable_get('cron_semaphore', FALSE)) {
watchdog('cron', t('Cron run exceeded the time limit and was aborted.'), WATCHDOG_WARNING);
// Release cron semaphore
variable_del('cron_semaphore');
}
} }
/** /**
......
...@@ -3337,6 +3337,11 @@ function system_update_1013() { ...@@ -3337,6 +3337,11 @@ function system_update_1013() {
return $ret; return $ret;
} }
function system_update_1014() {
variable_del('cron_busy');
return array();
}
/** /**
* @} End of "defgroup updates-4.7-to-x.x" * @} End of "defgroup updates-4.7-to-x.x"
* The next series of updates should start at 2000. * The next series of updates should start at 2000.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment