-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathjobs.ts
More file actions
335 lines (286 loc) · 10.9 KB
/
Copy pathjobs.ts
File metadata and controls
335 lines (286 loc) · 10.9 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* @module api/jobs
*
* Background job orchestration for webhook delivery and DLQ management.
*
* ## Responsibilities
* - Initialize the DLQ store (in-memory or Redis-backed).
* - Start the DLQ metrics sampling loop.
* - Expose authenticated endpoints for idempotent DLQ message replay.
*
* ## Configuration (environment variables)
* | Variable | Default | Description |
* |---------------------------|---------|------------------------------------------------|
* | `DLQ_METRICS_INTERVAL_MS` | `30000` | DLQ metrics sampling interval in milliseconds. |
*
* ## Usage
* Call {@link initializeJobs} once at application startup (e.g., from `index.ts`).
*/
import axios from 'axios';
import { Router, Request, Response, NextFunction } from 'express';
import { startDlqMetricsSampling, incrementDlqReplay } from '../webhookMetrics';
import { redactPayload } from '../utils/redact';
import { IdempotencyLayer } from '../events/idempotency';
import { requireAuth, requireRole } from '../middleware/authorization';
// ---------------------------------------------------------------------------
// Request context propagation
// ---------------------------------------------------------------------------
/** Context envelope propagated to asynchronous processors (e.g., webhook calls). */
export interface RequestContextEnvelope {
requestId?: string;
tenantId?: string;
actorId?: string;
}
const MAX_CONTEXT_FIELD_LENGTH = 128;
function sanitizeContextValue(value: unknown): string | undefined {
const raw = Array.isArray(value) ? value.find((v): v is string => typeof v === 'string') : value;
if (typeof raw !== 'string') return undefined;
const trimmed = raw.trim();
if (trimmed.length === 0 || trimmed.length > MAX_CONTEXT_FIELD_LENGTH) return undefined;
// Prevent header injection and other control-character issues.
if (/[\u0000-\u001f\u007f]/.test(trimmed)) return undefined;
return trimmed;
}
/**
* Extract a validated context envelope from the incoming request.
* Unknown, missing, or malformed values are omitted rather than propagated.
*/
export function extractRequestContext(req: Request): RequestContextEnvelope {
const context: RequestContextEnvelope = {};
const requestId = sanitizeContextValue(req.headers['x-request-id'] ?? (req as any).id);
if (requestId) context.requestId = requestId;
const tenantId = sanitizeContextValue(req.headers['x-tenant-id'] ?? (req as any).tenantId);
if (tenantId) context.tenantId = tenantId;
const actorId = sanitizeContextValue((req as any).user?.id ?? req.headers['x-actor-id']);
if (actorId) context.actorId = actorId;
return context;
}
// ---------------------------------------------------------------------------
// Store contract
// ---------------------------------------------------------------------------
/** A single replayable DLQ record as consumed by the replay endpoints. */
export interface ReplayableDlqItem {
id: string;
eventId: string;
targetUrl: string;
payload: Record<string, unknown>;
}
/**
* Minimal store contract required by the DLQ replay endpoints. Implementations
* may be in-memory (development/testing) or backed by Redis/SQLite.
*/
export interface ReplayableDlqStore {
getEntryById(id: string): Promise<ReplayableDlqItem | null> | ReplayableDlqItem | null;
removeEntry(id: string): Promise<void> | void;
incrementReplayAttempts(id: string): Promise<void> | void;
}
// ---------------------------------------------------------------------------
// Module-level state
// ---------------------------------------------------------------------------
let dlqStore: ReplayableDlqStore | null = null;
let stopSampling: (() => void) | null = null;
const router = Router();
/**
* Deliver a raw DLQ payload to its target URL.
*
* @returns `true` when the destination responded with a 2xx status.
*/
async function deliverRaw(
targetUrl: string,
eventId: string,
payload: Record<string, unknown>,
context: RequestContextEnvelope = {},
): Promise<boolean> {
try {
const headers: Record<string, string> = { 'X-Event-Id': eventId };
if (context.requestId) headers['X-Request-Id'] = context.requestId;
if (context.tenantId) headers['X-Tenant-Id'] = context.tenantId;
if (context.actorId) headers['X-Actor-Id'] = context.actorId;
const response = await axios.post(targetUrl, payload, {
headers,
validateStatus: () => true,
});
return response.status >= 200 && response.status < 300;
} catch {
return false;
}
}
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
/**
* Load DLQ metrics sampling interval from environment variables.
*
* @returns Sampling interval in milliseconds.
*/
function loadDlqMetricsInterval(): number {
const raw = process.env.DLQ_METRICS_INTERVAL_MS ?? '30000';
const parsed = Number(raw);
if (!Number.isFinite(parsed) || parsed <= 0) {
throw new Error(
`[api/jobs] Invalid DLQ_METRICS_INTERVAL_MS="${raw}". ` +
'Must be a finite positive number greater than zero.',
);
}
return parsed;
}
// ---------------------------------------------------------------------------
// Public API & Lifecycle Orchestration
// ---------------------------------------------------------------------------
/**
* Initialize background jobs: DLQ store and metrics sampling.
*
* This function is idempotent — calling it multiple times will stop the
* previous sampling loop and start a new one.
*
* @param customDlqStore - The DLQ store backing replay operations.
* @returns The initialized DLQ store.
*/
export function initializeJobs(customDlqStore: ReplayableDlqStore): ReplayableDlqStore {
// Stop any existing sampling loop
if (stopSampling !== null) {
stopSampling();
stopSampling = null;
}
dlqStore = customDlqStore;
// Start DLQ metrics sampling
const intervalMs = loadDlqMetricsInterval();
stopSampling = startDlqMetricsSampling(dlqStore, intervalMs);
return dlqStore;
}
/**
* Stop all background jobs and clean up resources.
*
* Intended for graceful shutdown or testing.
*/
export function shutdownJobs(): void {
if (stopSampling !== null) {
stopSampling();
stopSampling = null;
}
dlqStore = null;
}
/**
* Get the current DLQ store instance.
*
* @returns The DLQ store, or `null` if {@link initializeJobs} has not been called.
*/
export function getDlqStore(): ReplayableDlqStore | null {
return dlqStore;
}
// ---------------------------------------------------------------------------
// REST API Routing Interface Endpoints
// ---------------------------------------------------------------------------
const adminOnly = [requireAuth, requireRole('admin')];
/**
* POST /jobs/dlq/:id/replay
* Replays an individual dead letter queue message back through the delivery stack.
*/
router.post(
'/jobs/dlq/:id/replay',
...adminOnly,
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
const id = String(req.params.id ?? '');
const reason = typeof req.body?.reason === 'string' ? req.body.reason : '';
if (id.length === 0) {
res.status(400).json({ error: 'Invalid DLQ record ID' });
return;
}
if (reason.length < 5) {
res.status(400).json({ error: 'Audit trail reason must be at least 5 characters long' });
return;
}
try {
if (!dlqStore) {
res.status(503).json({ error: 'DLQ store is not initialized' });
return;
}
const dlqItem = await dlqStore.getEntryById(id);
if (!dlqItem) {
res.status(404).json({ error: 'DLQ item not found' });
return;
}
// Check the event idempotency layer cache before delivery
const isDuplicate = await IdempotencyLayer.isEventProcessed(dlqItem.eventId);
if (isDuplicate) {
incrementDlqReplay('idempotent_noop');
res.status(200).json({ status: 'ignored', reason: 'Idempotent no-op: Event already delivered' });
return;
}
// Redact sensitive payload properties before delivery logic processing
const safePayload = redactPayload(dlqItem.payload);
const context = extractRequestContext(req);
const deliverySuccess = await deliverRaw(dlqItem.targetUrl, dlqItem.eventId, safePayload, context);
if (deliverySuccess) {
await dlqStore.removeEntry(id);
await IdempotencyLayer.markEventProcessed(dlqItem.eventId);
incrementDlqReplay('success');
res.status(200).json({ status: 'success', message: 'DLQ record replayed and processed', auditReason: reason });
} else {
await dlqStore.incrementReplayAttempts(id);
incrementDlqReplay('failed');
res.status(500).json({ status: 'failed', error: 'Delivery transmission failed during retry execution' });
}
} catch (error) {
incrementDlqReplay('error');
next(error);
}
},
);
/**
* POST /jobs/dlq/replay
* Performs batch replay over an arbitrary array of target active DLQ item IDs.
*/
router.post(
'/jobs/dlq/replay',
...adminOnly,
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
const ids: unknown = req.body?.ids;
const reason = typeof req.body?.reason === 'string' ? req.body.reason : '';
if (!Array.isArray(ids) || ids.length === 0 || !ids.every((v) => typeof v === 'string')) {
res.status(400).json({ error: 'An array of valid IDs is required' });
return;
}
if (reason.length < 5) {
res.status(400).json({ error: 'Audit trail reason must be at least 5 characters long' });
return;
}
try {
if (!dlqStore) {
res.status(503).json({ error: 'DLQ store is not initialized' });
return;
}
const summary = { successCount: 0, noOpCount: 0, failureCount: 0 };
const context = extractRequestContext(req);
for (const id of ids as string[]) {
const dlqItem = await dlqStore.getEntryById(id);
if (!dlqItem) {
summary.failureCount++;
continue;
}
const isDuplicate = await IdempotencyLayer.isEventProcessed(dlqItem.eventId);
if (isDuplicate) {
incrementDlqReplay('idempotent_noop');
summary.noOpCount++;
continue;
}
const safePayload = redactPayload(dlqItem.payload);
const deliverySuccess = await deliverRaw(dlqItem.targetUrl, dlqItem.eventId, safePayload, context);
if (deliverySuccess) {
await dlqStore.removeEntry(id);
await IdempotencyLayer.markEventProcessed(dlqItem.eventId);
incrementDlqReplay('success');
summary.successCount++;
} else {
await dlqStore.incrementReplayAttempts(id);
incrementDlqReplay('failed');
summary.failureCount++;
}
}
res.status(200).json({ status: 'batch_completed', auditReason: reason, details: summary });
} catch (error) {
next(error);
}
},
);
export { router as jobsRouter };