-
Notifications
You must be signed in to change notification settings - Fork 122
/
sqlite.js
94 lines (84 loc) · 2.65 KB
/
sqlite.js
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
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
// eslint-disable-next-line import/no-unassigned-import
require('#config/env');
// eslint-disable-next-line import/no-unassigned-import
require('#config/mongoose');
const process = require('node:process');
const { promisify } = require('node:util');
const Graceful = require('@ladjs/graceful');
const Redis = require('@ladjs/redis');
const ip = require('ip');
const mongoose = require('mongoose');
const ms = require('ms');
const sharedConfig = require('@ladjs/shared-config');
const SQLite = require('./sqlite-server');
const closeDatabase = require('#helpers/close-database');
const logger = require('#helpers/logger');
const monitorServer = require('#helpers/monitor-server');
const setupMongoose = require('#helpers/setup-mongoose');
const imapSharedConfig = sharedConfig('IMAP');
const client = new Redis(imapSharedConfig.redis, logger);
const subscriber = new Redis(imapSharedConfig.redis, logger);
client.setMaxListeners(0);
subscriber.setMaxListeners(0);
const sqlite = new SQLite({ client, subscriber });
const graceful = new Graceful({
mongooses: [mongoose],
servers: [sqlite.server],
redisClients: [client, subscriber],
logger,
timeoutMs: ms('1m'),
customHandlers: [
// TODO: signal to all piscina workers we are shutting down
// <https://github.com/piscinajs/piscina/issues/615>
() => sqlite.piscina.close(),
() => {
sqlite.isClosing = true;
},
() => promisify(sqlite.wss.close).bind(sqlite.wss)(),
// normal databases
async () => {
if (!sqlite.databaseMap || sqlite.databaseMap.size === 0) return;
await Promise.all(
[...sqlite.databaseMap.keys()].map(async (key) => {
await closeDatabase(sqlite.databaseMap.get(key));
sqlite.databaseMap.delete(key);
})
);
},
// temporary databases
async () => {
if (
!sqlite.temporaryDatabaseMap ||
sqlite.temporaryDatabaseMap.size === 0
)
return;
await Promise.all(
[...sqlite.temporaryDatabaseMap.keys()].map(async (key) => {
await closeDatabase(sqlite.temporaryDatabaseMap.get(key));
sqlite.temporaryDatabaseMap.delete(key);
})
);
}
]
});
graceful.listen();
monitorServer();
(async () => {
try {
await sqlite.listen();
if (process.send) process.send('ready');
const { port } = sqlite.server.address();
logger.info(
`SQLite WebSocket server listening on ${port} (LAN: ${ip.address()}:${port})`,
{ hide_meta: true }
);
await setupMongoose(logger);
} catch (err) {
await logger.error(err);
process.exit(1);
}
})();