103 lines
3.0 KiB
PHP
103 lines
3.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Zentrale Telegram-Cron-Datei fuer wiederkehrende Aufgaben.
|
|
*
|
|
* Diese Datei ist der schmale Einstiegspunkt fuer den kompletten Telegram-Cron.
|
|
* Sie laedt Bootstrap und Task-Definitionen, setzt den Lock gegen Doppelstarts,
|
|
* fuehrt faellige Tasks aus und protokolliert den gesamten Lauf in app_import_runs.
|
|
*
|
|
* Empfohlener Cron:
|
|
* jede Minute aufrufen
|
|
*
|
|
* Beispiel Windows Aufgabenplanung:
|
|
* C:\laragon\bin\php\php-8.4.5-nts-Win32-vs17-x64\php.exe
|
|
* C:\Users\ich\source\repos\skyview.astronomiemuseum.de\scripts\telegram\telegram_cron.php
|
|
*/
|
|
|
|
const IMPORT_RUN_SOURCE = 'telegram-cron';
|
|
|
|
[
|
|
'stateFile' => $stateFile,
|
|
'telegramConfig' => $telegramConfig,
|
|
'pdo' => $pdo,
|
|
'nowBerlin' => $nowBerlin,
|
|
] = require __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php';
|
|
|
|
$tasks = require __DIR__ . DIRECTORY_SEPARATOR . 'tasks.php';
|
|
|
|
$lockHandle = openCronLock();
|
|
if ($lockHandle === null) {
|
|
writeMessage('Telegram-Cron laeuft bereits. Dieser Start wird beendet.', true);
|
|
exit(1);
|
|
}
|
|
|
|
$runId = startImportRun($pdo, IMPORT_RUN_SOURCE, null);
|
|
$recordsReceived = count($tasks);
|
|
$recordsInserted = 0;
|
|
$recordsUpdated = 0;
|
|
$messages = [];
|
|
|
|
try {
|
|
foreach ($tasks as $task) {
|
|
if (($task['enabled'] ?? false) !== true) {
|
|
$messages[] = sprintf('[%s] deaktiviert', (string) $task['key']);
|
|
continue;
|
|
}
|
|
|
|
$schedule = (string) ($task['schedule'] ?? '');
|
|
if (!isTaskDue($schedule, $nowBerlin)) {
|
|
$messages[] = sprintf('[%s] noch nicht faellig (%s)', (string) $task['key'], $schedule);
|
|
continue;
|
|
}
|
|
|
|
$messages[] = sprintf('[%s] gestartet (%s)', (string) $task['key'], (string) ($task['label'] ?? $task['key']));
|
|
|
|
$handler = $task['handler'] ?? null;
|
|
if (!is_callable($handler)) {
|
|
throw new RuntimeException('Task-Handler ist nicht aufrufbar: ' . (string) $task['key']);
|
|
}
|
|
|
|
$result = $handler($pdo, $telegramConfig, $nowBerlin);
|
|
$recordsInserted += (int) ($result['sent'] ?? 0);
|
|
$recordsUpdated += (int) ($result['updated'] ?? 0);
|
|
$messages[] = sprintf(
|
|
'[%s] ok - %s',
|
|
(string) $task['key'],
|
|
trim((string) ($result['message'] ?? 'ohne Rueckmeldung'))
|
|
);
|
|
}
|
|
|
|
finishImportRun(
|
|
$pdo,
|
|
$runId,
|
|
$recordsReceived,
|
|
$recordsInserted,
|
|
$recordsUpdated,
|
|
'success',
|
|
implode("\n", $messages)
|
|
);
|
|
|
|
writeMessage('Telegram-Cron abgeschlossen.');
|
|
foreach ($messages as $message) {
|
|
writeMessage($message);
|
|
}
|
|
} catch (Throwable $exception) {
|
|
finishImportRun(
|
|
$pdo,
|
|
$runId,
|
|
$recordsReceived,
|
|
$recordsInserted,
|
|
$recordsUpdated,
|
|
'error',
|
|
$exception->getMessage()
|
|
);
|
|
|
|
writeMessage('Telegram-Cron mit Fehler beendet: ' . $exception->getMessage(), true);
|
|
exit(1);
|
|
} finally {
|
|
flock($lockHandle, LOCK_UN);
|
|
fclose($lockHandle);
|
|
}
|