From d5994a15118d572bd4213fa5418cc3681922386c Mon Sep 17 00:00:00 2001 From: Bronwen Date: Mon, 26 Apr 2021 21:56:48 -0400 Subject: [PATCH] better process termination... --- package.json | 33 ++++------------- relay/service.mjs | 93 +++++++++++++++++++++++++++++------------------ 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/package.json b/package.json index 50f0db6..5e79ad0 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,4 @@ { - "config": { - "ports": { - "relay": 5600, - "relayEnd": 5699, - "http": 5700, - "service": 5000 - }, - "endpoints": [ - "valnet.xyz:5500", - "35.196.210.135:5600" - ] - }, "name": "valnet", "productName": "ElectronReact", "description": "Electron application boilerplate based on React, React Router, Webpack, React Fast Refresh for rapid application development", @@ -28,7 +16,7 @@ "test": "jest", "supervisor": "supervisor", "relay": "supervisor -w relay,src -n exit relay/index.mjs", - "relay:service": "supervisor -w relay,src -- relay/service.mjs", + "relay:service": "supervisor -w relay,src -n exit -- relay/service.mjs", "sloc": "find lib -type f | xargs wc -l" }, "build": { @@ -88,13 +76,13 @@ ], "publish": { "provider": "github", - "owner": "electron-react-boilerplate", - "repo": "electron-react-boilerplate" + "owner": "marcus13345", + "repo": "valnet" } }, "repository": { "type": "git", - "url": "git+https://github.com/electron-react-boilerplate/electron-react-boilerplate.git" + "url": "git+https://github.com/marcus13345/valnet.git" }, "author": { "name": "Electron React Boilerplate Maintainers", @@ -103,19 +91,14 @@ }, "contributors": [ { - "name": "Amila Welihinda", - "email": "amilajack@gmail.com", - "url": "https://github.com/amilajack" - }, - { - "name": "John Tran", - "email": "jptran318@gmail.com", - "url": "https://github.com/jooohhn" + "name": "Valerie Gosselin", + "email": "marcus@valnet.xyz", + "url": "https://github.com/marcus13345" } ], "license": "MIT", "bugs": { - "url": "https://github.com/electron-react-boilerplate/electron-react-boilerplate/issues" + "url": "https://github.com/marcus13345/valnet/issues" }, "keywords": [ "electron", diff --git a/relay/service.mjs b/relay/service.mjs index 8d4e19d..f6d1480 100644 --- a/relay/service.mjs +++ b/relay/service.mjs @@ -1,3 +1,4 @@ + // imports that arent installed... import { execSync, spawn } from 'child_process'; import { config } from '../src/lib/config/index.js'; @@ -15,6 +16,11 @@ async function doExternalImports() { } (async function bootloader() { + console.log('==================================='); + console.log(' Valnet Relay Service Bootloader'); + console.log('==================================='); + console.log('These messages won\'t appear in the logs...') + console.log('installing packages...'); let yarnOutput = ""; try { // sanity install packages... @@ -25,6 +31,7 @@ async function doExternalImports() { // this is also why we dynamically // load the dependencies... yarnOutput += execSync(`yarn`); + console.log(yarnOutput); } catch { enterRecoveryMode(yarnOutput); return; @@ -38,12 +45,21 @@ async function doExternalImports() { startService(); })(); - async function startService() { const logLock = new external.Volatile({}); const log = external.Signale.scope('SRVC'); const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim(); let proc; + let suppressRestart = false; + process.on('SIGTERM', () => { + if(proc) { + logp('killing child process'); + suppressRestart = true; + proc.kill(); + } + + process.exit(0); + }); const logs = new external.Datastore({ filename: 'svc.log', autoload: true @@ -52,43 +68,43 @@ async function startService() { external.expressWs(app); const logEvents = new EventEmitter(); - logp('=================================='); + logp('==================================='); logp('Starting Valnet Node as a Service!'); logp('Syncing to branch: ' + branch); logp('==================================='); - logp('= = = = = = = = = = = = = = = = = ='); - logp('= = = = = = = = = = = = '); - logp('= = = = = = = = = '); - logp('= = = = = = = '); - logp('= = = = = = '); + logp('Logging service started...'); - setInterval(function update() { - const remoteHash = execSync('git ls-remote https://github.com/marcus13345/valnet.git').toString() - .split('\n') - .filter(test => { - return test.trim().endsWith(branch); - })[0] - .split('\t')[0] - .trim(); - const localHash = execSync(`git rev-parse ${branch}`).toString().trim(); - if(remoteHash !== localHash) { - logp(`remote hash: ${remoteHash}`); - logp(`local hash: ${localHash}`); + (function update() { + try { + const remoteHash = execSync('git ls-remote https://github.com/marcus13345/valnet.git').toString() + .split('\n') + .filter(test => { + return test.trim().endsWith(branch); + })[0] + .split('\t')[0] + .trim(); + const localHash = execSync(`git rev-parse ${branch}`).toString().trim(); + if(remoteHash !== localHash) { + logp(`remote hash: ${remoteHash}`); + logp(`local hash: ${localHash}`); - logp('killing relay...'); - try { - proc.kill(); - } catch (e) { - logp('failed to kill active relay...', 'error'); - logp(e, 'error'); + logp('killing relay...'); + try { + proc.kill(); + } catch (e) { + logp('failed to kill active relay...', 'error'); + logp(e, 'error'); + } } - } - }, 5000); + } catch {} + setTimeout(update, 5000); + })(); (function keepAlive() { proc = spawn('node', ['./relay/index.mjs'], { stdio: 'pipe', env: { + ...process.env, FORCE_COLOR: true } }); @@ -113,10 +129,12 @@ async function startService() { appendLogs('update', execSync(`git pull`)); appendLogs('yarn', execSync(`yarn`)); - logp('restarting...') - setTimeout(() => { - keepAlive(); - }, 1000); + if(!suppressRestart) { + logp('restarting...'); + setTimeout(() => { + keepAlive(); + }, 1000); + } }) })(); @@ -188,7 +206,11 @@ async function startService() { }) app.get('/logs', (req, res) => { - // res.redirect(`/logs/${Date.now() - (1000 * 60 * 60 * 24)}`) + const sessions = getSessions(); + + }); + + app.get('/logs/:start', (req, res) => { res.end(Template.realtimeLogs()); }) @@ -236,12 +258,12 @@ function enterRecoveryMode(message) { }).listen(config.ports.service); } - - const Template = { + style() { + return `` + }, logs(messages) { return ` -
 ${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')}
 		
@@ -250,7 +272,6 @@ ${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')} }, realtimeLogs() { return ` -