Files

24 lines
846 B
PHP

<?php
declare(strict_types=1);
if (PHP_SAPI !== 'cli') {
http_response_code(404);
exit;
}
if (count($argv) > 2 || (isset($argv[1]) && $argv[1] !== '--force')) {
fwrite(STDERR, "Usage: php tools/poll.php [--force]\n");
exit(1);
}
require dirname(__DIR__, 3) . '/load.php';
try {
$result = $di['mod_service']('mailgateway')->poll(($argv[1] ?? '') === '--force');
echo json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR), PHP_EOL;
$failed = array_filter($result['mailboxes'], fn ($mailbox) => $mailbox['status'] !== 'completed'
|| $mailbox['failed'] > 0 || $mailbox['retry_pending'] > 0 || $mailbox['move_pending'] > 0);
exit($failed === [] ? 0 : 2);
} catch (Throwable) {
fwrite(STDERR, "Mail Gateway polling failed. Check the module tables, configuration, and cron logs.\n");
exit(1);
}