fix(mail-ingest): read a mail as soon as a transport takes it in

Only an upload started a processing run; a forwarded mail waited for the
next two-minute tick, and one that came in right after a run waited for two.
The IMAP and relay runs start one run once they took anything in, the
webhook once per mail it accepted.
This commit is contained in:
Maurice
2026-11-13 21:05:54 +01:00
parent a67361c6bd
commit 6ac7bd2d96
5 changed files with 25 additions and 1 deletions
@@ -10,6 +10,7 @@ import {
type ImapSession,
} from './mail-ingest-imap.client';
import { isPassingRefusal, MailIngestIntakeService } from './mail-ingest-intake.service';
import { MailIngestProcessJob } from './mail-ingest-process.job';
import { MailIngestSettingsService, type MailIngestImapConfig } from './mail-ingest-settings.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
@@ -51,6 +52,7 @@ export class MailIngestImapJob implements OnApplicationBootstrap {
private readonly intake: MailIngestIntakeService,
private readonly imap: MailIngestImapClient,
private readonly registrar: CronRegistrarService,
private readonly process: MailIngestProcessJob,
) {}
onApplicationBootstrap(): void {
@@ -76,6 +78,8 @@ export class MailIngestImapJob implements OnApplicationBootstrap {
try {
const { taken, uidValidity, lastUid } = await this.poll(config, now);
await this.cursor.recordOk('imap', config.mailbox, dbNow(new Date()), { uidValidity, lastUid });
// Read what came in now, not at the next processing tick.
if (taken > 0) this.process.runSoon();
return taken;
} catch (err) {
const reason = describeImapFailure(err);
@@ -75,6 +75,8 @@ export class MailIngestProcessJob implements OnApplicationBootstrap {
* Starts a run now instead of at the next tick, without waiting for it.
* Where the crons are off (the test suites) nothing starts, as no tick
* would either, so a run never outlives the request that asked for it.
*
* @txStandalone a run of its own, not awaited, under its own lease; nothing of the caller's writes rides on it.
*/
runSoon(): void {
if (!this.registrar.isEnabled()) return;
@@ -4,6 +4,7 @@ import { dbNow } from '../../db/types';
import { logError, logWarn } from '../audit/audit-log.logger';
import { CronRegistrarService, LEASE_OWNER } from '../scheduling/cron-registrar.service';
import { isPassingRefusal, MailIngestIntakeService } from './mail-ingest-intake.service';
import { MailIngestProcessJob } from './mail-ingest-process.job';
import { MailIngestRelayHost, type MailInboxRelay } from './mail-ingest-relay.service';
import { MailIngestSettingsService } from './mail-ingest-settings.service';
import { MAIL_INGEST_LIMITS } from './mail-ingest.constants';
@@ -69,6 +70,7 @@ export class MailIngestRelayJob implements OnApplicationBootstrap {
private readonly intake: MailIngestIntakeService,
private readonly relays: MailIngestRelayHost,
private readonly registrar: CronRegistrarService,
private readonly process: MailIngestProcessJob,
) {}
onApplicationBootstrap(): void {
@@ -91,6 +93,8 @@ export class MailIngestRelayJob implements OnApplicationBootstrap {
try {
const taken = await this.drain(relay, null, PAGES_PER_RUN);
await this.cursor.recordOk('cloud', '', dbNow(new Date()));
// Read what came in now, not at the next processing tick.
if (taken > 0) this.process.runSoon();
return taken;
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
@@ -1,5 +1,6 @@
import { DomainError } from '../common/domain-error';
import { MailIngestIntakeService, type MailIntakeRefusal } from './mail-ingest-intake.service';
import { MailIngestProcessJob } from './mail-ingest-process.job';
import { MailIngestSettingsService } from './mail-ingest-settings.service';
import { Injectable } from '@nestjs/common';
import type { MailIngestInboundResponse } from '@trek/shared';
@@ -45,6 +46,7 @@ export class MailIngestWebhookService {
constructor(
private readonly settings: MailIngestSettingsService,
private readonly intake: MailIngestIntakeService,
private readonly process: MailIngestProcessJob,
) {}
/**
@@ -92,6 +94,8 @@ export class MailIngestWebhookService {
const [status, error] = REFUSALS[result.reason];
throw new DomainError(status, error);
}
// Read it now, not at the next processing tick.
if (result.status === 'accepted') this.process.runSoon();
return { accepted: true, duplicate: result.status === 'duplicate' };
}
}