Files
2026-03-05 09:40:09 -08:00

138 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
use OCA\QortalTalkBridge\AppInfo\Application;
use OCA\QortalTalkBridge\Service\TalkBridgeRelayService;
if (!defined('OC_CONSOLE')) {
define('OC_CONSOLE', true);
}
$basePath = realpath(__DIR__ . '/../../');
if (!is_string($basePath) || $basePath === '') {
exit(0);
}
$baseFile = $basePath . '/lib/base.php';
if (!is_file($baseFile)) {
exit(0);
}
require_once $baseFile;
try {
$config = \OC::$server->getConfig();
$runtimeRaw = trim((string)$config->getAppValue(
Application::APP_ID,
'poll_job_runtime',
'{}'
));
$runtime = [];
if ($runtimeRaw !== '') {
try {
$decoded = json_decode($runtimeRaw, true, 512, JSON_THROW_ON_ERROR);
if (is_array($decoded)) {
$runtime = $decoded;
}
} catch (\Throwable) {
$runtime = [];
}
}
$attemptEpoch = time();
$runtime['attemptCount'] = max(0, (int)($runtime['attemptCount'] ?? 0)) + 1;
$runtime['lastAttemptAt'] = gmdate('c', $attemptEpoch);
$runtime['lastAttemptAtEpoch'] = $attemptEpoch;
$runtime['lastStatus'] = 'running';
$runtime['lastError'] = '';
$config->setAppValue(
Application::APP_ID,
'poll_job_runtime',
(string)json_encode($runtime, JSON_UNESCAPED_SLASHES)
);
$relayEnabled = trim((string)$config->getAppValue(
Application::APP_ID,
'server_relay_enabled',
'1'
)) === '1';
$senderUser = trim((string)$config->getAppValue(
Application::APP_ID,
'bridge_sender_user',
''
));
$senderPassword = trim((string)$config->getAppValue(
Application::APP_ID,
'bridge_sender_app_password',
''
));
if (!$relayEnabled || $senderUser === '' || $senderPassword === '') {
$runtime['lastStatus'] = 'skipped';
$runtime['lastError'] = !$relayEnabled
? 'Server relay is disabled'
: 'Bridge sender credentials are not configured';
$config->setAppValue(
Application::APP_ID,
'poll_job_runtime',
(string)json_encode($runtime, JSON_UNESCAPED_SLASHES)
);
exit(0);
}
/** @var TalkBridgeRelayService $relayService */
$relayService = \OC::$server->query(TalkBridgeRelayService::class);
$relayService->runCycle();
$runtime['lastStatus'] = 'ok';
$runtime['lastError'] = '';
$runtime['lastSuccessAt'] = gmdate('c');
$runtime['lastSuccessAtEpoch'] = time();
$config->setAppValue(
Application::APP_ID,
'poll_job_runtime',
(string)json_encode($runtime, JSON_UNESCAPED_SLASHES)
);
} catch (\Throwable $e) {
try {
$config = \OC::$server->getConfig();
$runtimeRaw = trim((string)$config->getAppValue(
Application::APP_ID,
'poll_job_runtime',
'{}'
));
$runtime = [];
if ($runtimeRaw !== '') {
try {
$decoded = json_decode($runtimeRaw, true, 512, JSON_THROW_ON_ERROR);
if (is_array($decoded)) {
$runtime = $decoded;
}
} catch (\Throwable) {
$runtime = [];
}
}
$runtime['lastStatus'] = 'failed';
$runtime['lastError'] = $e->getMessage();
$runtime['lastFailureAt'] = gmdate('c');
$runtime['lastFailureAtEpoch'] = time();
$config->setAppValue(
Application::APP_ID,
'poll_job_runtime',
(string)json_encode($runtime, JSON_UNESCAPED_SLASHES)
);
} catch (\Throwable) {
// ignore runtime persistence errors
}
try {
\OC::$server->getLogger()->warning('Qortal Talk bridge direct cron relay fallback failed', [
'app' => Application::APP_ID,
'error' => $e->getMessage(),
]);
} catch (\Throwable) {
// no-op
}
exit(1);
}
exit(0);