diff options
Diffstat (limited to 'src/application/cron.php')
-rw-r--r-- | src/application/cron.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/application/cron.php b/src/application/cron.php new file mode 100644 index 0000000..094fe50 --- /dev/null +++ b/src/application/cron.php @@ -0,0 +1,42 @@ +<?php + +const __ROOT__ = __DIR__ . "/.."; + +require_once __ROOT__ . "/vendor/autoload.php"; +require_once __ROOT__ . "/application/i18n.php"; +require_once __ROOT__ . "/application/common.php"; + +function print_error(string $msg): void { + file_put_contents("php://stderr", $msg); +} + +function check_sapi(): void { + if (PHP_SAPI !== "cli") { + http_response_code(500); + echo "Error: Can only be called via command line\n"; + exit(1); + } +} + +check_sapi(); + +$job = $argv[1] ?? null; + +if ($job === null) { + print_error("Error: No job specified\nUsage: php cron.php <job>\n"); + exit(1); +} + +$jobsDir = __DIR__ . "/jobs"; + +$availableJobs = array_map(fn($i) => pathinfo($i, PATHINFO_FILENAME), array_values(array_filter(scandir($jobsDir), + fn($i) => is_file($jobsDir . "/" . $i) && $i[0] !== "." && pathinfo($i, PATHINFO_EXTENSION) === "php"))); + +if (!in_array($job, $availableJobs)) { + print_error("Error: Invalid job specified\nAvailable jobs are:\n" . implode("", array_map(fn($i) => " $i\n", $availableJobs))); + exit(1); +} + +$jobFile = $jobsDir . "/" . $job . ".php"; + +include $jobFile; |