Adapt cron/db system to add a field "last_accessed" for purging unused caches

This commit is contained in:
Bofh 2022-12-21 01:10:04 +01:00
parent 3729dd5539
commit 45b3e5cb15
2 changed files with 35 additions and 7 deletions

View File

@ -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;
}

View File

@ -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;
}