-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.ts
More file actions
135 lines (125 loc) · 3.86 KB
/
processor.ts
File metadata and controls
135 lines (125 loc) · 3.86 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
import pg from "pg";
import { randomUUID } from "node:crypto";
import {
createEventHandlerMap,
createEventProcessor,
type TxOBProcessor,
WakeupEmitter,
} from "../../src/index.js";
import {
createProcessorClient,
createWakeupEmitter,
} from "../../src/pg/client.js";
import { eventSchemas, eventTypes } from "./events.js";
import { migrate } from "./server.js";
import dotenv from "dotenv";
import { sleep } from "../../src/sleep.js";
dotenv.config();
let processor: TxOBProcessor | undefined = undefined;
let wakeupEmitter: WakeupEmitter | undefined = undefined;
(async () => {
const clientConfig: pg.ClientConfig = {
user: process.env.POSTGRES_USER || 'outbox',
password: process.env.POSTGRES_PASSWORD || 'outbox',
database: process.env.POSTGRES_DB || 'outbox',
port: parseInt(process.env.POSTGRES_PORT || "5434"),
};
const client = new pg.Client(clientConfig);
await client.connect();
await migrate(client);
wakeupEmitter = await createWakeupEmitter({
listenClientConfig: clientConfig,
createTrigger: true,
querier: client,
});
const handlerMap = createEventHandlerMap({
eventSchemas,
handlerMap: {
ResourceSaved: {
thing1: async (event) => {
console.log(
`${event.id} thing1 ${event.correlation_id} activity=${event.data.id}`,
);
if (Math.random() > 0.99) throw new Error("some issue");
},
thing2: async (event) => {
console.log(
`${event.id} thing2 ${event.correlation_id} kind=${event.data.type}`,
);
if (Math.random() > 0.96) throw new Error("some issue");
},
thing3: async (event) => {
await sleep(Math.random() * 1_000);
console.log(`${event.id} thing3 ${event.correlation_id}`);
if (Math.random() > 0.8) throw new Error("some issue");
},
},
EventMaxErrorsReached: {
// Optional: add handlers for EventMaxErrorsReached events if needed
// For example, you might want to send alerts or log to external systems
notify: async (event) => {
console.log(
"Event max errors reached",
event.data.failedEventType,
event.data.failedEventId,
);
},
},
},
});
processor = createEventProcessor({
eventSchemas,
maxEventConcurrency: 50,
client: createProcessorClient({ querier: client, eventSchemas }),
wakeupEmitter,
handlerMap,
pollingIntervalMs: 5000,
logger: {
info: console.log,
error: console.error,
warn: console.warn,
debug: () => { },
},
onEventMaxErrorsReached: async ({ event, txClient }) => {
// Transactionally persist an 'event max errors reached' event
// This hook is called when:
// - Maximum allowed errors are reached
// - An unprocessable error is encountered
// - Event handler map is missing for the event type
await txClient.createEvent({
id: randomUUID(),
timestamp: new Date(),
type: eventTypes.EventMaxErrorsReached,
data: eventSchemas.EventMaxErrorsReached.parse({
failedEventId: event.id,
failedEventType: event.type,
failedEventCorrelationId: event.correlation_id,
}),
correlation_id: event.correlation_id,
handler_results: {},
errors: 0,
});
console.log("Event max errors reached event created", {
failedEventId: event.id,
});
},
});
processor.start();
})();
const shutdown = (() => {
let shutdownStarted = false;
return async () => {
if (shutdownStarted) return;
shutdownStarted = true;
try {
await processor?.stop();
await wakeupEmitter?.close();
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit(0);
};
})();
process.once("SIGTERM", shutdown);
process.once("SIGINT", shutdown);