$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); }