Skip to content

Commit

Permalink
fix: handle system signals
Browse files Browse the repository at this point in the history
  • Loading branch information
nowfred committed Oct 11, 2024
1 parent 25f69dc commit ecb3901
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"prepare": "husky"
},
"dependencies": {
"@moebius/http-graceful-shutdown": "^1.1.0",
"chalk": "^5.3.0",
"commander": "^12.1.0",
"compression": "^1.7.4",
Expand Down
15 changes: 15 additions & 0 deletions src/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import http from 'node:http';
import https from 'node:https';
import type { Server } from 'node:net';
import path from 'path';
import { GracefulShutdownManager } from '@moebius/http-graceful-shutdown';
import compression from 'compression';
import type { Express } from 'express';
import express from 'express';
Expand Down Expand Up @@ -161,6 +162,20 @@ async function createServer(config: ServerConfig): Promise<ICreateServerOut> {
void printServerInfo(config, { version, server });
});

const shutdownManager = new GracefulShutdownManager(server);

process.on('SIGTERM', () => {
shutdownManager.terminate(() => {
console.log('Server has been gracefully terminated');
});
});

process.on('SIGINT', () => {
shutdownManager.terminate(() => {
console.log('Server has been gracefully terminated');
});
});

return server;
},
app,
Expand Down

0 comments on commit ecb3901

Please sign in to comment.