From 45b3e5cb15c042d96d9c7951968088e54c602edd Mon Sep 17 00:00:00 2001 From: Bofh Date: Wed, 21 Dec 2022 01:10:04 +0100 Subject: [PATCH] Adapt cron/db system to add a field "last_accessed" for purging unused caches --- base.php | 31 +++++++++++++++++++++++++++---- cron.php | 11 ++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/base.php b/base.php index 02c6f83..5dc5568 100644 --- a/base.php +++ b/base.php @@ -755,6 +755,22 @@ function content_cache__get($key) { unlink($cfg->file); return null; } + $fname = basename($cfg->file); + $ps = explode(',', $fname); + if (count($ps) === 2 && $ps[1] === 'always') { + $dir = filedb_getdir('cron/db'); + foreach (scandir($dir) as $fl) { + if (in_array($fl,['.','..'])) + continue; + if (strpos($fl, ','.$ps[0].',') !== false) { + $data = @unserialize(filedb_get('cron/db',$fl)); + if ($data === false) break; + $data['last_accessed'] = time(); + file_put_contents($dir.'/'.$fl, serialize($data)); + break; + } + } + } return unserialize(file_get_contents($cfg->file)); } @@ -788,15 +804,22 @@ function cronjob_db_create($software, $instance, $sql, $time=3600) { $dir_crons_db = $dir_crons.'/db'; if (!file_exists($dir_crons_db)) mkdir($dir_crons_db); + $cron_file = $software.','.$instance.','.$job_key; $result_file = $dir_crons_db.'/'.$cron_file.','.$time; - $touch_1970 = !file_exists($result_file); + if (!file_exists($result_file)) { + file_put_contents($result_file, serialize( + ['last_accessed' => time(), 'sql' => $sql])); + touch($result_file, 1000); + } + + $result_fname = basename($result_file); foreach (scandir($dir_crons_db) as $fl) { - if (strpos($fl, $cron_file) !== false) + if (in_array($fl,['.','..'])) + continue; + if (strpos($fl, $cron_file) !== false && $fl !== $result_fname) unlink($dir_crons_db.'/'.$fl); } - file_put_contents($result_file, $sql); - if ($touch_1970) touch($result_file, 1000); return $cron_file; } diff --git a/cron.php b/cron.php index c52470d..747d07a 100644 --- a/cron.php +++ b/cron.php @@ -54,7 +54,12 @@ function run__cronjobs_db() { } foreach ($jobs as $job) { $ms = time() - filemtime($job->file); - $sql = file_get_contents($job->file); + $data = null; + $data = @unserialize(file_get_contents($job->file)); + if ($data === false) { + $data = ['last_accessed' => time(), 'sql' => $data]; + file_put_contents($job->file, serialize($data)); + } if ($ms < $job->time) continue; logi("Processing job: {$job->id}, software={$job->software}, instance={$job->instance}"); @@ -63,8 +68,8 @@ function run__cronjobs_db() { case 'mastodon': $pg = new PgDatabase($job->software, $job->instance); if ($pg->is_ok()) { - $result = $pg->fetch_all($sql); - content_cache__put($job->software.$job->instance.$sql, 'always,'.$job->time, $result); + $result = $pg->fetch_all($data['sql']); + content_cache__put($job->software.$job->instance.$data['sql'], 'always,'.$job->time, $result); $pg->close(); $ok = true; }