Replace cron/worker "last" config entries with key-value entries

This commit is contained in:
Philipp Holzer 2022-12-29 20:30:19 +01:00
parent 10f8631cd9
commit 6b3265742a
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
6 changed files with 25 additions and 17 deletions

View file

@ -89,9 +89,9 @@ class Worker
self::$process = $process; self::$process = $process;
// Kill stale processes every 5 minutes // 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)) { if (time() > ($last_cleanup + 300)) {
DI::config()->set('system', 'worker_last_cleaned', time()); DI::keyValue()->set( 'worker_last_cleaned', time());
Worker\Cron::killStaleWorkers(); Worker\Cron::killStaleWorkers();
} }
@ -388,7 +388,7 @@ class Worker
$stamp = (float)microtime(true); $stamp = (float)microtime(true);
$condition = ["`id` = ? AND `next_try` < ?", $queue['id'], DateTimeFormat::utcNow()]; $condition = ["`id` = ? AND `next_try` < ?", $queue['id'], DateTimeFormat::utcNow()];
if (DBA::update('workerqueue', ['done' => true], $condition)) { 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 = (microtime(true) - $stamp);
self::$db_duration_write += (microtime(true) - $stamp); self::$db_duration_write += (microtime(true) - $stamp);
@ -429,7 +429,7 @@ class Worker
$stamp = (float)microtime(true); $stamp = (float)microtime(true);
if (DBA::update('workerqueue', ['done' => true], ['id' => $queue['id']])) { 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 = (microtime(true) - $stamp);
self::$db_duration_write += (microtime(true) - $stamp); self::$db_duration_write += (microtime(true) - $stamp);
@ -1422,7 +1422,7 @@ class Worker
$duration = max($start, $end) - min($start, $end); $duration = max($start, $end) - min($start, $end);
// Quit when the last cron execution had been after the previous window // 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()) { 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)]); 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; return false;

View file

@ -93,11 +93,11 @@ class Daemon
} }
// Check every minute if the daemon is running // 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; return;
} }
DI::config()->set('system', 'last_daemon_check', time()); DI::keyValue()->set('last_daemon_check', time());
$pidfile = DI::config()->get('system', 'pidfile'); $pidfile = DI::config()->get('system', 'pidfile');
if (empty($pidfile)) { if (empty($pidfile)) {

View file

@ -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.)'); $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) { if (!$last_worker_call) {
$warningtext[] = DI::l10n()->t('The worker was never executed. Please check your database structure!'); $warningtext[] = DI::l10n()->t('The worker was never executed. Please check your database structure!');
} elseif ((strtotime(DateTimeFormat::utcNow()) - strtotime($last_worker_call)) > 60 * 60) { } elseif ((strtotime(DateTimeFormat::utcNow()) - strtotime($last_worker_call)) > 60 * 60) {

View file

@ -37,7 +37,7 @@ class Cron
{ {
$a = DI::app(); $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')); $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
@ -84,7 +84,7 @@ class Cron
Worker::add(Worker::PRIORITY_LOW, 'PostUpdate'); Worker::add(Worker::PRIORITY_LOW, 'PostUpdate');
// Hourly cron calls // 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 // Update trending tags cache for the community page
@ -105,7 +105,7 @@ class Cron
// Clear cache entries // Clear cache entries
Worker::add(Worker::PRIORITY_LOW, 'ClearCache'); 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 // Daily maintenance cron calls
@ -145,12 +145,12 @@ class Cron
// Resubscribe to relay servers // Resubscribe to relay servers
Relay::reSubscribe(); Relay::reSubscribe();
DI::config()->set('system', 'last_cron_daily', time()); DI::keyValue()->set('last_cron_daily', time());
} }
Logger::notice('end'); Logger::notice('end');
DI::config()->set('system', 'last_cron', time()); DI::keyValue()->set('last_cron', time());
} }
/** /**

View file

@ -45,7 +45,7 @@ class PullDirectory
return; 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]); Logger::info('Synchronization started.', ['now' => $now, 'directory' => $directory]);
@ -64,7 +64,7 @@ class PullDirectory
$result = Contact::addByUrls($contacts['results']); $result = Contact::addByUrls($contacts['results']);
$now = $contacts['now'] ?? 0; $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]); Logger::info('Synchronization ended', ['now' => $now, 'count' => $result['count'], 'added' => $result['added'], 'updated' => $result['updated'], 'unchanged' => $result['unchanged'], 'directory' => $directory]);
} }

View file

@ -1148,11 +1148,19 @@ function update_1502()
function update_1505() 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) { foreach ($postUpdateEntries as $postUpdateEntry) {
DI::keyValue()->set($postUpdateEntry['k'], $postUpdateEntry['v']); 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;
} }