forked from StellarLend/Stellarlend-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutbox-dispatcher.worker.ts
More file actions
289 lines (259 loc) · 8.31 KB
/
Copy pathoutbox-dispatcher.worker.ts
File metadata and controls
289 lines (259 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { Queue, Worker } from 'bullmq';
import { db } from '@/lib/db/client';
import { outboxEvents } from '@/lib/db/schema';
import { eq, and, or, lt, isNull } from 'drizzle-orm';
import { addNotification } from '@/lib/notifications/repository';
import { logger } from '@/lib/logger';
import crypto from 'crypto';
import {
parseOutboxPayload,
OutboxPayloadValidationError,
NotificationOutboxPayloadSchema,
AuditOutboxPayloadSchema,
} from '@/lib/validation/outbox';
import type { OutboxPayload } from '@/lib/validation/outbox';
const ROUTE = 'jobs/outbox-dispatcher';
const MAX_OUTBOX_RETRY_ATTEMPTS = 3;
const VALID_OUTBOX_TYPES = new Set(['notification', 'audit']);
const MAX_OUTBOX_RETRY_ATTEMPTS = 3;
const VALID_OUTBOX_TYPES = new Set(['notification', 'audit']);
const MAX_OUTBOX_RETRY_ATTEMPTS = 3;
// Redis connection options (pulled from environment)
const connection = {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379', 10),
};
/** Maximum dispatch attempts before an event is left FAILED (bounded retries). */
export const MAX_ATTEMPTS = 3;
/**
* Lease duration for a PROCESSING claim. Events that crash between claim and
* dispatch stay PROCESSING; after the lease expires they are re-claimed, which
* gives safe recovery after partial failures.
*/
export const CLAIM_LEASE_MS = 60_000;
export const BATCH_SIZE = 10;
const LAST_ERROR_MAX_LENGTH = 500;
/**
* Visibility counters for the dispatcher (exported for tests/monitoring).
*/
export const dispatcherMetrics = {
dispatched: 0,
rejected: 0,
failed: 0,
recoveredStale: 0,
};
// Define BullMQ Queues
export const notificationQueue = new Queue('notification-queue', { connection });
export const auditQueue = new Queue('audit-queue', { connection });
function getAttempts(event: { attempts?: number | null } | null | undefined): number {
const value = Number(event?.attempts ?? 0);
return Number.isFinite(value) ? value : 0;
}
/**
* Dispatches a single outbox event to its corresponding BullMQ queue.
*
* Boundary enforcement: the payload is parsed and validated before anything is
* enqueued. Malformed, tampered, or unknown-type events are marked FAILED and
* never reach a queue. Valid events use the outbox event ID as the BullMQ
* jobId to guarantee strict idempotency (at-least-once delivery).
*/
export async function dispatchEvent(event: typeof outboxEvents.$inferSelect) {
if (!event?.id || !event.type || !event.payload) {
throw new Error('Outbox event is missing required fields');
}
const attempts = getAttempts(event);
if (attempts >= MAX_OUTBOX_RETRY_ATTEMPTS) {
await db
.update(outboxEvents)
.set({
status: 'FAILED',
lastError: 'Retry limit reached',
})
.where(eq(outboxEvents.id, event.id));
return;
}
try {
const payload = JSON.parse(event.payload);
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
throw new Error('Outbox payload must be a JSON object');
}
try {
if (event.type === 'notification') {
await notificationQueue.add('send_notification', payload, {
jobId: event.id,
});
} else if (event.type === 'audit') {
await auditQueue.add('log_audit', payload, {
jobId: event.id,
});
}
await db
.update(outboxEvents)
.set({
status: 'COMPLETED',
processedAt: new Date(),
lastError: null,
})
.where(eq(outboxEvents.id, event.id));
} catch (error: any) {
const nextAttempts = Math.min(attempts + 1, MAX_OUTBOX_RETRY_ATTEMPTS);
await db
.update(outboxEvents)
.set({
status: 'FAILED',
attempts: nextAttempts,
lastError: error?.message || String(error),
})
.where(eq(outboxEvents.id, event.id));
}
}
let running = false;
let intervalId: NodeJS.Timeout | null = null;
/**
* Polls the database for events that need dispatch:
* - PENDING events
* - FAILED events with retry attempts remaining (bounded retries)
* - PROCESSING events whose claim lease expired (safe recovery after crashes)
*
* Claimed events are transitioned to PROCESSING with a lease timestamp inside
* a transaction to prevent double dispatch.
*/
export async function processOutbox() {
if (running) return;
running = true;
try {
const staleCutoff = new Date(Date.now() - CLAIM_LEASE_MS);
const events = db.transaction((tx) => {
const pending = tx
.select()
.from(outboxEvents)
.where(
or(
eq(outboxEvents.status, 'PENDING'),
and(
eq(outboxEvents.status, 'FAILED'),
lt(outboxEvents.attempts, MAX_OUTBOX_RETRY_ATTEMPTS)
)
)
)
.limit(BATCH_SIZE)
.all();
if (pending.length === 0) return [];
for (const event of pending) {
const attempts = getAttempts(event);
if (attempts >= MAX_OUTBOX_RETRY_ATTEMPTS && event.status === 'FAILED') {
tx
.update(outboxEvents)
.set({
status: 'FAILED',
lastError: 'Retry limit reached',
})
.where(eq(outboxEvents.id, event.id))
.run();
continue;
}
tx
.update(outboxEvents)
.set({
status: 'PROCESSING',
claimedAt,
lastError: wasStale ? 'recovered stale claim' : null,
})
.where(eq(outboxEvents.id, event.id))
.run();
}
return pending;
});
for (const event of events) {
try {
await dispatchEvent({ ...event, attempts: getAttempts(event) });
} catch (err) {
logger.error('Error dispatching outbox event', 'jobs/outbox-dispatcher', {
eventId: event.id,
error: err instanceof Error ? err.message : String(err),
});
}
}
} catch (err) {
logger.error('Error in outbox dispatcher loop', ROUTE, { error: String(err) });
} finally {
running = false;
}
}
/**
* Starts the polling loop for the outbox dispatcher.
*/
export function startDispatcher(intervalMs = 1000) {
if (intervalId) return;
intervalId = setInterval(processOutbox, intervalMs);
}
/**
* Stops the polling loop.
*/
export function stopDispatcher() {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
}
// ---------------------------------------------------------------------------
// Downstream Queue Consumers (Workers)
// ---------------------------------------------------------------------------
/**
* Notification consumer. Re-validates the job payload at the consumer boundary
* (defense in depth) so a tampered job cannot inject notifications; invalid
* jobs are rejected loudly instead of producing side effects.
*/
export const notificationWorker = new Worker(
'notification-queue',
async (job) => {
const parsed = NotificationOutboxPayloadSchema.safeParse(job.data);
if (!parsed.success) {
const reason = parsed.error.issues
.map((issue) => `${issue.path.join('.')}: ${issue.message}`)
.join('; ');
logger.error('Notification consumer rejected invalid job', ROUTE, {
jobId: job.id,
reason,
});
throw new Error(`Invalid notification job payload: ${reason}`);
}
const { userId, title, message, type, id } = parsed.data;
await addNotification(userId, {
id: id || job.id || crypto.randomUUID(),
title,
message,
type,
read: false,
createdAt: new Date().toISOString(),
});
},
{ connection }
);
/**
* Audit consumer. Re-validates the job payload at the consumer boundary and
* rejects tampered jobs instead of writing them to the audit log.
*/
export const auditWorker = new Worker(
'audit-queue',
async (job) => {
const parsed = AuditOutboxPayloadSchema.safeParse(job.data);
if (!parsed.success) {
const reason = parsed.error.issues
.map((issue) => `${issue.path.join('.')}: ${issue.message}`)
.join('; ');
logger.error('Audit consumer rejected invalid job', ROUTE, {
jobId: job.id,
reason,
});
throw new Error(`Invalid audit job payload: ${reason}`);
}
const { userId, action, details, timestamp } = parsed.data;
logger.info(`AUDIT LOG [${action}]`, 'jobs/consumers', {
userId,
details,
timestamp,
});
},
{ connection }
);