summaryrefslogtreecommitdiff
path: root/src/application/cron.php
blob: 094fe50f0592904548d878b613318bace097c6de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;