Refactor request listeners to use async functions

This commit is contained in:
Zaid Humaimidi committed 2023-08-09 19:51:40 -07:00
1 parent 58488ebc3d
commit 7b8a49a7ba
11 files changed
+148 -163

No files matched your search

+9 -3
View File
@@ -95,7 +95,7 @@ if (!cluster.worker) {
exception: err,
pid: process.pid,
});
server.stop().then(exitWorkerGracefully).catch(exitWorkerUngracefully);
server.stop(false).then(exitWorkerGracefully).catch(exitWorkerUngracefully);
});
const initPromise = db
@@ -111,13 +111,19 @@ if (!cluster.worker) {
process.on("SIGINT", () => {
initPromise.finally(() => {
server.stop().then(exitWorkerGracefully).catch(exitWorkerUngracefully);
server
.stop(false)
.then(exitWorkerGracefully)
.catch(exitWorkerUngracefully);
});
});
process.on("SIGTERM", () => {
initPromise.finally(() => {
server.stop().then(exitWorkerGracefully).catch(exitWorkerUngracefully);
server
.stop(false)
.then(exitWorkerGracefully)
.catch(exitWorkerUngracefully);
});
});
}
+1 -11
View File
@@ -79,8 +79,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({
@@ -88,19 +86,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");
void listener(req, res);
};
const initPromise = db
.connect()
.then(() => {
server.start(options, _listener);
server.start(options, listener);
})
.catch((err) => {
setTimeout(() => {
@@ -109,14 +101,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);
});
+1 -11
View File
@@ -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, listener);
})
.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);
});
+1 -11
View File
@@ -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);
});