JA
apps/caramail-server / src / Command / StatusFile.php · commit 070afda
<?phpnamespace App\Command;/** What a running server writes every second (status.json in its data folder), for the status command. */final class StatusFile{    public function __construct(private readonly string $file)    {    }    public function write(array $stats): void    {        $stats['pid'] = getmypid();        $stats['at'] = time();        @file_put_contents($this->file, json_encode($stats, JSON_UNESCAPED_UNICODE) . "\n");    }    /** @return array<string, mixed>|null null when no server wrote it lately */    public function read(int $maxAge = 10): ?array    {        $data = is_file($this->file) ? json_decode((string)file_get_contents($this->file), true) : null;        if (!is_array($data) || (time() - (int)($data['at'] ?? 0)) > $maxAge) {            return null;        }        return $data;    }    public function remove(): void    {        @unlink($this->file);    }}