From cc388362d6088fe7570b543f52ec7844d2981d6e Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sat, 28 Jan 2023 11:40:52 +0100 Subject: [PATCH] close query sessions after timeout #468 --- packages/api/src/controllers/sessions.js | 8 +++- packages/api/src/proc/sessionProcess.js | 24 +++++++++- .../web/src/settings/SettingsModal.svelte | 48 ++++++++++++------- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/packages/api/src/controllers/sessions.js b/packages/api/src/controllers/sessions.js index b4db80ad..658b2c45 100644 --- a/packages/api/src/controllers/sessions.js +++ b/packages/api/src/controllers/sessions.js @@ -10,6 +10,7 @@ const processArgs = require('../utility/processArgs'); const { appdir } = require('../utility/directories'); const { getLogger } = require('dbgate-tools'); const pipeForkLogs = require('../utility/pipeForkLogs'); +const config = require('./config'); const logger = getLogger('sessions'); @@ -120,7 +121,12 @@ module.exports = { socket.emit(`session-closed-${sesid}`); }); - subprocess.send({ msgtype: 'connect', ...connection, database }); + subprocess.send({ + msgtype: 'connect', + ...connection, + database, + globalSettings: await config.getSettings(), + }); return _.pick(newOpened, ['conid', 'database', 'sesid']); }, diff --git a/packages/api/src/proc/sessionProcess.js b/packages/api/src/proc/sessionProcess.js index e8e65809..d5f60b0f 100644 --- a/packages/api/src/proc/sessionProcess.js +++ b/packages/api/src/proc/sessionProcess.js @@ -10,7 +10,7 @@ const requireEngineDriver = require('../utility/requireEngineDriver'); const { decryptConnection } = require('../utility/crypting'); const connectUtility = require('../utility/connectUtility'); const { handleProcessCommunication } = require('../utility/processComm'); -const { getLogger } = require('dbgate-tools'); +const { getLogger, extractIntSettingsValue, extractBoolSettingsValue } = require('dbgate-tools'); const logger = getLogger('sessionProcess'); @@ -19,6 +19,7 @@ let storedConnection; let afterConnectCallbacks = []; // let currentHandlers = []; let lastPing = null; +let lastActivity = null; let currentProfiler = null; class TableWriter { @@ -215,6 +216,8 @@ function waitConnected() { } async function handleStartProfiler({ jslid }) { + lastActivity = new Date().getTime(); + await waitConnected(); const driver = requireEngineDriver(storedConnection); @@ -233,6 +236,8 @@ async function handleStartProfiler({ jslid }) { } async function handleStopProfiler({ jslid }) { + lastActivity = new Date().getTime(); + const driver = requireEngineDriver(storedConnection); currentProfiler.writer.close(); driver.stopProfiler(systemConnection, currentProfiler); @@ -240,6 +245,8 @@ async function handleStopProfiler({ jslid }) { } async function handleExecuteQuery({ sql }) { + lastActivity = new Date().getTime(); + await waitConnected(); const driver = requireEngineDriver(storedConnection); @@ -273,6 +280,8 @@ async function handleExecuteQuery({ sql }) { } async function handleExecuteReader({ jslid, sql, fileName }) { + lastActivity = new Date().getTime(); + await waitConnected(); const driver = requireEngineDriver(storedConnection); @@ -331,6 +340,19 @@ function start() { logger.info('Session not alive, exiting'); process.exit(0); } + + const useSessionTimeout = + storedConnection && storedConnection.globalSettings + ? extractBoolSettingsValue(storedConnection.globalSettings, 'session.autoClose', true) + : false; + const sessionTimeout = + storedConnection && storedConnection.globalSettings + ? extractIntSettingsValue(storedConnection.globalSettings, 'session.autoCloseTimeout', 15, 1, 120) + : 15; + if (useSessionTimeout && time - lastActivity > sessionTimeout * 60 * 1000 && !currentProfiler) { + logger.info('Session not active, exiting'); + process.exit(0); + } }, 10 * 1000); process.on('message', async message => { diff --git a/packages/web/src/settings/SettingsModal.svelte b/packages/web/src/settings/SettingsModal.svelte index c111d67c..a43713e2 100644 --- a/packages/web/src/settings/SettingsModal.svelte +++ b/packages/web/src/settings/SettingsModal.svelte @@ -69,9 +69,10 @@ ORDER BY isInline tabs={[ { label: 'General', slot: 1 }, - { label: 'Themes', slot: 2 }, - { label: 'Default Actions', slot: 3 }, - { label: 'Confirmations', slot: 4 }, + { label: 'Connection', slot: 2 }, + { label: 'Themes', slot: 3 }, + { label: 'Default Actions', slot: 4 }, + { label: 'Confirmations', slot: 5 }, ]} > @@ -108,6 +109,19 @@ ORDER BY defaultValue="10" /> +
SQL editor
+ +
+
Connection
-
SQL editor
- Query sessions + +
- + +
Application theme
{#each $extensions.themes as theme} @@ -207,7 +223,7 @@ ORDER BY
- +
Default actions
- +
Confirmations