From 0f5e321c0b2e368adf493e1e466bec0ff900dcbd Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Fri, 31 Mar 2023 08:24:31 +0200 Subject: Mutex for actualize script (#5235) * Simplify extension method One parameter was not used Furthermore, that unused parameter was preventing simpler calls (i.e. without a dedicated extension class). Now it is possible to register a call back with a lamda: ```php Minz_ExtensionManager::addHook('feed_before_actualize', function () { // Hello }); ``` * Mutex for actualize script In the case of extremely long cron jobs that are refreshing feeds, a cron job might start before the previous one has completed, leading to an accumulation of cron jobs running in parallel. Although we already have a mutex preventing concurrency problems on a single feed refresh, the problem occurs in particular if each feed can take a long time (e.g. due to heavy processing, which is my usecase). https://github.com/FreshRSS/FreshRSS/pull/5234 must be merged first * Fiw wrong return --- app/actualize_script.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/actualize_script.php b/app/actualize_script.php index 8e445b1e7..b4d57c4e3 100755 --- a/app/actualize_script.php +++ b/app/actualize_script.php @@ -37,6 +37,30 @@ function notice(string $message): void { } } +// +// Avoid having multiple actualization processes at the same time +$mutexFile = TMP_PATH . '/actualize.freshrss.lock'; +$mutexTtl = 900; // seconds (refreshed before each new feed) +if (file_exists($mutexFile) && ((time() - @filemtime($mutexFile)) > $mutexTtl)) { + unlink($mutexFile); +} + +if (($handle = @fopen($mutexFile, 'x')) === false) { + notice('FreshRSS feeds actualization was already running, so aborting new run at ' . $begin_date->format('c')); + die(); +} +fclose($handle); + +register_shutdown_function(function () use ($mutexFile) { + unlink($mutexFile); +}); + +Minz_ExtensionManager::addHook('feed_before_actualize', function ($feed) use ($mutexFile) { + touch($mutexFile); + return $feed; +}); +// + notice('FreshRSS starting feeds actualization at ' . $begin_date->format('c')); // make sure the PHP setup of the CLI environment is compatible with FreshRSS as well -- cgit v1.2.3