Files
kaffeekasse-saas/bin/cron.php
T

45 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
$rootPath = dirname(__DIR__);
$lockFile = $rootPath . '/storage/cron.lock';
if (is_file($lockFile) && (time() - filemtime($lockFile)) < 600) {
fwrite(STDOUT, "Cron already running.\n");
exit(0);
}
file_put_contents($lockFile, (string) time(), LOCK_EX);
try {
$app = require $rootPath . '/app/bootstrap.php';
if (!$app['database']->isConfigured()) {
throw new RuntimeException('Database config missing.');
}
$pdo = $app['database']->pdo();
$pendingRfid = (int) $pdo->query(
'SELECT COUNT(*) FROM rfid_events WHERE status = "received"'
)->fetchColumn();
$summary = [
'timestamp' => gmdate(DATE_ATOM),
'pending_rfid_events' => $pendingRfid,
'notes' => [
'In diesem MVP dient Cron als technischer Haken fuer spaetere Reminder, Exporte und RFID-Regeln.',
],
];
fwrite(STDOUT, json_encode($summary, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);
} catch (\Throwable $throwable) {
fwrite(STDERR, $throwable->getMessage() . PHP_EOL);
exit(1);
} finally {
if (is_file($lockFile)) {
unlink($lockFile);
}
}