39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
// Opus 19avr v5.3: L99 honest cache-based endpoint
|
|
header('Content-Type: application/json');
|
|
|
|
$cache = '/tmp/l99-honest-cache.json';
|
|
$lock = '/tmp/l99-honest.lock';
|
|
|
|
// If cache exists and age < 20min, return it
|
|
if (file_exists($cache)) {
|
|
$age = time() - filemtime($cache);
|
|
$data = @json_decode(file_get_contents($cache), true);
|
|
if ($data && $age < 1200) {
|
|
$data['cache_age_sec'] = $age;
|
|
echo json_encode($data, JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Trigger async refresh if not already running
|
|
if (!file_exists($lock) || (time() - filemtime($lock)) > 300) {
|
|
@file_put_contents($lock, (string)time());
|
|
@exec('nohup /var/www/html/api/handlers/l99-honest-refresh.sh > /dev/null 2>&1 &');
|
|
}
|
|
|
|
// Return stale cache if exists, or "computing" status
|
|
if (file_exists($cache)) {
|
|
$data = @json_decode(file_get_contents($cache), true);
|
|
$data['cache_age_sec'] = time() - filemtime($cache);
|
|
$data['status'] = 'stale_cache_refreshing';
|
|
echo json_encode($data, JSON_PRETTY_PRINT);
|
|
} else {
|
|
echo json_encode([
|
|
'ok' => false,
|
|
'status' => 'computing_first_run',
|
|
'check_in_sec' => 90,
|
|
'message' => 'First run computing, wait ~90s and retry'
|
|
]);
|
|
}
|