85 lines
1.8 KiB
PHP
85 lines
1.8 KiB
PHP
<?php chdir(__DIR__) ?>
|
|
<?php require 'base.php' ?>
|
|
<?php
|
|
|
|
if (file_exists('.cron.lock'))
|
|
die('cron.php is locked by ".cron.lock"');
|
|
touch('.cron.lock');
|
|
|
|
function logi($thing) { echo date('Y/m/d H:i:s').' INFO | '.strval($thing)."\n"; }
|
|
function loge($thing) { echo date('Y/m/d H:i:s').' ERROR | '.strval($thing)."\n"; }
|
|
|
|
function end_cronjobs() {
|
|
unlink('.cron.lock');
|
|
die;
|
|
}
|
|
|
|
|
|
////////////////
|
|
// INITIAL SETUP
|
|
//
|
|
$GLOBALS['_crondir'] = $GLOBALS['appconf']['data_dir'].'/cron';
|
|
if (!file_exists($GLOBALS['_crondir'])) {
|
|
mkdir($GLOBALS['_crondir']);
|
|
end_cronjobs();
|
|
}
|
|
$time_start = time();
|
|
//
|
|
////////////////
|
|
|
|
|
|
////////////////////////////////
|
|
// CRONJOBS for database caching
|
|
//
|
|
function run__cronjobs_db() {
|
|
$dir = $GLOBALS['_crondir'].'/db';
|
|
if (!file_exists($dir)) {
|
|
mkdir($dir);
|
|
return false;
|
|
}
|
|
$jobs = [];
|
|
foreach (scandir($dir) as $jf) {
|
|
if (in_array($jf,['.','..']))
|
|
continue;
|
|
$ps = explode(',',$jf);
|
|
$jobs []= (object)[
|
|
'id' => $ps[2],
|
|
'file' => $dir.'/'.$jf,
|
|
'software' => $ps[0],
|
|
'instance' => $ps[1],
|
|
'time' => intval($ps[3]),
|
|
];
|
|
}
|
|
foreach ($jobs as $job) {
|
|
$ms = time() - filemtime($job->file);
|
|
$sql = file_get_contents($job->file);
|
|
if ($ms < $job->time) continue;
|
|
|
|
logi("Processing job: {$job->id}, software={$job->software}, instance={$job->instance}");
|
|
$ok = false;
|
|
switch ($job->software) {
|
|
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);
|
|
$pg->close();
|
|
$ok = true;
|
|
}
|
|
break;
|
|
}
|
|
if ($ok) {
|
|
logi("Job done: {$job->id}");
|
|
touch($job->file);
|
|
}
|
|
}
|
|
}
|
|
run__cronjobs_db();
|
|
//
|
|
////////////////////////////////
|
|
|
|
$ms_total = time() - $time_start;
|
|
logi("Cron has finished in {$ms_total} seconds");
|
|
|
|
end_cronjobs();
|