From 026230d8e1d600a4c33884cf135f5e8e4b38f0d5 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 15 Jul 2024 11:03:07 +0200 Subject: [PATCH] custom module: add `stop()` callback (runs when the server stops --- CHANGELOG.md | 1 + .../docs/docs/custom-module/custom-module.md | 6 +++ src/server/custom-module.js | 12 ++++++ src/server/electron-app.js | 3 -- src/server/index.js | 41 +++++++++++++++---- src/server/osc/index.js | 7 ++++ 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67de9d3a..79c38c24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - custom module - provide error backtraces in module.exports functions - add `reload()` callback to provide finer control over the module's lifecycle + - add `stop()` callback (runs when the server stops) - remote control - add /SESSION/CREATE command diff --git a/resources/docs/docs/custom-module/custom-module.md b/resources/docs/docs/custom-module/custom-module.md index a79cf24e..eac6bc0a 100644 --- a/resources/docs/docs/custom-module/custom-module.md +++ b/resources/docs/docs/custom-module/custom-module.md @@ -10,6 +10,7 @@ Using the server's `custom-module` option, users can load a custom javascript mo // Do whatever you want // initialize variables // declare functions +// listen to app events // load modules // etc @@ -21,6 +22,11 @@ module.exports = { // it is called for the main module only }, + stop: function(){ + // this will be executed once when the osc server stops + // it is called for the main module only + }, + reload: function(){ // this will be executed after the custom module is reloaded // it is called for the main module only diff --git a/src/server/custom-module.js b/src/server/custom-module.js index ac8c0464..d7b369c6 100644 --- a/src/server/custom-module.js +++ b/src/server/custom-module.js @@ -210,6 +210,18 @@ class CustomModule { } } + stop() { + + if (this.module.exports && this.module.exports.stop) { + try { + this.module.exports.stop() + } catch(e) { + console.error('(ERROR, CUSTOM MODULE) Error while calling custom module stop function') + console.error(e) + } + } + } + setTimeout(fn, t) { var timeout = setTimeout(fn, t) this.timeouts.push(timeout) diff --git a/src/server/electron-app.js b/src/server/electron-app.js index 60aea880..3b9c5cb7 100644 --- a/src/server/electron-app.js +++ b/src/server/electron-app.js @@ -115,9 +115,6 @@ if (process.platform === 'darwin') { ) } -app.on('before-quit',()=>{ - process.exit() -}) app.on('ready',()=>{ var menu = Menu.buildFromTemplate(template) diff --git a/src/server/index.js b/src/server/index.js index 8dcb80df..a710e4f4 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -175,21 +175,21 @@ function startServerProcess() { function stopServerProcess() { if (settings.read('no-gui')) { - if (serverProcess) serverProcess.kill() + if (serverProcess) serverProcess.kill('SIGINT') return } var toClose = [...clientWindows].filter(w=>w && !w.isDestroyed()), closed = 0 - if (toClose.length === 0) serverProcess.kill() + if (toClose.length === 0) serverProcess.kill('SIGINT') for (var w of toClose) { w.on('closed', ()=>{ closed++ if (closed === toClose.length) { clientWindows = [] - if (serverProcess) serverProcess.kill() + if (serverProcess) serverProcess.kill('SIGINT') } }) w.close() @@ -283,10 +283,26 @@ if (settings.read('docs')) { server.bindCallbacks(callbacks) serverStarted = true - process.on('exit',()=>{ - if (osc.midi) osc.midi.stop() + + var closeServer = ()=>{ + osc.server.stop() zeroconf.unpublishAll() - }) + } + + try { + let {app} = require('electron') + app.on('ready', ()=>{ + process.on('SIGINT', function() { + closeServer() + process.exit(0) + }) + }) + } catch(e) { + process.on('SIGINT', function() { + closeServer() + process.exit(0) + }) + } if (!process.env.OSC_SERVER_PROCESS) server.eventEmitter.on('serverStarted', ()=>{ if (!settings.read('no-qrcode')) showQRCode() @@ -299,8 +315,17 @@ if (settings.read('docs')) { // - node process: server (node mode in a forked process) app = require('./electron-app') - process.on('exit',()=>{ - if (serverProcess) serverProcess.kill() + + app.on('ready', ()=>{ + process.on('SIGINT', function() { + if (serverProcess) serverProcess.kill('SIGINT') + process.exit(0) + }) + }) + + app.on('before-quit',()=>{ + console.log('PARENT PROCESS QUIT') + if (serverProcess) serverProcess.kill('SIGINT') }) diff --git a/src/server/osc/index.js b/src/server/osc/index.js index 34dc5c92..8061369d 100644 --- a/src/server/osc/index.js +++ b/src/server/osc/index.js @@ -217,6 +217,13 @@ class OscServer { } + stop() { + + if (this.customModule.stop) this.customModule.stop() + if (midi) midi.stop() + + } + }