-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathsqlite-worker.js
More file actions
398 lines (353 loc) · 10.8 KB
/
Copy pathsqlite-worker.js
File metadata and controls
398 lines (353 loc) · 10.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
// eslint-disable-next-line import/no-unassigned-import
require('#helpers/polyfill-towellformed');
// eslint-disable-next-line import/no-unassigned-import
require('#config/env');
// eslint-disable-next-line import/no-unassigned-import
require('#config/mongoose');
const os = require('node:os');
const process = require('node:process');
const { setTimeout } = require('node:timers/promises');
const Graceful = require('@ladjs/graceful');
const Redis = require('@ladjs/redis');
const mongoose = require('mongoose');
const ms = require('ms');
const sharedConfig = require('@ladjs/shared-config');
const Aliases = require('#models/aliases');
const ServerShutdownError = require('#helpers/server-shutdown-error');
const config = require('#config');
const email = require('#helpers/email');
const i18n = require('#helpers/i18n');
const logger = require('#helpers/logger');
const setupMongoose = require('#helpers/setup-mongoose');
const { backup, rekey } = require('#helpers/worker');
const imapSharedConfig = sharedConfig('IMAP');
const client = new Redis(imapSharedConfig.redis, logger);
const subscriber = new Redis(imapSharedConfig.redis, logger);
client.setMaxListeners(0);
subscriber.setMaxListeners(0);
//
// Configuration
//
const CHANNEL = `sqlite_backup_queue:${config.env}`;
const REKEY_QUEUE = `rekey_queue:${config.env}`;
const BUSY_KEY = `sqlite_worker_busy:${config.env}`;
const MAX_CONCURRENCY = 2;
const MIN_FREE_MEM = 1024 * 1024 * 1024; // 1 GB
const REKEY_STALE_THRESHOLD = ms('15m');
//
// State
//
let isShuttingDown = false;
let activeJobs = 0;
// Track in-flight rekey payloads so they can be re-queued on shutdown
const inFlightRekeyPayloads = new Set();
//
// Publish busy state to Redis so IMAP/POP3 clients can skip backup requests.
// The key holds the current number of active jobs; deleted when 0.
//
async function updateBusyCounter(delta) {
try {
if (delta > 0) {
await client.multi().incr(BUSY_KEY).expire(BUSY_KEY, 60).exec();
} else {
const val = await client.decr(BUSY_KEY);
if (val <= 0) await client.del(BUSY_KEY);
}
} catch (err) {
logger.debug(err);
}
}
//
// Process a single backup or rekey job
//
async function processJob(payload, payloadStr) {
activeJobs++;
await updateBusyCounter(1);
// Track in-flight rekey jobs for re-queue on shutdown
if (payload.action === 'rekey' && payloadStr) {
inFlightRekeyPayloads.add(payloadStr);
}
try {
switch (payload.action) {
case 'backup': {
await backup(payload);
break;
}
case 'rekey': {
await rekey(payload);
break;
}
default: {
logger.warn('sqlite-worker received unknown action', {
action: payload.action
});
}
}
} catch (err) {
//
// If the rekey was interrupted by shutdown, re-queue it immediately
// so it survives the restart. Do NOT log as fatal — it's expected.
//
if (payload.action === 'rekey' && err instanceof ServerShutdownError) {
if (payloadStr) {
try {
await client.rpush(REKEY_QUEUE, payloadStr);
logger.info('Re-queued rekey job interrupted by shutdown', {
alias_id: payload?.session?.user?.alias_id
});
} catch (requeueErr) {
logger.fatal('Failed to re-queue rekey job during shutdown', {
err: requeueErr,
alias_id: payload?.session?.user?.alias_id
});
}
}
} else {
logger.fatal(err, { payload: { ...payload, session: undefined } });
}
} finally {
if (payload.action === 'rekey' && payloadStr) {
inFlightRekeyPayloads.delete(payloadStr);
}
activeJobs--;
await updateBusyCounter(-1);
}
}
//
// Redis Pub/Sub message handler (backups only — rekey uses List polling)
//
function onMessage(channel, message) {
if (channel !== CHANNEL) return;
if (isShuttingDown) return;
let payload;
try {
payload = JSON.parse(message);
} catch (err) {
logger.warn('sqlite-worker failed to parse message', { err, message });
return;
}
// Rekey jobs should no longer arrive via Pub/Sub (they use the Redis List).
// If one does arrive (e.g. during a rolling deploy with mixed versions),
// push it to the List so it's handled by the polling loop.
if (payload.action === 'rekey') {
client
.rpush(REKEY_QUEUE, message)
.catch((err) => logger.fatal('Failed to redirect rekey to queue', err));
return;
}
//
// Memory gate: skip backup if free memory is too low.
//
if (payload.action === 'backup' && os.freemem() < MIN_FREE_MEM) {
logger.warn('sqlite-worker skipping backup due to low memory', {
freemem: os.freemem(),
threshold: MIN_FREE_MEM,
alias_id: payload?.session?.user?.alias_id
});
return;
}
//
// Concurrency gate: skip backup if at capacity.
//
if (payload.action === 'backup' && activeJobs >= MAX_CONCURRENCY) {
logger.debug('sqlite-worker skipping backup due to concurrency limit', {
activeJobs,
alias_id: payload?.session?.user?.alias_id
});
return;
}
// Fire and forget — processJob handles its own errors
processJob(payload, message);
}
//
// Poll the Redis List for rekey jobs.
// Uses BLPOP with a 5s timeout so we can check isShuttingDown periodically.
//
async function pollRekeyQueue() {
// eslint-disable-next-line no-unmodified-loop-condition
while (!isShuttingDown) {
try {
// BLPOP returns [key, value] or null on timeout
const result = await client.blpop(REKEY_QUEUE, 5);
if (!result) continue; // timeout — loop and check isShuttingDown
const [, payloadStr] = result;
let payload;
try {
payload = JSON.parse(payloadStr);
} catch (err) {
logger.warn('sqlite-worker failed to parse rekey queue item', {
err,
payloadStr
});
continue;
}
// Process rekey synchronously (one at a time) to avoid resource contention
await processJob(payload, payloadStr);
} catch (err) {
// If Redis disconnects, wait briefly and retry
if (!isShuttingDown) {
logger.error('Rekey queue poll error', { err });
await setTimeout(2000);
}
}
}
}
//
// Startup recovery: detect aliases stuck in rekey state from previous
// crashes/deploys and clear them (email the user to retry).
//
async function recoverStuckRekeys() {
try {
const threshold = new Date(Date.now() - REKEY_STALE_THRESHOLD);
const stuckAliases = await Aliases.find({
is_rekey: true,
$or: [
{ rekey_started_at: { $lt: threshold } },
// Handle legacy aliases without rekey_started_at (pre-migration)
{ rekey_started_at: { $exists: false } }
]
})
.select('name domain user rekey_started_at')
.populate('domain', 'name')
.populate('user', 'email locale')
.lean()
.exec();
if (stuckAliases.length === 0) return;
logger.warn(
`sqlite-worker startup: found ${stuckAliases.length} stuck rekey operations`
);
for (const alias of stuckAliases) {
try {
await Aliases.findByIdAndUpdate(alias._id, {
$set: { is_rekey: false },
$unset: { rekey_started_at: 1 }
});
const ownerEmail = alias.user?.email;
const locale = alias.user?.locale || i18n.config.defaultLocale;
const domainName = alias.domain?.name || 'unknown';
const username = `${alias.name}@${domainName}`;
if (ownerEmail) {
await email({
template: 'alert',
message: {
to: ownerEmail,
cc: config.alertsEmail,
subject: i18n.translate(
'ALIAS_REKEY_INTERRUPTED_SUBJECT',
locale,
username
)
},
locals: {
message: i18n.translate(
'ALIAS_REKEY_INTERRUPTED',
locale,
username
),
locale
}
});
}
logger.info('Cleared stuck rekey', {
alias_id: alias._id,
alias_name: username,
rekey_started_at: alias.rekey_started_at
});
} catch (err) {
logger.error('Failed to clear stuck rekey', {
err,
alias_id: alias._id
});
}
}
} catch (err) {
logger.error('recoverStuckRekeys failed', { err });
}
}
//
// Graceful shutdown
//
const graceful = new Graceful({
mongooses: [mongoose],
redisClients: [client, subscriber],
logger,
timeoutMs: ms('2m'),
customHandlers: [
async () => {
isShuttingDown = true;
// Unsubscribe to stop receiving new backup jobs
try {
await subscriber.unsubscribe(CHANNEL);
} catch (err) {
logger.debug(err);
}
// Wait for in-flight jobs to complete (up to 90s)
if (activeJobs > 0) {
logger.info(
`sqlite-worker waiting for ${activeJobs} in-flight jobs to complete`
);
const deadline = Date.now() + ms('90s');
// eslint-disable-next-line no-unmodified-loop-condition
while (activeJobs > 0 && Date.now() < deadline) {
await setTimeout(500);
}
if (activeJobs > 0) {
logger.warn(
`sqlite-worker shutdown timeout with ${activeJobs} jobs still active`
);
//
// Re-queue any in-flight rekey jobs that didn't complete.
// This ensures they survive the deploy and get picked up
// by the next worker instance.
//
for (const payloadStr of inFlightRekeyPayloads) {
try {
await client.rpush(REKEY_QUEUE, payloadStr);
logger.info('Re-queued in-flight rekey job during shutdown');
} catch (err) {
logger.fatal('Failed to re-queue rekey job during shutdown', {
err
});
}
}
}
}
// Clean up busy counter
try {
await client.del(BUSY_KEY);
} catch (err) {
logger.debug(err);
}
}
]
});
graceful.listen();
//
// Start
//
(async () => {
try {
await setupMongoose(logger);
// Recover any stuck rekeys from previous crashes/deploys
await recoverStuckRekeys();
// Subscribe to the backup channel (backups still use Pub/Sub)
subscriber.on('message', onMessage);
await subscriber.subscribe(CHANNEL);
// Start polling the rekey queue (Redis List — persistent, survives restarts)
pollRekeyQueue();
if (process.send) process.send('ready');
logger.info('SQLite backup worker started', {
hide_meta: true,
channel: CHANNEL,
rekeyQueue: REKEY_QUEUE,
maxConcurrency: MAX_CONCURRENCY
});
} catch (err) {
await Promise.race([logger.error(err), setTimeout(5000)]);
process.exit(1);
}
})();