Skip to content
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

Close Telemetry Server: subset B - Stop listening to client requests #52

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions common/websocket-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ WebsocketTracker.prototype.closeAll = function() {
});
}

WebsocketTracker.prototype.pauseAll = function() {
this.sockets.forEach((value,key) => {
key.pause();
});
}

module.exports = function (server) {
return new WebsocketTracker(server);
}
54 changes: 9 additions & 45 deletions iCubTelemVizServer/iCubTelemVizServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// When the signal comes from the terminal, the generated event doesn't have a 'signal' parameter,
// so it appears undefined in the callback body. We worked around this issue by explicitly setting
// the 'signal' parameter case by case.
process.once('SIGQUIT', () => {handleTermination('SIGQUIT');});
process.once('SIGTERM', () => {handleTermination('SIGTERM');});
process.once('SIGINT', () => {handleTermination('SIGINT');});
process.once('SIGQUIT', () => {terminationHandler.run('SIGQUIT');});
process.once('SIGTERM', () => {terminationHandler.run('SIGTERM');});
process.once('SIGINT', () => {terminationHandler.run('SIGINT');});

// require and setup basic http functionalities
var portTelemetryReqOrigin = process.env.PORT_TLM_REQ_ORIGIN || 8080
Expand Down Expand Up @@ -175,45 +175,9 @@ var openMctServerHandler = new OpenMctServerHandler(console.log,console.error);
var ret = openMctServerHandler.start();
console.log(ret);

function handleTermination(signal) {
console.log('Received '+signal+' ...');

/*
* === Closure subset A ===
* A.1 - Stop the child Node.js process running the OpenMCT static server.
* A.2 - Stop the Telemetry HTTP server telemServer from accepting new connections.
* A.3 - Stop the Control Console HTTP server consoleServer from accepting new connections.
*/
const closeChildProcessPromise = new Promise(function(resolve,reject) {
ret = openMctServerHandler.stop(signal,resolve,reject);
console.log(ret);
});
const closeTelemServerPromise = new Promise(function(resolve,reject) {
telemServer.close((error) => {
if (error === undefined) {
resolve('iCub Telemetry Server closed: all sockets closed.');
} else {
reject(error);
}
});
console.log('iCub Telemetry Server closing: no further connection requests accepted.');
});
const closeConsoleServerPromise = new Promise(function(resolve,reject) {
consoleServer.close((error) => {
if (error === undefined) {
resolve('Control Console Server closed: all sockets closed.');
} else {
reject(error);
}
});
console.log('Control Console Server closing: no further incoming requests accepted.');
});
Promise.all([closeChildProcessPromise,closeTelemServerPromise,closeConsoleServerPromise]).then(
function(values) {
values.forEach((v) => console.log(v));
},
function(error) {
console.error(error.toString());
}
);
}
// Handler for a gracious termination
const TerminationHandler = require('./terminationHandler.js');
var terminationHandler = new TerminationHandler(
openMctServerHandler,
telemServer,telemServerTracker,
consoleServer,consoleServerTracker);
67 changes: 67 additions & 0 deletions iCubTelemVizServer/terminationHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"use strict";

function TerminationHandler(
openMctServerHandler,
telemServer,telemServerTracker,
consoleServer,consoleServerTracker) {
this.openMctServerHandler = openMctServerHandler;
this.telemServer = telemServer;
this.telemServerTracker = telemServerTracker;
this.consoleServer = consoleServer;
this.consoleServerTracker = consoleServerTracker;
}

TerminationHandler.prototype.run = function(signal) {
console.log('Received '+signal+' ...');
this.runSubsetA(signal);
}

TerminationHandler.prototype.runSubsetA = function (signal) {
/*
* === Closure subset A ===
* A.1 - Stop the child Node.js process running the OpenMCT static server.
* A.2 - Stop the Telemetry HTTP server telemServer from accepting new connections.
* A.3 - Stop the Control Console HTTP server consoleServer from accepting new connections.
*/
const closeChildProcessPromise = new Promise(function(resolve,reject) {
let ret = this.openMctServerHandler.stop(signal,resolve,reject);
console.log(ret);
}.bind(this));
const closeTelemServerPromise = new Promise(function(resolve,reject) {
this.telemServer.close((error) => {
if (error === undefined) {
resolve('iCub Telemetry Server closed: all sockets closed.');
} else {
reject(error);
}
});
console.log('iCub Telemetry Server closing: no further connection requests accepted.');
}.bind(this));
const closeConsoleServerPromise = new Promise(function(resolve,reject) {
this.consoleServer.close((error) => {
if (error === undefined) {
resolve('Control Console Server closed: all sockets closed.');
} else {
reject(error);
}
});
console.log('Control Console Server closing: no further incoming requests accepted.');
}.bind(this));
closeChildProcessPromise.then(
function(value) {
console.log(value);
this.runSubsetB();
}.bind(this),
function(error) { console.error(error.toString()); }
);
}

TerminationHandler.prototype.runSubsetB = function() {
/*
* === Closure subset B ===
* B.1 - Stop listening to client requests ("subscribe"/"unsubscribe" events) on the existing connections.
*/
this.telemServerTracker.pauseAll();
}

module.exports = TerminationHandler;