forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-queue.ts
More file actions
348 lines (312 loc) · 9.63 KB
/
Copy pathevent-queue.ts
File metadata and controls
348 lines (312 loc) · 9.63 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
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* event-queue.ts — Persistent local queue for events.
*
* Buffers events to SQLite before passing to handlers, preventing data loss
* during traffic spikes or downstream handler failures. Supports recovery of
* unprocessed events on restart.
*
* Queue States:
* pending → Enqueued, awaiting handler processing
* processing → Currently being handled
* processed → Successfully handled
* failed → Handler failed; eligible for retry
*/
import Database from "better-sqlite3";
import { EngineEvent } from "./event-propagator";
import * as fs from "fs";
import * as path from "path";
import { logger } from "./logger";
const DEFAULT_DB_PATH = path.join(process.cwd(), "event-queue.db");
const MAX_RETRIES = 3;
export interface QueuedEvent {
id: string;
eventData: EngineEvent;
status: "pending" | "processing" | "processed" | "failed";
attempts: number;
enqueueTime: number;
processTime?: number;
error?: string;
/** Timestamp when the event becomes eligible for the next attempt (ms since epoch) */
nextAttempt?: number;
}
export class EventQueue {
private db: Database.Database;
private readonly dbPath: string;
private readonly maxRetries: number;
constructor(dbPath: string = DEFAULT_DB_PATH, maxRetries: number = MAX_RETRIES) {
this.dbPath = dbPath;
this.maxRetries = maxRetries;
this.db = this.initializeDatabase();
}
/**
* Initialize SQLite database with events table if not present.
* Schema:
* id (TEXT PK) - Unique event identifier
* eventData (TEXT) - JSON-serialized EngineEvent
* status (TEXT) - pending | processing | processed | failed
* attempts (INT) - Number of processing attempts
* enqueueTime (INT) - Milliseconds since epoch
* processTime (INT) - Milliseconds since epoch (nullable)
* error (TEXT) - Last error message (nullable)
* nextAttempt (INT) - Next time eligible for retry
*/
private initializeDatabase(): Database.Database {
const db = new Database(this.dbPath);
db.pragma("journal_mode = WAL");
db.pragma("synchronous = NORMAL");
db.exec(`
CREATE TABLE IF NOT EXISTS events (
id TEXT PRIMARY KEY,
eventData TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending',
attempts INT NOT NULL DEFAULT 0,
enqueueTime INT NOT NULL,
processTime INT,
error TEXT,
nextAttempt INT NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_status ON events(status);
CREATE INDEX IF NOT EXISTS idx_enqueueTime ON events(enqueueTime);
CREATE INDEX IF NOT EXISTS idx_nextAttempt ON events(nextAttempt);
`);
return db;
}
/**
* Enqueue an event for processing. Returns true if successfully queued.
*/
enqueue(event: EngineEvent): boolean {
try {
const now = Date.now();
const stmt = this.db.prepare(`
INSERT INTO events (id, eventData, status, attempts, enqueueTime, nextAttempt)
VALUES (?, ?, ?, ?, ?, ?)
`);
stmt.run(
event.id,
JSON.stringify(event),
"pending",
0,
now,
now
);
return true;
} catch (err) {
logger.error("[EventQueue] Enqueue failed:", err);
return false;
}
}
/**
* Dequeue a pending event for processing. Transitions to 'processing' state.
* Returns the event or null if none available.
*/
dequeue(): QueuedEvent | null {
try {
const now = Date.now();
const stmt = this.db.prepare(`
SELECT * FROM events
WHERE (status = 'pending' OR (status = 'failed' AND attempts < ?))
AND nextAttempt <= ?
ORDER BY enqueueTime ASC
LIMIT 1
`);
const row = stmt.get(this.maxRetries, now) as any;
if (!row) return null;
// Transition to processing
const updateStmt = this.db.prepare(`
UPDATE events
SET status = 'processing', attempts = attempts + 1
WHERE id = ?
`);
updateStmt.run(row.id);
return {
id: row.id,
eventData: JSON.parse(row.eventData),
status: "processing",
attempts: row.attempts + 1,
enqueueTime: row.enqueueTime,
processTime: row.processTime,
error: row.error,
};
} catch (err) {
logger.error("[EventQueue] Dequeue failed:", err);
return null;
}
}
/**
* Mark an event as successfully processed. Transitions to 'processed' state.
*/
markProcessed(eventId: string): boolean {
try {
const stmt = this.db.prepare(`
UPDATE events
SET status = 'processed', processTime = ?
WHERE id = ?
`);
stmt.run(Date.now(), eventId);
return true;
} catch (err) {
logger.error("[EventQueue] Mark processed failed:", err);
return false;
}
}
/**
* Mark an event as failed with error message. If retries available,
* transitions back to 'pending'; otherwise to 'failed'.
*/
markFailed(eventId: string, error: Error): boolean {
try {
// Get current attempt count and nextAttempt
const getStmt = this.db.prepare("SELECT attempts FROM events WHERE id = ?");
const row = getStmt.get(eventId) as any;
if (!row) return false;
const hasMoreRetries = row.attempts < this.maxRetries;
const newStatus = "failed"; // always update to 'failed' in DB to track states
// Compute exponential backoff delay (in ms) based on next attempt count
const isTest = process.env.NODE_ENV === "test";
const delayMs = (hasMoreRetries && !isTest) ? Math.pow(2, row.attempts) * 1000 : 0; // attempts already incremented in dequeue
const nextAttempt = Date.now() + delayMs;
const stmt = this.db.prepare(`
UPDATE events
SET status = ?, error = ?, nextAttempt = ?
WHERE id = ?
`);
stmt.run(newStatus, error.message, nextAttempt, eventId);
return true;
} catch (err) {
logger.error("[EventQueue] Mark failed failed:", err);
return false;
}
}
/**
* Recover unprocessed events from queue (for startup).
* Returns all pending and failed (with retries available) events.
*/
recoverPending(): QueuedEvent[] {
try {
// Reset any processing or retrying failed events back to pending
const resetStmt = this.db.prepare(`
UPDATE events
SET status = 'pending'
WHERE status = 'processing' OR (status = 'failed' AND attempts < ?)
`);
resetStmt.run(this.maxRetries);
const stmt = this.db.prepare(`
SELECT * FROM events
WHERE status = 'pending'
ORDER BY enqueueTime ASC
`);
const rows = stmt.all() as any[];
return rows.map(row => ({
id: row.id,
eventData: JSON.parse(row.eventData),
status: row.status,
attempts: row.attempts,
enqueueTime: row.enqueueTime,
processTime: row.processTime,
error: row.error,
}));
} catch (err) {
logger.error("[EventQueue] Recover pending failed:", err);
return [];
}
}
/**
* Get queue statistics (size, oldest event, error rate).
*/
getStats(): {
total: number;
pending: number;
processing: number;
processed: number;
failed: number;
oldestEventAge: number | null;
} {
try {
const allStmt = this.db.prepare("SELECT status, attempts FROM events");
const rows = allStmt.all() as any[];
const stats = {
total: rows.length,
pending: 0,
processing: 0,
processed: 0,
failed: 0,
};
rows.forEach(row => {
if (row.status === "pending") {
stats.pending++;
} else if (row.status === "processed") {
stats.processed++;
} else if (row.status === "processing") {
stats.processing++;
} else if (row.status === "failed") {
if (row.attempts < this.maxRetries) {
stats.processing++;
} else {
stats.failed++;
}
}
});
const oldestStmt = this.db.prepare("SELECT enqueueTime FROM events ORDER BY enqueueTime ASC LIMIT 1");
const oldest = oldestStmt.get() as any;
const oldestEventAge = oldest ? Date.now() - oldest.enqueueTime : null;
return {
...stats,
oldestEventAge,
};
} catch (err) {
logger.error("[EventQueue] Get stats failed:", err);
return {
total: 0,
pending: 0,
processing: 0,
processed: 0,
failed: 0,
oldestEventAge: null,
};
}
}
/**
* Clean up old processed events. Removes processed events older than maxAgeMs.
*/
cleanup(maxAgeMs: number = 24 * 60 * 60 * 1000): number {
try {
if (maxAgeMs <= 0) {
const result = this.db.prepare("DELETE FROM events WHERE status = 'processed'").run();
return result.changes;
}
const stmt = this.db.prepare(`
DELETE FROM events
WHERE status = 'processed' AND processTime <= ?
`);
const cutoff = Date.now() - maxAgeMs;
const result = stmt.run(cutoff);
return result.changes;
} catch (err) {
logger.error("[EventQueue] Cleanup failed:", err);
return 0;
}
}
/**
* Close database connection.
*/
close(): void {
if (this.db) {
this.db.close();
}
}
/**
* Delete queue file (for testing/reset). Closes database first.
*/
deleteQueue(): void {
this.close();
if (fs.existsSync(this.dbPath)) {
fs.unlinkSync(this.dbPath);
}
if (fs.existsSync(this.dbPath + "-shm")) {
fs.unlinkSync(this.dbPath + "-shm");
}
if (fs.existsSync(this.dbPath + "-wal")) {
fs.unlinkSync(this.dbPath + "-wal");
}
}
}