From 2a8927af8f7ed8cd8d279beda1646db2a21859c1 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 7 Feb 2014 13:00:05 +0100 Subject: [PATCH] [IMPROVED] Check if cron is running in PHP scripts * Ensures we don't spawn more than one PHP process * Will require admin-intervention to reset the cron_active flag in DB Shouldn't trigger often but may help people. --- cronjobs/shared.inc.php | 5 ++++- public/include/classes/monitoring.class.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cronjobs/shared.inc.php b/cronjobs/shared.inc.php index b34026df..a73c5b56 100644 --- a/cronjobs/shared.inc.php +++ b/cronjobs/shared.inc.php @@ -82,7 +82,10 @@ if ($monitoring->isDisabled($cron_name)) { // Mark cron as running for monitoring $log->logDebug('Marking cronjob as running for monitoring'); -$monitoring->setStatus($cron_name . '_starttime', 'date', time()); +if (!$monitoring->startCronjob($cron_name)) { + $log->logFatal('Unable to start cronjob: ' . $monitoring->getCronError()); + exit; +} // Check if we need to halt our crons due to an outstanding upgrade if ($setting->getValue('DB_VERSION') != DB_VERSION || $config['version'] != CONFIG_VERSION) { diff --git a/public/include/classes/monitoring.class.php b/public/include/classes/monitoring.class.php index 3cd84cba..b34bd47a 100644 --- a/public/include/classes/monitoring.class.php +++ b/public/include/classes/monitoring.class.php @@ -93,6 +93,21 @@ class Monitoring extends Base { return false; } + /** + * Start a cronjob, mark various fields properly + * @param cron_name string Cronjob name + **/ + public function startCronjob($cron_name) { + $aStatus = $this->getStatus($cron_name . '_active'); + if ($aStatus['value'] == 1) { + $this->setErrorMessage('Cron is already active in database: ' . $cron_name . '_active is 1'); + return false; + } + $this->setStatus($cron_name . "_active", "yesno", 1); + $this->setStatus($cron_name . '_starttime', 'date', time()); + return true; + } + /** * End cronjob with an error message * @param cron_name string Cronjob Name