Files
radio-free-elsewhere/cli/poll_icecast.php
T
2026-10-30 08:37:10 -05:00

35 lines
1.0 KiB
PHP

<?php
// Polls Icecast and records track history. Run from cron once a minute; each run polls
// four times, 15 seconds apart:
// * * * * * php /path/to/rfe/cli/poll_icecast.php
if (PHP_SAPI !== 'cli') {
http_response_code(403);
exit;
}
require __DIR__ . '/../includes/db.php';
require __DIR__ . '/../includes/broadcasts.php';
// Skip this run if the previous one is still going. Locks this script rather than a file in /tmp,
// which another user (e.g. a manual run over SSH) could own and leave unopenable for cron.
$lock = fopen(__FILE__, 'r');
if ($lock === false) {
fwrite(STDERR, date('c') . " Unable to open lock\n");
exit(1);
}
if (!flock($lock, LOCK_EX | LOCK_NB)) exit;
for ($i = 0; $i < 4; $i++) {
if ($i > 0) sleep(15);
$track = fetchCurrentTrack();
if ($track === false) continue; // Status API unreachable; don't treat it as the stream ending
try {
recordPoll(getDB(), $track);
} catch (Throwable $e) {
fwrite(STDERR, date('c') . ' ' . $e->getMessage() . "\n");
}
}