Refactor request listeners to use async functions
This commit is contained in:
1 parent
58488ebc3d
commit
7b8a49a7ba
11 files changed
+148
-163
No files matched your search
+1
-11
@@ -84,8 +84,6 @@ if (!cluster.worker) {
|
||||
timeout: 30000,
|
||||
};
|
||||
|
||||
let stopping = false;
|
||||
|
||||
process.on("uncaughtException", (err) => {
|
||||
if ((err as NodeJS.ErrnoException).code === "ERR_IPC_DISCONNECTED") return;
|
||||
logger.error({
|
||||
@@ -93,19 +91,13 @@ if (!cluster.worker) {
|
||||
exception: err,
|
||||
pid: process.pid,
|
||||
});
|
||||
stopping = true;
|
||||
server.stop().then(exitWorkerGracefully).catch(exitWorkerUngracefully);
|
||||
});
|
||||
|
||||
const _listener = (req, res): void => {
|
||||
if (stopping) res.setHeader("Connection", "close");
|
||||
listener(req, res);
|
||||
};
|
||||
|
||||
const initPromise = db
|
||||
.connect()
|
||||
.then(() => {
|
||||
server.start(options, _listener);
|
||||
server.start(options, async (req, res) => listener(req, res));
|
||||
})
|
||||
.catch((err) => {
|
||||
setTimeout(() => {
|
||||
@@ -114,14 +106,12 @@ if (!cluster.worker) {
|
||||
});
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
stopping = true;
|
||||
initPromise.finally(() => {
|
||||
server.stop().then(exitWorkerGracefully).catch(exitWorkerUngracefully);
|
||||
});
|
||||
});
|
||||
|
||||
process.on("SIGTERM", () => {
|
||||
stopping = true;
|
||||
initPromise.finally(() => {
|
||||
server.stop().then(exitWorkerGracefully).catch(exitWorkerUngracefully);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user