Timeout nodes that are no longer reachable

Fixes #7
This commit is contained in:
2023-08-02 23:07:14 +02:00
parent 81e23807a8
commit 80c1dda083

View File

@@ -78,6 +78,7 @@ app.post("/jobs", async (req, res) => {
}); });
wss.on('connection', function connection(ws) { wss.on('connection', function connection(ws) {
ws.isAlive = true;
var nodeID = uuid.v4(); var nodeID = uuid.v4();
console.log("New connection from", nodeID) console.log("New connection from", nodeID)
ws.on('error', console.error); ws.on('error', console.error);
@@ -119,12 +120,29 @@ wss.on('connection', function connection(ws) {
} }
} }
}); });
ws.on('pong', () => {
ws.isAlive = true;
});
ws.on('close', function(reasonCode, description) { ws.on('close', function(reasonCode, description) {
console.log(`Node ${ws.nodeID} disconnected.`); console.log(`Node ${ws.nodeID} disconnected.`);
delete nodes[ws.nodeID] delete nodes[ws.nodeID]
}); });
}); });
const timeoutConnections = setInterval(() => {
for (const [id, node] of Object.entries(nodes)) {
if (node.isAlive === false) {
console.log(`Node considered dead ${id}`);
return node.terminate();
}
node.isAlive = false;
node.ping('ping');
};
}, 5000);
app.use('/public', express.static('public')); app.use('/public', express.static('public'));
app.listen(PORT_WEB, () => { app.listen(PORT_WEB, () => {