forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplicationErrorRecovery.server.ts
More file actions
207 lines (190 loc) · 6.8 KB
/
Copy pathreplicationErrorRecovery.server.ts
File metadata and controls
207 lines (190 loc) · 6.8 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
import { Logger } from "@trigger.dev/core/logger";
// When the LogicalReplicationClient's WAL stream errors (e.g. after a
// Postgres failover) it calls stop() on itself and stays stopped. The host
// service has to decide how to recover. Three strategies are available:
//
// - "reconnect" — re-subscribe in-process with exponential backoff. Default;
// works without a process supervisor.
// - "exit" — exit the process so an external supervisor (Docker
// restart=always, ECS, systemd, k8s, ...) replaces it. Recommended when a
// supervisor is present because it gets a clean slate every time.
// - "log" — preserve the historical no-op behaviour. Useful for
// debugging or in test environments where you want to observe the
// silent-death failure mode.
export type ReplicationErrorRecoveryStrategy =
| {
type: "reconnect";
initialDelayMs?: number;
maxDelayMs?: number;
// 0 (or undefined) means retry forever.
maxAttempts?: number;
}
| {
type: "exit";
exitDelayMs?: number;
exitCode?: number;
}
| { type: "log" };
export type ReplicationErrorRecoveryDeps = {
strategy: ReplicationErrorRecoveryStrategy;
logger: Logger;
// Re-subscribe the underlying replication client. Implementations should
// call client.subscribe(...) and resolve once the stream is started.
reconnect: () => Promise<void>;
// True once the host service has begun graceful shutdown — recovery
// suppresses all work in that state.
isShuttingDown: () => boolean;
};
export type ReplicationErrorRecovery = {
// Called from the replication client's "error" event handler.
handle(error: unknown): void;
// Called from the replication client's "start" event handler. Resets the
// reconnect attempt counter so the next failure starts from initialDelayMs.
notifyStreamStarted(): void;
// Called from the replication client's "leaderElection" event handler with
// isLeader=false. Only the reconnect strategy acts on this; exit and log
// strategies treat losing the lock as a normal multi-instance state (an
// "exit" instance would otherwise restart-loop whenever a peer holds it).
notifyLeaderElectionLost(error: unknown): void;
// Cancel any pending reconnect/exit timer. Called from shutdown().
dispose(): void;
};
export function createReplicationErrorRecovery(
deps: ReplicationErrorRecoveryDeps
): ReplicationErrorRecovery {
const { strategy, logger, reconnect, isShuttingDown } = deps;
let attempt = 0;
let pendingReconnect: NodeJS.Timeout | null = null;
let pendingExit: NodeJS.Timeout | null = null;
let exiting = false;
function scheduleReconnect(error: unknown): void {
if (strategy.type !== "reconnect") return;
if (pendingReconnect) return;
attempt += 1;
const maxAttempts = strategy.maxAttempts ?? 0;
if (maxAttempts > 0 && attempt > maxAttempts) {
logger.error("Replication reconnect exceeded maxAttempts; giving up", {
attempt,
maxAttempts,
error,
});
return;
}
const initialDelay = strategy.initialDelayMs ?? 1_000;
const maxDelay = strategy.maxDelayMs ?? 60_000;
const delay = Math.min(initialDelay * Math.pow(2, attempt - 1), maxDelay);
logger.error("Replication stream lost — scheduling reconnect", {
attempt,
delayMs: delay,
error,
});
pendingReconnect = setTimeout(async () => {
pendingReconnect = null;
if (isShuttingDown()) return;
try {
await reconnect();
// Success path is handled by notifyStreamStarted, which fires from
// the replication client's "start" event after the stream is live.
} catch (err) {
// subscribe() can throw without first emitting an "error" event —
// notably when the initial pg client.connect() fails because Postgres
// is still unreachable mid-failover. Schedule the next attempt
// ourselves so recovery doesn't silently stop. If subscribe() did
// also emit an "error" event, handle() will call scheduleReconnect()
// first; the guard on pendingReconnect makes this idempotent.
logger.error("Replication reconnect attempt failed", {
attempt,
error: err,
});
scheduleReconnect(err);
}
}, delay);
}
function scheduleExit(): void {
if (strategy.type !== "exit") return;
if (exiting) return;
exiting = true;
const delay = strategy.exitDelayMs ?? 5_000;
const code = strategy.exitCode ?? 1;
logger.error("Fatal replication error — exiting to let process supervisor restart", {
exitCode: code,
exitDelayMs: delay,
});
pendingExit = setTimeout(() => {
// eslint-disable-next-line no-process-exit
process.exit(code);
}, delay);
// Don't hold a clean shutdown back on this timer.
pendingExit.unref();
}
return {
handle(error) {
if (isShuttingDown()) return;
switch (strategy.type) {
case "log":
return;
case "exit":
return scheduleExit();
case "reconnect":
return scheduleReconnect(error);
}
},
notifyStreamStarted() {
if (attempt > 0) {
logger.info("Replication reconnect succeeded", { attempt });
attempt = 0;
}
},
notifyLeaderElectionLost(error) {
if (isShuttingDown()) return;
// Only the reconnect strategy should react. For exit, losing the
// lock to a peer would otherwise trigger a restart loop. For log,
// we keep historical no-op semantics.
if (strategy.type !== "reconnect") return;
scheduleReconnect(error);
},
dispose() {
if (pendingReconnect) {
clearTimeout(pendingReconnect);
pendingReconnect = null;
}
if (pendingExit) {
clearTimeout(pendingExit);
pendingExit = null;
}
},
};
}
// Shape of the env-driven configuration object the instance bootstrap files
// build from process.env. Kept separate from the strategy union above so the
// instance code can pass a single object regardless of which strategy is set.
export type ReplicationErrorRecoveryEnv = {
strategy: "reconnect" | "exit" | "log";
reconnectInitialDelayMs?: number;
reconnectMaxDelayMs?: number;
reconnectMaxAttempts?: number;
exitDelayMs?: number;
exitCode?: number;
};
export function strategyFromEnv(
env: ReplicationErrorRecoveryEnv
): ReplicationErrorRecoveryStrategy {
switch (env.strategy) {
case "exit":
return {
type: "exit",
exitDelayMs: env.exitDelayMs,
exitCode: env.exitCode,
};
case "log":
return { type: "log" };
case "reconnect":
default:
return {
type: "reconnect",
initialDelayMs: env.reconnectInitialDelayMs,
maxDelayMs: env.reconnectMaxDelayMs,
maxAttempts: env.reconnectMaxAttempts,
};
}
}