From 6b3265742aecae522c5ac9eeedacfbdb7513a371 Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 29 Dec 2022 20:30:19 +0100 Subject: [PATCH] Replace cron/worker "last" config entries with key-value entries --- src/Core/Worker.php | 10 +++++----- src/Core/Worker/Daemon.php | 4 ++-- src/Module/Admin/Summary.php | 2 +- src/Worker/Cron.php | 10 +++++----- src/Worker/PullDirectory.php | 4 ++-- update.php | 12 ++++++++++-- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Core/Worker.php b/src/Core/Worker.php index d6f97eed21..cbe6645294 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -89,9 +89,9 @@ class Worker self::$process = $process; // Kill stale processes every 5 minutes - $last_cleanup = DI::config()->get('system', 'worker_last_cleaned', 0); + $last_cleanup = DI::keyValue()->get('worker_last_cleaned') ?? 0; if (time() > ($last_cleanup + 300)) { - DI::config()->set('system', 'worker_last_cleaned', time()); + DI::keyValue()->set( 'worker_last_cleaned', time()); Worker\Cron::killStaleWorkers(); } @@ -388,7 +388,7 @@ class Worker $stamp = (float)microtime(true); $condition = ["`id` = ? AND `next_try` < ?", $queue['id'], DateTimeFormat::utcNow()]; if (DBA::update('workerqueue', ['done' => true], $condition)) { - DI::config()->set('system', 'last_worker_execution', DateTimeFormat::utcNow()); + DI::keyValue()->set('last_worker_execution', DateTimeFormat::utcNow()); } self::$db_duration = (microtime(true) - $stamp); self::$db_duration_write += (microtime(true) - $stamp); @@ -429,7 +429,7 @@ class Worker $stamp = (float)microtime(true); if (DBA::update('workerqueue', ['done' => true], ['id' => $queue['id']])) { - DI::config()->set('system', 'last_worker_execution', DateTimeFormat::utcNow()); + DI::keyValue()->set('last_worker_execution', DateTimeFormat::utcNow()); } self::$db_duration = (microtime(true) - $stamp); self::$db_duration_write += (microtime(true) - $stamp); @@ -1422,7 +1422,7 @@ class Worker $duration = max($start, $end) - min($start, $end); // Quit when the last cron execution had been after the previous window - $last_cron = DI::config()->get('system', 'last_cron_daily'); + $last_cron = DI::keyValue()->get('last_cron_daily'); if ($last_cron + $duration > time()) { Logger::info('The Daily cron had been executed recently', ['last' => date(DateTimeFormat::MYSQL, $last_cron), 'start' => date('H:i:s', $start), 'end' => date('H:i:s', $end)]); return false; diff --git a/src/Core/Worker/Daemon.php b/src/Core/Worker/Daemon.php index 6fd64e4f59..858acc25d7 100644 --- a/src/Core/Worker/Daemon.php +++ b/src/Core/Worker/Daemon.php @@ -93,11 +93,11 @@ class Daemon } // Check every minute if the daemon is running - if (DI::config()->get('system', 'last_daemon_check', 0) + 60 > time()) { + if ((DI::keyValue()->get('last_daemon_check') ?? 0) + 60 > time()) { return; } - DI::config()->set('system', 'last_daemon_check', time()); + DI::keyValue()->set('last_daemon_check', time()); $pidfile = DI::config()->get('system', 'pidfile'); if (empty($pidfile)) { diff --git a/src/Module/Admin/Summary.php b/src/Module/Admin/Summary.php index d27a9b011b..42098c95cd 100644 --- a/src/Module/Admin/Summary.php +++ b/src/Module/Admin/Summary.php @@ -98,7 +98,7 @@ class Summary extends BaseAdmin $warningtext[] = DI::l10n()->t('The last update failed. Please run "php bin/console.php dbstructure update" from the command line and have a look at the errors that might appear. (Some of the errors are possibly inside the logfile.)'); } - $last_worker_call = DI::config()->get('system', 'last_worker_execution', false); + $last_worker_call = DI::keyValue()->get('last_worker_execution') ?? false; if (!$last_worker_call) { $warningtext[] = DI::l10n()->t('The worker was never executed. Please check your database structure!'); } elseif ((strtotime(DateTimeFormat::utcNow()) - strtotime($last_worker_call)) > 60 * 60) { diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index 94f002805b..055a9b4354 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -37,7 +37,7 @@ class Cron { $a = DI::app(); - $last = DI::config()->get('system', 'last_cron'); + $last = DI::keyValue()->get('last_cron'); $poll_interval = intval(DI::config()->get('system', 'cron_interval')); @@ -84,7 +84,7 @@ class Cron Worker::add(Worker::PRIORITY_LOW, 'PostUpdate'); // Hourly cron calls - if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) { + if ((DI::keyValue()->get('last_cron_hourly') ?? 0) + 3600 < time()) { // Update trending tags cache for the community page @@ -105,7 +105,7 @@ class Cron // Clear cache entries Worker::add(Worker::PRIORITY_LOW, 'ClearCache'); - DI::config()->set('system', 'last_cron_hourly', time()); + DI::keyValue()->set('last_cron_hourly', time()); } // Daily maintenance cron calls @@ -145,12 +145,12 @@ class Cron // Resubscribe to relay servers Relay::reSubscribe(); - DI::config()->set('system', 'last_cron_daily', time()); + DI::keyValue()->set('last_cron_daily', time()); } Logger::notice('end'); - DI::config()->set('system', 'last_cron', time()); + DI::keyValue()->set('last_cron', time()); } /** diff --git a/src/Worker/PullDirectory.php b/src/Worker/PullDirectory.php index 8a22e504c0..d174d11f3e 100644 --- a/src/Worker/PullDirectory.php +++ b/src/Worker/PullDirectory.php @@ -45,7 +45,7 @@ class PullDirectory return; } - $now = (int)DI::config()->get('system', 'last-directory-sync', 0); + $now = (int)(DI::keyValue()->get('last-directory-sync') ?? 0); Logger::info('Synchronization started.', ['now' => $now, 'directory' => $directory]); @@ -64,7 +64,7 @@ class PullDirectory $result = Contact::addByUrls($contacts['results']); $now = $contacts['now'] ?? 0; - DI::config()->set('system', 'last-directory-sync', $now); + DI::keyValue()->set('last-directory-sync', $now); Logger::info('Synchronization ended', ['now' => $now, 'count' => $result['count'], 'added' => $result['added'], 'updated' => $result['updated'], 'unchanged' => $result['unchanged'], 'directory' => $directory]); } diff --git a/update.php b/update.php index fb62f28df4..632d173a71 100644 --- a/update.php +++ b/update.php @@ -1148,11 +1148,19 @@ function update_1502() function update_1505() { - $postUpdateEntries = DBA::selectToArray('config', ['k', 'v'], ["`k` LIKE ?", "post_update_%"]); + $conditions = [ + "(`k` LIKE ?) OR (`k` = ?) OR (`cat` = ? AND `k` LIKE ?)", + "post_update_%", + "worker_last_cleaned", + "system", + "last%" + ]; + + $postUpdateEntries = DBA::selectToArray('config', ['k', 'v'], $conditions); foreach ($postUpdateEntries as $postUpdateEntry) { DI::keyValue()->set($postUpdateEntry['k'], $postUpdateEntry['v']); } - return DBA::delete('config', ["`k` LIKE ?", "post_update_%"]) ? Update::SUCCESS : Update::FAILED; + return DBA::delete('config', $conditions) ? Update::SUCCESS : Update::FAILED; }