From 2cb38709a56d50271c836e78821b28e6a03b68f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Guti=C3=A9rrez=20Hermoso?= Date: Thu, 13 Jun 2024 17:45:41 -0400 Subject: [PATCH] supervisor: new file This is a new entrypoint, mostly intended for Docker, so we have one simple process controlling the main Grist process. The purpose of this is to be able to make Grist easily restartable with a new environment. --- sandbox/supervisor.mjs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sandbox/supervisor.mjs diff --git a/sandbox/supervisor.mjs b/sandbox/supervisor.mjs new file mode 100644 index 00000000..2508cb17 --- /dev/null +++ b/sandbox/supervisor.mjs @@ -0,0 +1,35 @@ +import {spawn} from 'child_process'; + +let grist; + +function startGrist(newConfig={}) { + saveNewConfig(newConfig); + // H/T https://stackoverflow.com/a/36995148/11352427 + grist = spawn('./sandbox/run.sh', { + stdio: ['inherit', 'inherit', 'inherit', 'ipc'] + }); + grist.on('message', function(data) { + if (data.action === 'restart') { + console.log('Restarting Grist with new environment'); + + // Note that we only set this event handler here, after we have + // a new environment to reload with. Small chance of a race here + // in case something else sends a SIGINT before we do it + // ourselves further below. + grist.on('exit', () => { + grist = startGrist(data.newConfig); + }); + + grist.kill('SIGINT'); + } + }); + return grist; +} + +// Stub function +function saveNewConfig(newConfig) { + // TODO: something here to actually persist the new config before + // restarting Grist. +} + +startGrist();