fix(mail-ingest): never queue a mail again for a failure after its commit

An error in the announcements, documents or notices after a mail was written sent it back to the queue, which duplicated its entries or marked it failed.
This commit is contained in:
Maurice
2026-11-23 14:56:19 +01:00
parent 303d161ecf
commit 4cef885e12
2 changed files with 34 additions and 4 deletions
@@ -142,7 +142,7 @@ export class MailIngestProcessService {
}
if (kind.kind === 'forwarding_confirmation') {
await this.keepForInbox(message, { ...outcome, forwardingCode: kind.code }, 'forwarding_confirmation', now);
await this.notify.bookingsPending(person.id, await this.candidates.countPending(person.id));
await this.afterRead(message.id, () => this.pendingNotice(person.id));
return;
}
await this.readBookings(message, raw, mail, person, outcome, now);
@@ -171,7 +171,7 @@ export class MailIngestProcessService {
return;
}
await this.keepForInbox(message, done, read.emptyReason, now);
await this.notify.bookingsPending(person.id, await this.candidates.countPending(person.id));
await this.afterRead(message.id, () => this.pendingNotice(person.id));
return;
}
const context: CheckContext = {
@@ -184,7 +184,20 @@ export class MailIngestProcessService {
};
const planned = await this.plan(read.drafts, context, person, prefs);
const added = await this.write(message, planned, done, now);
await this.afterCommit(added, planned, mail, person);
await this.afterRead(message.id, () => this.afterCommit(added, planned, mail, person));
}
/** What follows a mail's commit fails on its own: the mail is read, so it is never queued again for it. */
private async afterRead(messageId: number, step: () => Promise<void>): Promise<void> {
try {
await step();
} catch (err) {
logError(`[mail-ingest] After reading mail ${messageId}: ${err instanceof Error ? err.message : String(err)}`);
}
}
private async pendingNotice(userId: number): Promise<void> {
await this.notify.bookingsPending(userId, await this.candidates.countPending(userId));
}
/** The trips a booking of this mail may go to: the address's trip alone, or the person's editable ones. */
@@ -376,7 +389,7 @@ export class MailIngestProcessService {
});
}, Promise.resolve());
if (planned.some((booking) => booking.resolution === 'pending' && booking.prepared === null)) {
await this.notify.bookingsPending(person.id, await this.candidates.countPending(person.id));
await this.pendingNotice(person.id);
}
}
@@ -602,6 +602,23 @@ describe('what a mail names', () => {
});
describe('a mail that keeps failing', () => {
it('is not read again when what follows its commit fails', async () => {
const user = await person();
const trip = await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' });
const announce = vi
.spyOn(t.get(MailIngestApplyService), 'announce')
.mockRejectedValueOnce(new Error('connection lost'));
try {
const messageId = await forward(user.id, eml('flight'));
expect(await findRow(orm(), MailIngestMessages, { id: messageId })).toMatchObject({ status: 'processed' });
expect(await job().tick(new Date(NOW.getTime() + 60 * 60 * 1000))).toBe(0);
expect(await entriesOf(messageId)).toHaveLength(1);
expect(await countRows(orm(), Reservations, { trip: trip.id })).toBe(1);
} finally {
announce.mockRestore();
}
});
it('is tried again later and given up after its last try', async () => {
const user = await person();
await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' });