Files

110 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
use OCA\QortalFilesBridge\AppInfo\Application;
use OCA\QortalFilesBridge\Service\FilesPublishService;
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,
'publish_queue_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,
'publish_queue_runtime',
(string)json_encode($runtime, JSON_UNESCAPED_SLASHES)
);
/** @var FilesPublishService $queueService */
$queueService = \OC::$server->query(FilesPublishService::class);
$queueService->processPublishQueueCycle();
$runtime['lastStatus'] = 'ok';
$runtime['lastError'] = '';
$runtime['lastSuccessAt'] = gmdate('c');
$runtime['lastSuccessAtEpoch'] = time();
$config->setAppValue(
Application::APP_ID,
'publish_queue_runtime',
(string)json_encode($runtime, JSON_UNESCAPED_SLASHES)
);
} catch (\Throwable $e) {
try {
$config = \OC::$server->getConfig();
$runtimeRaw = trim((string)$config->getAppValue(
Application::APP_ID,
'publish_queue_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,
'publish_queue_runtime',
(string)json_encode($runtime, JSON_UNESCAPED_SLASHES)
);
} catch (\Throwable) {
// ignore runtime persistence errors
}
try {
\OC::$server->getLogger()->warning('Qortal Files bridge direct cron publish queue fallback failed', [
'app' => Application::APP_ID,
'error' => $e->getMessage(),
]);
} catch (\Throwable) {
// no-op
}
exit(1);
}
exit(0);