fix(notifications): claim a reminder before sending it, give it back when sending fails

The trip and todo reminder ticks sent with `.catch(() => {})` and then
marked the reminder sent whatever happened. A send that threw (a DB error
resolving the recipients) or whose every channel failed (SMTP down, no
in-app) was recorded as delivered and never retried, without a log line.
And because the mark came after the send, two processes running the same
tick (a rolling update overlap, a stray container on the same volume)
both sent.

Each reminder is now claimed first with one conditional UPDATE through
its repository: reminder_sent_for is set only while it differs from the
start date (and the start date is still the one read), reminded_at only
while it is empty or older than the 20 hour dedup window. Only the
process whose update lands sends. If the send throws, or it attempted
in-app and channels and none went out, the claim is given back, the
failure is logged, and the next tick tries again until the trip starts
or the todo falls out of its window. A send nobody wanted (everyone
opted out) stays marked, since a retry would reach no one.

For that, NotificationsService.send() now reports what it attempted and
what was delivered; a channel that rejects or answers false counts as
not delivered. Existing callers ignore the result.
This commit is contained in:
Maurice
2026-10-08 11:42:24 +02:00
parent f527b933fb
commit c04fca81ee
7 changed files with 260 additions and 33 deletions
@@ -222,7 +222,7 @@ export class TodoItemsRepository extends TrekRepository<TodoItems> {
const platform = this.getEntityManager().getPlatform();
const rows = await this.qb('ti')
.join('ti.trip', 't')
.select(['ti.id', columnRef(platform, 'ti.trip_id'), 'ti.name', 'ti.due_date', columnRef(platform, 'ti.assigned_user_id'), 't.title', 't.user'])
.select(['ti.id', columnRef(platform, 'ti.trip_id'), 'ti.name', 'ti.due_date', columnRef(platform, 'ti.assigned_user_id'), 'ti.reminded_at', 't.title', 't.user'])
.andWhere({ checked: 0 })
.andWhere({ due_date: { $ne: null } })
.andWhere({ due_date: { $ne: '' } })
@@ -236,21 +236,32 @@ export class TodoItemsRepository extends TrekRepository<TodoItems> {
name: row.name,
due_date: row.due_date,
assigned_user_id: row.assigned_user_id,
reminded_at: row.reminded_at,
trip_title: row.title,
trip_owner_id: row.user_id,
}));
}
/**
* RJ5 (`reminder-jobs.service.ts#todoTick`, looped) — `UPDATE todo_items
* SET reminded_at = CURRENT_TIMESTAMP WHERE id = ?`. Stays AFTER the
* notification send in call order (plan3f-inputs.md correction #8 — a
* documented, not-fixed-by-this-plan at-least-once ordering, unlike
* `notifications.service.ts#respond`'s deliberately claim-then-act shape).
* RJ5 (`reminder-jobs.service.ts#todoTick`, looped): claims the reminder
* before it is sent. `UPDATE todo_items SET reminded_at = CURRENT_TIMESTAMP
* WHERE id = ? AND (reminded_at IS NULL OR reminded_at <= now - 20 h)`, the
* same dedup bound {@link listDueForReminder} selects by, so of two
* processes running the tick only the one whose update lands sends. True
* when this caller holds the claim.
*/
async markReminded(id: number): Promise<void> {
async claimReminder(id: number): Promise<boolean> {
const platform = this.getEntityManager().getPlatform();
await this.nativeUpdate({ id }, { reminded_at: currentTimestamp(platform) });
const changed = await this.nativeUpdate(
{ id, $or: [{ reminded_at: null }, { reminded_at: { $lte: nowMinusHours(platform, 20) } }] },
{ reminded_at: currentTimestamp(platform) },
);
return changed === 1;
}
/** Gives a claim back after the send failed, so the next tick tries again: `reminded_at` returns to what it was. */
async releaseReminder(id: number, remindedAt: string | null): Promise<void> {
await this.nativeUpdate({ id }, { reminded_at: remindedAt });
}
}
@@ -261,6 +272,7 @@ interface TodoReminderQueryRow {
name: string;
due_date: string;
assigned_user_id: number | null;
reminded_at: string | null;
title: string;
user_id: number;
}
@@ -272,6 +284,8 @@ export interface TodoReminderRow {
name: string;
due_date: string;
assigned_user_id: number | null;
/** The previous reminder's time, what {@link TodoItemsRepository.releaseReminder} restores. */
reminded_at: string | null;
trip_title: string;
trip_owner_id: number;
}
+22 -3
View File
@@ -889,9 +889,28 @@ export class TripsRepository extends TrekRepository<Trips> {
.execute<TripReminderCandidateRow[]>('all', false);
}
/** `UPDATE trips SET reminder_sent_for = ? WHERE id = ?`: the start date the reminder went out for. */
async markReminderSent(id: number, startDate: string): Promise<void> {
await this.nativeUpdate({ id }, { reminder_sent_for: startDate });
/**
* Claims a trip reminder before it is sent: `UPDATE trips SET
* reminder_sent_for = :start WHERE id = ? AND start_date = :start AND
* (reminder_sent_for IS NULL OR reminder_sent_for <> :start)`. Of two
* processes running the tick only the one whose update lands sends, and a
* trip moved since it was read is left for the next tick. True when this
* caller holds the claim.
*/
async claimReminder(id: number, startDate: string): Promise<boolean> {
const changed = await this.nativeUpdate(
{ id, start_date: startDate, $or: [{ reminder_sent_for: null }, { reminder_sent_for: { $ne: startDate } }] },
{ reminder_sent_for: startDate },
);
return changed === 1;
}
/**
* Gives a claim back after the send failed, so the next tick tries again:
* `reminder_sent_for` returns to what it was, unless the row moved on since.
*/
async releaseReminder(id: number, startDate: string, previous: string | null): Promise<void> {
await this.nativeUpdate({ id, reminder_sent_for: startDate }, { reminder_sent_for: previous });
}
// ---------------------------------------------------------------------------