Skip to content

Close client connections on SIGINT/SIGTERM #2959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/cli/parse-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ const help = function(){
function startServer(options, callback) {
const app = express();
const api = new ParseServer(options);
const sockets = {}
let nextId = 0;
app.use(options.mountPath, api);

var server = app.listen(options.port, callback);
server.on('connection', initializeConnections);
if (options.startLiveQueryServer || options.liveQueryServerOptions) {
let liveQueryServer = server;
if (options.liveQueryPort) {
Expand All @@ -43,8 +46,28 @@ function startServer(options, callback) {
}
ParseServer.createLiveQueryServer(liveQueryServer, options.liveQueryServerOptions);
}

function initializeConnections(socket) {
const socketId = nextId++;
socket.id = socket.id || socketId;
sockets[socketId] = socket;

socket.on('close', (s) => {
delete sockets[s.id];
})
}

function shutConnections() {
for (const socketId in sockets) {
try {
sockets[socketId].destroy()
} catch (e) { }
}
}

var handleShutdown = function() {
console.log('Termination signal received. Shutting down.');
shutConnections();
server.close(function () {
process.exit(0);
});
Expand Down