From 024ee70a117abc690ec1f8109b6fa460fa689e70 Mon Sep 17 00:00:00 2001 From: Jorge Miguel Lobo Escalona <47326048+jloboescalona2@users.noreply.github.com> Date: Mon, 15 Feb 2021 16:05:52 +0100 Subject: [PATCH] F #3951: FireEdge server adjustments (#809) Co-authored-by: Jorge Lobo --- src/fireedge/src/server/index.js | 22 ++++++++++++++++++- .../api/provision/provision-functions.js | 5 +---- .../src/server/routes/websockets/index.js | 5 ++++- .../routes/websockets/provision/index.js | 19 ++++++++++------ 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/fireedge/src/server/index.js b/src/fireedge/src/server/index.js index d4f9359c11..cb48abbad8 100644 --- a/src/fireedge/src/server/index.js +++ b/src/fireedge/src/server/index.js @@ -145,7 +145,7 @@ const appServer = validateServerIsSecure() ) : unsecureServer(app) -websockets(appServer) +const sockets = websockets(appServer) || [] let config = { color: 'red', @@ -164,3 +164,23 @@ appServer.listen(port, '0.0.0.0', err => { }) vmrcUpgrade(appServer) guacamole(appServer) + +function handleBreak (code) { + if (appServer && appServer.close && typeof appServer.close === 'function') { + appServer.close(() => { + // this close sockets + sockets.forEach((socket) => { + if (socket && socket.close && typeof socket.close === 'function') { + socket.close() + } + }) + process.exit(0) + }) + } +} +const eventProcess = ['SIGINT', 'SIGTERM'] +eventProcess.forEach((nameEvent = '') => { + if (nameEvent) { + process.on(nameEvent, handleBreak) + } +}) diff --git a/src/fireedge/src/server/routes/api/provision/provision-functions.js b/src/fireedge/src/server/routes/api/provision/provision-functions.js index 2214ac71bf..14dddff835 100644 --- a/src/fireedge/src/server/routes/api/provision/provision-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provision-functions.js @@ -509,9 +509,6 @@ const createProvision = (res = {}, next = () => undefined, params = {}, userData // This function is only executed if the command is completed const close = (success, lastLine) => { stream.end() - const removeConfigFile = (file) => { - removeFile(file) - } if (success && regexp.test(lastLine)) { const newPath = renameFolder(config.path, lastLine.match('\\d+'), 'replace', removeConfigFile) if (newPath) { @@ -541,7 +538,7 @@ const createProvision = (res = {}, next = () => undefined, params = {}, userData } } if (success === false) { - renameFolder(config.path, appendError, 'append', removeConfigFile) + renameFolder(config.path, appendError, 'append') } } diff --git a/src/fireedge/src/server/routes/websockets/index.js b/src/fireedge/src/server/routes/websockets/index.js index af0b2c5131..a49fed3679 100644 --- a/src/fireedge/src/server/routes/websockets/index.js +++ b/src/fireedge/src/server/routes/websockets/index.js @@ -24,6 +24,7 @@ const { // user config const websockets = (appServer = {}) => { + const sockets = [] if ( appServer && appServer.constructor && @@ -31,11 +32,12 @@ const websockets = (appServer = {}) => { appServer.constructor.name === 'Server' ) { const io = socketIO({ path: defaultEndpointWebsocket }).listen(appServer) - defaultFilesWebsockets.map(file => { + defaultFilesWebsockets.forEach(file => { try { // eslint-disable-next-line global-require const fileInfo = require(`./${file}`) if (fileInfo.main && typeof fileInfo.main === 'function') { + sockets.push(io) fileInfo.main(io) } } catch (error) { @@ -47,6 +49,7 @@ const websockets = (appServer = {}) => { } }) } + return sockets } module.exports = { diff --git a/src/fireedge/src/server/routes/websockets/provision/index.js b/src/fireedge/src/server/routes/websockets/provision/index.js index 68b1c0ac2a..f566f10561 100644 --- a/src/fireedge/src/server/routes/websockets/provision/index.js +++ b/src/fireedge/src/server/routes/websockets/provision/index.js @@ -18,11 +18,21 @@ const { messageTerminal } = require('server/utils/general') const { subscriber } = require('server/routes/api/provision/functions') const type = 'provision' const main = (app = {}) => { + const configErrorProvision = (error = '') => { + return { + color: 'red', + type: error, + message: '%s' + } + } + try { app .use(authWebsocket) .on('connection', (server = {}) => { - server.on('disconnect', () => { console.log('disconnect') }) + server.on('disconnect', () => { + messageTerminal(configErrorProvision('disconnect')) + }) subscriber( 'oneprovision', data => { @@ -31,12 +41,7 @@ const main = (app = {}) => { ) }) } catch (error) { - const configErrorProvision = { - color: 'red', - type: error, - message: '%s' - } - messageTerminal(configErrorProvision) + messageTerminal(configErrorProvision(error)) } }