cleanup of not used sessions

This commit is contained in:
Jan Prochazka
2022-11-13 11:52:31 +01:00
parent a9f1f19696
commit 6cca81f8f1
4 changed files with 63 additions and 1 deletions

View File

@@ -103,6 +103,10 @@ module.exports = {
if (handleProcessCommunication(message, subprocess)) return;
this[`handle_${msgtype}`](sesid, message);
});
subprocess.on('exit', () => {
this.opened = this.opened.filter(x => x.sesid != sesid);
});
subprocess.send({ msgtype: 'connect', ...connection, database });
return _.pick(newOpened, ['conid', 'database', 'sesid']);
},
@@ -165,6 +169,17 @@ module.exports = {
return { state: 'ok' };
},
ping_meta: true,
async ping({ sesid }) {
const session = this.opened.find(x => x.sesid == sesid);
if (!session) {
throw new Error('Invalid session');
}
session.subprocess.send({ msgtype: 'ping' });
return { state: 'ok' };
},
// runCommand_meta: true,
// async runCommand({ conid, database, sql }) {
// console.log(`Running SQL command , conid=${conid}, database=${database}, sql=${sql}`);

View File

@@ -15,6 +15,7 @@ let systemConnection;
let storedConnection;
let afterConnectCallbacks = [];
// let currentHandlers = [];
let lastPing = null;
class TableWriter {
constructor() {
@@ -271,10 +272,15 @@ async function handleExecuteReader({ jslid, sql, fileName }) {
});
}
function handlePing() {
lastPing = new Date().getTime();
}
const messageHandlers = {
connect: handleConnect,
executeQuery: handleExecuteQuery,
executeReader: handleExecuteReader,
ping: handlePing,
// cancel: handleCancel,
};
@@ -285,6 +291,15 @@ async function handleMessage({ msgtype, ...other }) {
function start() {
childProcessChecker();
setInterval(() => {
const time = new Date().getTime();
if (time - lastPing > 30_000) {
console.log('Session not alive, exiting');
process.exit(0);
}
}, 30_000);
process.on('message', async message => {
if (handleProcessCommunication(message)) return;
try {