From 6f8fd92e49b491f0a7246c9f9322999d7091c3f3 Mon Sep 17 00:00:00 2001 From: bordun Date: Fri, 4 Dec 2020 01:22:31 +0100 Subject: [PATCH] launcher: launch server with fork instead of spawn server: let the launcher apply config file updates --- CHANGELOG.md | 3 ++- src/server/index.js | 15 ++++++++++++--- src/server/settings.js | 31 +++++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec29646d..3d115fe8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ - editor: cloned `dropdown` and `input` widgets not opening when selected - widgets: prevent value-stateless widgets (tap buttons, clone, scripts, html and svg) from sending a value for cross-client synchronization (leads to unexpected script execution) and exclude them from state saves - input: apply `decimals` number before checking the value against the `validation` expression - - range: `steps` and `dashed` property not working; remove `origin` property + - range: `steps` and `dashed` property not working; remove `origin` property + - config conflicts between launcher and server (affecting at least session history) ## 1.7.3 diff --git a/src/server/index.js b/src/server/index.js index c7fe4a8e..51171c8c 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -108,7 +108,7 @@ if (settings.cli) { path = require('path'), address = 'file://' + path.resolve(__dirname + '/../launcher/' + 'index.html'), {ipcMain} = require('electron'), - {spawn} = require('child_process'), + {fork} = require('child_process'), launcher process.on('exit',()=>{ @@ -152,7 +152,7 @@ if (settings.cli) { ipcMain.on('start',function(e, options){ - var args = ['--', app.getAppPath(), '--no-gui'] + var args = ['--no-gui'] // if (process.platform === 'win32') args.unshift('--') // not needed with ELECTRON_RUN_AS_NODE since '--' is always prepended for (var k in settings.read('options')) { @@ -165,7 +165,7 @@ if (settings.cli) { } } - global.serverProcess = spawn(process.argv[0], args, {stdio: 'pipe', env: {...process.env, 'ELECTRON_RUN_AS_NODE':'1'}}) + global.serverProcess = fork(app.getAppPath(), args, {stdio: 'pipe', env: process.env}) launcher.webContents.send('server-started') if (!settings.read('no-gui')) { @@ -186,6 +186,15 @@ if (settings.cli) { console.error(String(data).trim()) }) + + + global.serverProcess.on('message', (data) => { + var [command, args] = data + if (command === 'settings.write') { + settings.write(args[0], args[1], false) + } + }) + global.serverProcess.on('close', (code) => { console.log('(INFO) Server stopped') global.serverProcess = null diff --git a/src/server/settings.js b/src/server/settings.js index 67c387e7..cac4e2b4 100644 --- a/src/server/settings.js +++ b/src/server/settings.js @@ -148,11 +148,23 @@ module.exports = { if (!tmp && configPathExists) { - config[key] = value - fs.writeFile(configPath, JSON.stringify(config, null, 4), function(err, data) { - if (err) throw err - }) + if (process.send) { + // server running as child process of launcher + // -> just pass the modification to the launcher process + process.send(['settings.write', [key, value]]) + } else { + config[key] = value + fs.writeFile(configPath, JSON.stringify(config, null, 4), function(err, data) { + if (err) throw err + }) + } + + } + + // we are in launcher process -> update server process' config + if (global.serverProcess) { + global.serverProcess.send(['settings.write', [key, value]]) } }, @@ -161,3 +173,14 @@ module.exports = { infos: infos, appAddresses: ()=>address(module.exports.read('use-ssl') ? 'https://' : 'http://', settings.options.port || 8080) } + +if (process.send) { + // server running as child process of launcher + // update local config when the main config is modified + process.on('message', function(data) { + var [command, args] = data + if (command === 'settings.write') { + module.exports.write(args[0], args[1], true) + } + }) +}