Quelltext des aktuellen Standes
<?php
/**
* Per-task worker — invoked by bin/scheduler.php (or BackgroundDispatcher)
* with two arguments:
*
* php run-task.php <task_name> <run_id>
*
* The scheduler has already inserted the cronjob_runs row (status=running)
* and updated cron_tasks.next_run_at. This script's job is to actually run
* the handler and write the final outcome.
*
* Errors here MUST NOT bubble up — we always write a finished row to
* cronjob_runs so the admin UI shows the state.
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../bootstrap.php';
use OpenNIT\Cron\BackgroundDispatcher;
use OpenNIT\Cron\Bootstrap;
use OpenNIT\Cron\Scheduler;
use OpenNIT\Cron\TaskRegistry;
$rootDir = dirname(__DIR__);
if ($argc < 3) {
fwrite(STDERR, "Usage: run-task.php <task_name> <run_id>\n");
exit(2);
}
$taskName = (string)$argv[1];
$runId = (int)$argv[2];
if ($runId <= 0) {
fwrite(STDERR, "[run-task] invalid run_id: " . $argv[2] . PHP_EOL);
exit(2);
}
try {
$context = Bootstrap::init($rootDir);
} catch (\Throwable $e) {
fwrite(STDERR, "[run-task] bootstrap failed: " . $e->getMessage() . PHP_EOL);
exit(1);
}
$lockDir = $rootDir . '/storage/locks';
$registry = new TaskRegistry();
$dispatcher = new BackgroundDispatcher(
phpBinary: PHP_BINARY,
workerScript: __FILE__,
logFile: $rootDir . '/storage/logs/scheduler.log',
);
$scheduler = new Scheduler($context->db, $context, $registry, $dispatcher, $lockDir);
$ts = date('Y-m-d H:i:s');
echo "[{$ts}] worker start: task={$taskName} run_id={$runId}" . PHP_EOL;
try {
$scheduler->executeInWorker($taskName, $runId);
} catch (\Throwable $e) {
fwrite(STDERR, "[run-task] uncaught: " . $e->getMessage() . PHP_EOL);
// Best-effort: try to record the failure even though Scheduler should
// have done that already.
try {
$context->db->update('cronjob_runs', [
'finished_at' => date('Y-m-d H:i:s'),
'status' => 'error',
'message' => substr('Uncaught: ' . $e->getMessage(), 0, 1000),
], 'id = ?', [$runId]);
} catch (\Throwable) {}
exit(1);
}
$ts = date('Y-m-d H:i:s');
echo "[{$ts}] worker done: task={$taskName} run_id={$runId}" . PHP_EOL;
exit(0);