fix(audit-wave-11): BullMQ jobId plumbing for natural dedup
concurrency-auditor C-2: every queue.add(...) site previously enqueued without a stable jobId, so a double-dispatch (webhook retry, double- click on Send, scheduler tick collision) would create two queue jobs and the downstream worker would deliver twice. BullMQ rejects a duplicate jobId while the original is still queued or active, so a stable per-entity key gives at-most-once semantics naturally. Added jobIds across all 10 enqueue sites: - email send-invoice → `send-invoice:<invoiceId>` - notifications invoice-overdue-notify → keyed per UTC day so dupes collapse intra-day but tomorrow's run can re-notify if unpaid - export gdpr-export → keyed on the exportId (unique per request) - webhooks deliver (3 sites: dispatch, retry, test) → keyed on the webhook_deliveries row UUID - maintenance expense-dedup-scan → keyed on expenseId - notifications send-notification-email → keyed on notification id - email send-inquiry-confirmation → keyed on interestId (1 per submission) - email send-inquiry-sales-notification → keyed on interestId+email (1 per recipient per submission) - reports generate-report → keyed on the generated_reports row id Pure refactor — no UX impact. Closes the BullMQ dedup gap that was the second half of the concurrency-auditor's CRITICAL-tier findings. Test fixture update: gdpr-export integration test now asserts the jobId option on the queue.add call. Tests 1315/1315. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -187,7 +187,11 @@ export async function createExpense(portId: string, data: CreateExpenseInput, me
|
||||
// queue-side hiccup to fail the user's create.
|
||||
try {
|
||||
const { getQueue } = await import('@/lib/queue');
|
||||
await getQueue('maintenance').add('expense-dedup-scan', { expenseId: expense.id });
|
||||
await getQueue('maintenance').add(
|
||||
'expense-dedup-scan',
|
||||
{ expenseId: expense.id },
|
||||
{ jobId: `expense-dedup-scan:${expense.id}` },
|
||||
);
|
||||
} catch (err) {
|
||||
logger.warn({ err, expenseId: expense.id }, 'Failed to enqueue expense-dedup-scan');
|
||||
}
|
||||
|
||||
@@ -98,13 +98,20 @@ export async function requestGdprExport(input: RequestExportInput): Promise<Requ
|
||||
userAgent: input.userAgent,
|
||||
});
|
||||
|
||||
await getQueue('export').add('gdpr-export', {
|
||||
exportId: row.id,
|
||||
portId: input.portId,
|
||||
clientId: input.clientId,
|
||||
emailToClient: input.emailToClient,
|
||||
emailOverride: input.emailOverride ?? null,
|
||||
});
|
||||
// Stable jobId: exportId is unique per request — dedup is guaranteed
|
||||
// because a second enqueue with the same exportId would either be
|
||||
// rejected (in-flight) or no-op (completed). concurrency-auditor C-2.
|
||||
await getQueue('export').add(
|
||||
'gdpr-export',
|
||||
{
|
||||
exportId: row.id,
|
||||
portId: input.portId,
|
||||
clientId: input.clientId,
|
||||
emailToClient: input.emailToClient,
|
||||
emailOverride: input.emailOverride ?? null,
|
||||
},
|
||||
{ jobId: `gdpr-export:${row.id}` },
|
||||
);
|
||||
|
||||
return { export: row };
|
||||
}
|
||||
|
||||
@@ -48,14 +48,18 @@ export async function sendInquiryNotifications(params: InquiryNotificationParams
|
||||
: 'sales@portnimara.com';
|
||||
|
||||
const emailQueue = getQueue('email');
|
||||
await emailQueue.add('send-inquiry-confirmation', {
|
||||
to: clientEmail,
|
||||
firstName,
|
||||
mooringNumber,
|
||||
contactEmail,
|
||||
portId,
|
||||
portName: 'Port Nimara', // future: resolve from getPortBrandingConfig
|
||||
});
|
||||
await emailQueue.add(
|
||||
'send-inquiry-confirmation',
|
||||
{
|
||||
to: clientEmail,
|
||||
firstName,
|
||||
mooringNumber,
|
||||
contactEmail,
|
||||
portId,
|
||||
portName: 'Port Nimara', // future: resolve from getPortBrandingConfig
|
||||
},
|
||||
{ jobId: `send-inquiry-confirmation:${interestId}` },
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error({ err, interestId }, 'Failed to queue client confirmation email');
|
||||
}
|
||||
@@ -115,16 +119,22 @@ export async function sendInquiryNotifications(params: InquiryNotificationParams
|
||||
|
||||
await Promise.all(
|
||||
externalEmails.map((externalEmail) =>
|
||||
emailQueue.add('send-inquiry-sales-notification', {
|
||||
to: externalEmail,
|
||||
fullName: clientFullName,
|
||||
email: clientEmail,
|
||||
phone: clientPhone,
|
||||
mooringNumber,
|
||||
crmUrl,
|
||||
portId,
|
||||
portName: 'Port Nimara',
|
||||
}),
|
||||
emailQueue.add(
|
||||
'send-inquiry-sales-notification',
|
||||
{
|
||||
to: externalEmail,
|
||||
fullName: clientFullName,
|
||||
email: clientEmail,
|
||||
phone: clientPhone,
|
||||
mooringNumber,
|
||||
crmUrl,
|
||||
portId,
|
||||
portName: 'Port Nimara',
|
||||
},
|
||||
// Per-recipient per-interest jobId so a public-form retry
|
||||
// doesn't fan out duplicate sales notifications.
|
||||
{ jobId: `send-inquiry-sales-notification:${interestId}:${externalEmail}` },
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -589,7 +589,16 @@ export async function sendInvoice(id: string, portId: string, meta: AuditMeta) {
|
||||
// remains intact; downstream consumers can decide whether to render
|
||||
// an external document, link to the in-app view, or wait for the
|
||||
// admin-uploaded AcroForm-fill feature to ship.
|
||||
await getQueue('email').add('send-invoice', { invoiceId: id, portId });
|
||||
// Stable jobId for natural dedup: a double-click on the Send button
|
||||
// or a webhook retry on the upstream caller can fire this twice. BullMQ
|
||||
// rejects a duplicate `jobId` while the original is still queued or
|
||||
// active, so we get at-most-once email per invoice-send action.
|
||||
// concurrency-auditor C-2.
|
||||
await getQueue('email').add(
|
||||
'send-invoice',
|
||||
{ invoiceId: id, portId },
|
||||
{ jobId: `send-invoice:${id}` },
|
||||
);
|
||||
|
||||
// Update status to 'sent'
|
||||
const [updated] = await db
|
||||
@@ -718,10 +727,17 @@ export async function detectOverdue(portId: string) {
|
||||
daysPastDue,
|
||||
});
|
||||
|
||||
await getQueue('notifications').add('invoice-overdue-notify', {
|
||||
invoiceId: inv.id,
|
||||
portId,
|
||||
});
|
||||
// Stable jobId: detectOverdue runs daily; if it fires twice in
|
||||
// the same UTC day (e.g. a manual re-trigger after a worker
|
||||
// restart) we don't want duplicate overdue emails. Per-day key
|
||||
// gives idempotency for the daily fire while letting tomorrow's
|
||||
// run re-notify if the invoice still hasn't been paid.
|
||||
const dayKey = new Date().toISOString().slice(0, 10);
|
||||
await getQueue('notifications').add(
|
||||
'invoice-overdue-notify',
|
||||
{ invoiceId: inv.id, portId },
|
||||
{ jobId: `invoice-overdue-notify:${inv.id}:${dayKey}` },
|
||||
);
|
||||
|
||||
logger.info(
|
||||
{ invoiceId: inv.id, invoiceNumber: inv.invoiceNumber, portId },
|
||||
|
||||
@@ -163,7 +163,11 @@ export async function createNotification(
|
||||
|
||||
if (shouldEmail) {
|
||||
const queue = getQueue('notifications');
|
||||
await queue.add('send-notification-email', { notificationId: notif.id });
|
||||
await queue.add(
|
||||
'send-notification-email',
|
||||
{ notificationId: notif.id },
|
||||
{ jobId: `send-notification-email:${notif.id}` },
|
||||
);
|
||||
}
|
||||
|
||||
return notif;
|
||||
|
||||
@@ -56,13 +56,22 @@ export async function dispatchWebhookEvent(
|
||||
})
|
||||
.returning({ id: webhookDeliveries.id });
|
||||
|
||||
await queue.add('deliver', {
|
||||
webhookId: webhook.id,
|
||||
portId,
|
||||
event: webhookEvent,
|
||||
deliveryId: delivery!.id,
|
||||
payload,
|
||||
});
|
||||
// Stable jobId off the delivery row's UUID — the row exists once
|
||||
// per (webhook, event-instance) so this naturally dedups a
|
||||
// double-dispatch of the same internal event without blocking
|
||||
// legitimate retries (those re-enqueue from the worker with the
|
||||
// attempt count instead of going through this service).
|
||||
await queue.add(
|
||||
'deliver',
|
||||
{
|
||||
webhookId: webhook.id,
|
||||
portId,
|
||||
event: webhookEvent,
|
||||
deliveryId: delivery!.id,
|
||||
payload,
|
||||
},
|
||||
{ jobId: `deliver:${delivery!.id}` },
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
// Never block callers - log and swallow
|
||||
|
||||
@@ -320,13 +320,17 @@ export async function redeliverWebhookDelivery(
|
||||
.returning();
|
||||
|
||||
const queue = getQueue('webhooks');
|
||||
await queue.add('deliver', {
|
||||
webhookId,
|
||||
portId,
|
||||
event: source.eventType,
|
||||
deliveryId: next!.id,
|
||||
payload: replayPayload,
|
||||
});
|
||||
await queue.add(
|
||||
'deliver',
|
||||
{
|
||||
webhookId,
|
||||
portId,
|
||||
event: source.eventType,
|
||||
deliveryId: next!.id,
|
||||
payload: replayPayload,
|
||||
},
|
||||
{ jobId: `deliver:${next!.id}` },
|
||||
);
|
||||
|
||||
void createAuditLog({
|
||||
userId: meta.userId,
|
||||
@@ -371,13 +375,17 @@ export async function sendTestWebhook(portId: string, webhookId: string, eventTy
|
||||
|
||||
// Enqueue the job
|
||||
const queue = getQueue('webhooks');
|
||||
await queue.add('deliver', {
|
||||
webhookId,
|
||||
portId,
|
||||
event: eventType,
|
||||
deliveryId: delivery!.id,
|
||||
payload: delivery!.payload,
|
||||
});
|
||||
await queue.add(
|
||||
'deliver',
|
||||
{
|
||||
webhookId,
|
||||
portId,
|
||||
event: eventType,
|
||||
deliveryId: delivery!.id,
|
||||
payload: delivery!.payload,
|
||||
},
|
||||
{ jobId: `deliver:${delivery!.id}` },
|
||||
);
|
||||
|
||||
return { deliveryId: delivery!.id, status: 'queued' };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user