Skip to content

Commit 72eab9c

Browse files
committed
Close client connections on SIGINT/SIGTERM
1 parent 22c790f commit 72eab9c

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/cli/parse-server.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ const help = function(){
3131
function startServer(options, callback) {
3232
const app = express();
3333
const api = new ParseServer(options);
34+
const sockets = {}
35+
let nextId = 0;
3436
app.use(options.mountPath, api);
3537

3638
var server = app.listen(options.port, callback);
39+
//server.on('connection', initializeConnections);
3740
if (options.startLiveQueryServer || options.liveQueryServerOptions) {
3841
let liveQueryServer = server;
3942
if (options.liveQueryPort) {
@@ -43,8 +46,28 @@ function startServer(options, callback) {
4346
}
4447
ParseServer.createLiveQueryServer(liveQueryServer, options.liveQueryServerOptions);
4548
}
49+
50+
function initializeConnections(socket) {
51+
const socketId = nextId++;
52+
socket.id = socket.id || socketId;
53+
sockets[socketId] = socket;
54+
55+
socket.on('close', (s) => {
56+
delete sockets[s.id];
57+
})
58+
}
59+
60+
function shutConnections() {
61+
for (const socketId in sockets) {
62+
try {
63+
sockets[socketId].destroy()
64+
} catch (e) { }
65+
}
66+
}
67+
4668
var handleShutdown = function() {
4769
console.log('Termination signal received. Shutting down.');
70+
shutConnections();
4871
server.close(function () {
4972
process.exit(0);
5073
});
@@ -98,7 +121,7 @@ runner({
98121
});
99122
}
100123
} else {
101-
startServer(options, () => {
124+
startServer(options, (p) => {
102125
logOptions();
103126
console.log('');
104127
console.log('['+process.pid+'] parse-server running on '+options.serverURL);

0 commit comments

Comments
 (0)