Skip to content

Commit 14a53f7

Browse files
committed
Fix(backend): stop listening port when receive ctrl+c/SIGINT or SIGTERM
1 parent a221fde commit 14a53f7

File tree

4 files changed

+298
-78
lines changed

4 files changed

+298
-78
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ RUN yarn build
1010

1111
EXPOSE 3003
1212

13-
CMD ["yarn", "server"]
13+
CMD ["yarn", "serve"]

backend/server.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const RedisStore = connectRedis(session);
2121
const isProduction = process.env.NODE_ENV === 'production';
2222
const port = process.env.PORT || config.http_port;
2323
const app = express();
24+
const server = require('http').createServer(app);
2425

2526
if (isProduction) {
2627
app.use(helmet());
@@ -57,18 +58,21 @@ app.use(session({
5758
// api router
5859
app.use('/api', indexRtr);
5960

60-
let listend = false;
61+
const stopListenOnPort = () => {
62+
server.close(() => {
63+
console.error(`\nClosed out remaining connections.`.red); // eslint-disable-line no-console
64+
});
65+
};
6166
const startListenOnPort = () => {
62-
if (!listend) {
63-
// Start server listen on specific port
64-
app.listen(port, (error) => {
65-
if (error) {
66-
console.log(`\n${error}`); // eslint-disable-line no-console
67-
}
68-
listend = true;
69-
console.log(`\nExpress: Listening on port ${port}, open up http://localhost:${port}/ in your broswer!\n`.green); // eslint-disable-line no-console
70-
});
71-
}
67+
// Start server listen on specific port
68+
server.listen(port, (error) => {
69+
if (error) {
70+
console.error(`\n${error}`); // eslint-disable-line no-console
71+
stopListenOnPort();
72+
process.exit(1);
73+
}
74+
console.log(`\nExpress: Listening on port ${port}, open up http://localhost:${port}/ in your broswer!\n`.green); // eslint-disable-line no-console
75+
});
7276
};
7377

7478
if (!isProduction) {
@@ -96,3 +100,13 @@ if (!isProduction) {
96100
});
97101
startListenOnPort();
98102
}
103+
104+
const stopHandler = (signal) => {
105+
console.error(`\nExit process in responding to %s`.red, signal); // eslint-disable-line no-console
106+
stopListenOnPort();
107+
process.exit(1);
108+
};
109+
110+
process.on('SIGTERM', stopHandler, 'SIGTERM');
111+
process.on('SIGINT', stopHandler, 'SIGINT');
112+
process.on('SIGHUP', stopHandler, 'SIGINT');

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
"author": "Chong Guo <[email protected]>",
99
"license": "MIT",
1010
"scripts": {
11-
"dev": "yarn dll && cross-env NODE_ENV=development yarn server",
12-
"prod": "yarn build && cross-env NODE_ENV=production yarn server",
11+
"dev": "yarn dll && cross-env NODE_ENV=development yarn serve",
12+
"prod": "yarn build && cross-env NODE_ENV=production yarn serve",
1313
"dll": "yarn clean && cross-env NODE_ENV=production webpack --config webpack.config.dll.babel.js",
1414
"build": "yarn dll && cross-env NODE_ENV=production webpack --config webpack.config.prod.babel.js",
1515
"profile": "yarn dll && cross-env NODE_ENV=production webpack --config webpack.config.profile.babel.js",
16-
"server": "nodemon --exec babel-node backend/server.js",
16+
"serve": "nodemon --exec babel-node backend/server.js",
1717
"eslint": "eslint -f codeframe \"**/*.js\"",
1818
"tslint": "tslint -t codeFrame \"**/*.tsx\"",
1919
"stylelint": "stylelint \"**/*.css **/*.sass **/*.scss\"",
2020
"lint": "yarn eslint && yarn tslint && yarn stylelint",
21-
"clean": "rm-cli -r frontend/dist .awcache coverage",
21+
"clean": "trash frontend/dist .awcache coverage",
2222
"test": "jest --no-cache --coverage",
2323
"coverage": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
2424
"deploy": "heroku container:push web"
@@ -66,7 +66,7 @@
6666
"coveralls": "^3.0.2",
6767
"cross-env": "^5.2.0",
6868
"css-loader": "^1.0.0",
69-
"cssnano": "^4.0.0",
69+
"cssnano": "^4.0.1",
7070
"duplicate-package-checker-webpack-plugin": "^3.0.0",
7171
"eslint": "^5.1.0",
7272
"eslint-config-airbnb": "^17.0.0",
@@ -89,7 +89,7 @@
8989
"mini-css-extract-plugin": "^0.4.1",
9090
"morgan": "^1.9.0",
9191
"node-sass": "^4.9.2",
92-
"nodemon": "^1.18.0",
92+
"nodemon": "^1.18.1",
9393
"offline-plugin": "^5.0.5",
9494
"optimize-css-assets-webpack-plugin": "^4.0.3",
9595
"pg": "^7.4.3",
@@ -110,13 +110,13 @@
110110
"redux-devtools-log-monitor": "^1.4.0",
111111
"redux-logger": "^3.0.6",
112112
"redux-thunk": "^2.3.0",
113-
"rm-cli": "^1.4.2",
114113
"run-sequence": "^2.2.1",
115114
"sass-loader": "^7.0.3",
116115
"style-loader": "^0.21.0",
117116
"stylelint": "^9.3.0",
118117
"stylelint-config-standard": "^18.2.0",
119118
"superagent": "^3.8.3",
119+
"trash-cli": "^1.4.0",
120120
"ts-jest": "^23.0.0",
121121
"ts-loader": "^4.4.2",
122122
"tslint": "^5.10.0",
@@ -126,7 +126,7 @@
126126
"uglifyjs-webpack-plugin": "^1.2.7",
127127
"url-loader": "^1.0.1",
128128
"uuid": "^3.3.2",
129-
"webpack": "^4.15.1",
129+
"webpack": "^4.16.0",
130130
"webpack-bundle-analyzer": "^2.13.1",
131131
"webpack-cli": "^3.0.8",
132132
"webpack-dev-middleware": "^3.1.3",

0 commit comments

Comments
 (0)