diff options
author | Jonas Kohl | 2024-12-26 20:12:34 +0100 |
---|---|---|
committer | Jonas Kohl | 2024-12-26 20:12:34 +0100 |
commit | 686fff945e0b4697aa74da404ce90534bb7b121d (patch) | |
tree | cb5582c8d118cb480f2978fa821950f4a2259401 /src/application/cron.php | |
parent | 11fed2c8ce3dd38fe686e7b27db738f64373fe3d (diff) |
Add async email and topic subscribingv0.7.2
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; |