[Web][Rspamd] Keep the global archive switch in Redis only

This commit is contained in:
FreddleSpl0it committed 2026-11-09 01:09:43 +01:00
1 parent 2fc6868b72
commit c48691fca7
15 files changed
+78 -55

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ catch (PDOException $e) {
http_response_code(501);
exit;
}
// Init Redis, only to mirror the switch rspamd reads
// Init Redis, it keeps the global switch; without it nothing is archived
$redis = new Redis();
try {
$redis->connect('redis-mailcow', 6379);
+3 -7
View File
@@ -1072,13 +1072,12 @@ rspamd_config:register_symbol({
end
local function enabled_cb(err, data)
if not err and type(data) == 'string' and data == '0' then
return
if not err and type(data) == 'string' and data == '1' then
archive()
end
archive()
end
local ret = rspamd_redis_make_request(task,
rspamd_redis_make_request(task,
redis_params, -- connect params
'ARCHIVE_ENABLED', -- hash key
false, -- is write
@@ -1086,8 +1085,5 @@ rspamd_config:register_symbol({
'GET', -- command
{'ARCHIVE_ENABLED'} -- arguments
)
if not ret then
archive()
end
end
})
@@ -51,7 +51,7 @@ class ArchiveModule
$settings = new SettingsService($settingsRepo, $lookup, $state, $smtp, $context);
$journal = new JournalService($settingsRepo, new OwnerResolver($lookup), new HeaderBuilder(), $smtp, $state, $context);
$status = new StatusService($settingsRepo, $state, new LegacyQueue(), $context);
$mailboxes = new MailboxListService($lookup, $settingsRepo, $context);
$mailboxes = new MailboxListService($lookup, $state, $context);
$api = new ArchiveApiController($settings, $status, $journal, $mailboxes, new ApiResponse(self::SECRET_KEYS));
return new self($settings, $journal, $status, $mailboxes, $api);
}
@@ -5,10 +5,11 @@ declare(strict_types=1);
namespace Mailcow\Archive\Redis;
use Mailcow\Archive\Model\Settings;
use RuntimeException;
/**
* Redis side of the archive:
* - ARCHIVE_ENABLED: the switch rspamd reads before it asks for owners, mirrored from the settings.
* - ARCHIVE_ENABLED: the global switch, kept here only; rspamd reads it before it asks for owners.
* - ARCHIVE_DROPPED / ARCHIVE_DROPPED_LOG: copies that could not be handed to Postfix (rspamd.local.lua, release
* hooks), counted and the last 100 listed.
* - POSTFIX_MAILLOG: the Postfix log, read for the archive log: deliveries of the archive_smtp service and expired
@@ -33,6 +34,16 @@ class ArchiveState
$this->redis?->set(self::ENABLED, $enabled ? '1' : '0');
}
/** The global switch: on only while ARCHIVE_ENABLED is '1', as rspamd reads it (missing or unreadable is off) */
public function enabled(): bool
{
try {
return $this->redis?->get(self::ENABLED) === '1';
} catch (RuntimeException) {
return false;
}
}
/** A mail was not archived: its copy could not be handed to Postfix */
public function dropped(string $qid, string $reason, string $messageId = ''): void
{
@@ -19,7 +19,6 @@ class SettingsRepository extends Repository
{
$rows = $this->query("SELECT `name`, `value` FROM `archive_settings`")->fetchAll(PDO::FETCH_KEY_PAIR);
$settings = new Settings();
$settings->enabled = ($rows['enabled'] ?? '0') === '1';
$settings->address = (string)($rows['address'] ?? '');
$settings->host = (string)($rows['host'] ?? '');
$settings->port = (int)($rows['port'] ?? $settings->port);
@@ -40,7 +39,6 @@ class SettingsRepository extends Repository
public function save(Settings $settings): void
{
$values = [
'enabled' => $settings->enabled ? '1' : '0',
'address' => $settings->address,
'host' => $settings->host,
'port' => (string)$settings->port,
@@ -48,8 +48,7 @@ class JournalService
public function prepare(array $request): array
{
$settings = $this->settings->load();
$this->state->setEnabled($settings->enabled);
if (!$settings->enabled || !$settings->canDeliver()) {
if (!$this->state->enabled() || !$settings->canDeliver()) {
return ['archive' => false];
}
$from = self::text($request['from'] ?? '');
@@ -75,7 +74,7 @@ class JournalService
{
try {
$settings = $this->settings->load();
if (!$settings->enabled || !$settings->canDeliver()) {
if (!$this->state->enabled() || !$settings->canDeliver()) {
return false;
}
$resolved = $this->resolver->resolve($from, $rcpts, '', false);
@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Mailcow\Archive\Service;
use Mailcow\Archive\Model\Mailbox;
use Mailcow\Archive\Redis\ArchiveState;
use Mailcow\Archive\Repository\LookupRepository;
use Mailcow\Archive\Repository\SettingsRepository;
use Mailcow\Core\AccessDenied;
use Mailcow\Core\Context;
@@ -16,7 +16,7 @@ use Mailcow\Core\Context;
*/
class MailboxListService
{
public function __construct(private LookupRepository $lookup, private SettingsRepository $settings, private Context $context)
public function __construct(private LookupRepository $lookup, private ArchiveState $state, private Context $context)
{
}
@@ -29,7 +29,7 @@ class MailboxListService
public function archived(): array
{
$this->context->requireAdmin(false);
$enabled = $this->settings->load()->enabled;
$enabled = $this->state->enabled();
return [
'enabled' => (int)$enabled,
'mailboxes' => $enabled ? array_map(fn(Mailbox $mailbox): array => $mailbox->toArray(), $this->lookup->archivedMailboxes()) : [],
@@ -14,7 +14,6 @@ use Mailcow\Core\AccessDenied;
use Mailcow\Core\Context;
use Mailcow\Core\Input;
use Mailcow\Core\UserError;
use PDOException;
/**
* Admin use cases of the archive target. Read-only API keys may read the settings (never password or key).
@@ -34,17 +33,15 @@ class SettingsService
public function get(): Settings
{
$this->context->requireAdmin(false);
return $this->repo->load();
$settings = $this->repo->load();
$settings->enabled = $this->state->enabled();
return $settings;
}
/** Global switch for internal callers (page templates, mailbox details), no permission check; off while the table is missing */
/** Global switch for internal callers (page templates, mailbox details), no permission check */
public function enabled(): bool
{
try {
return $this->repo->load()->enabled;
} catch (PDOException) {
return false;
}
return $this->state->enabled();
}
/**
@@ -57,6 +54,7 @@ class SettingsService
{
$this->context->requireAdmin();
$settings = $this->repo->load();
$settings->enabled = $this->state->enabled();
if (array_key_exists('address', $input)) {
$address = strtolower((string)Input::text($input['address']));
if (!is_scalar($input['address']) || ($address !== '' && !filter_var($address, FILTER_VALIDATE_EMAIL))) {
@@ -58,7 +58,7 @@ class StatusService
}
}
return [
'enabled' => (int)$settings->enabled,
'enabled' => (int)$this->state->enabled(),
'configured' => (int)$settings->canDeliver(),
'queued' => count($copies),
'queued_bytes' => $bytes,