better process termination...

service-rework
Bronwen 2021-04-26 21:56:48 -04:00
parent 2baebc7845
commit d5994a1511
2 changed files with 65 additions and 61 deletions

View File

@ -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", "name": "valnet",
"productName": "ElectronReact", "productName": "ElectronReact",
"description": "Electron application boilerplate based on React, React Router, Webpack, React Fast Refresh for rapid application development", "description": "Electron application boilerplate based on React, React Router, Webpack, React Fast Refresh for rapid application development",
@ -28,7 +16,7 @@
"test": "jest", "test": "jest",
"supervisor": "supervisor", "supervisor": "supervisor",
"relay": "supervisor -w relay,src -n exit relay/index.mjs", "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" "sloc": "find lib -type f | xargs wc -l"
}, },
"build": { "build": {
@ -88,13 +76,13 @@
], ],
"publish": { "publish": {
"provider": "github", "provider": "github",
"owner": "electron-react-boilerplate", "owner": "marcus13345",
"repo": "electron-react-boilerplate" "repo": "valnet"
} }
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/electron-react-boilerplate/electron-react-boilerplate.git" "url": "git+https://github.com/marcus13345/valnet.git"
}, },
"author": { "author": {
"name": "Electron React Boilerplate Maintainers", "name": "Electron React Boilerplate Maintainers",
@ -103,19 +91,14 @@
}, },
"contributors": [ "contributors": [
{ {
"name": "Amila Welihinda", "name": "Valerie Gosselin",
"email": "amilajack@gmail.com", "email": "marcus@valnet.xyz",
"url": "https://github.com/amilajack" "url": "https://github.com/marcus13345"
},
{
"name": "John Tran",
"email": "jptran318@gmail.com",
"url": "https://github.com/jooohhn"
} }
], ],
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://github.com/electron-react-boilerplate/electron-react-boilerplate/issues" "url": "https://github.com/marcus13345/valnet/issues"
}, },
"keywords": [ "keywords": [
"electron", "electron",

View File

@ -1,3 +1,4 @@
// imports that arent installed... // imports that arent installed...
import { execSync, spawn } from 'child_process'; import { execSync, spawn } from 'child_process';
import { config } from '../src/lib/config/index.js'; import { config } from '../src/lib/config/index.js';
@ -15,6 +16,11 @@ async function doExternalImports() {
} }
(async function bootloader() { (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 = ""; let yarnOutput = "";
try { try {
// sanity install packages... // sanity install packages...
@ -25,6 +31,7 @@ async function doExternalImports() {
// this is also why we dynamically // this is also why we dynamically
// load the dependencies... // load the dependencies...
yarnOutput += execSync(`yarn`); yarnOutput += execSync(`yarn`);
console.log(yarnOutput);
} catch { } catch {
enterRecoveryMode(yarnOutput); enterRecoveryMode(yarnOutput);
return; return;
@ -38,12 +45,21 @@ async function doExternalImports() {
startService(); startService();
})(); })();
async function startService() { async function startService() {
const logLock = new external.Volatile({}); const logLock = new external.Volatile({});
const log = external.Signale.scope('SRVC'); const log = external.Signale.scope('SRVC');
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim(); const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
let proc; 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({ const logs = new external.Datastore({
filename: 'svc.log', filename: 'svc.log',
autoload: true autoload: true
@ -52,17 +68,14 @@ async function startService() {
external.expressWs(app); external.expressWs(app);
const logEvents = new EventEmitter(); const logEvents = new EventEmitter();
logp('=================================='); logp('===================================');
logp('Starting Valnet Node as a Service!'); logp('Starting Valnet Node as a Service!');
logp('Syncing to branch: ' + branch); logp('Syncing to branch: ' + branch);
logp('==================================='); logp('===================================');
logp('= = = = = = = = = = = = = = = = = ='); logp('Logging service started...');
logp('= = = = = = = = = = = = ');
logp('= = = = = = = = = ');
logp('= = = = = = = ');
logp('= = = = = = ');
setInterval(function update() { (function update() {
try {
const remoteHash = execSync('git ls-remote https://github.com/marcus13345/valnet.git').toString() const remoteHash = execSync('git ls-remote https://github.com/marcus13345/valnet.git').toString()
.split('\n') .split('\n')
.filter(test => { .filter(test => {
@ -83,12 +96,15 @@ async function startService() {
logp(e, 'error'); logp(e, 'error');
} }
} }
}, 5000); } catch {}
setTimeout(update, 5000);
})();
(function keepAlive() { (function keepAlive() {
proc = spawn('node', ['./relay/index.mjs'], { proc = spawn('node', ['./relay/index.mjs'], {
stdio: 'pipe', stdio: 'pipe',
env: { env: {
...process.env,
FORCE_COLOR: true FORCE_COLOR: true
} }
}); });
@ -113,10 +129,12 @@ async function startService() {
appendLogs('update', execSync(`git pull`)); appendLogs('update', execSync(`git pull`));
appendLogs('yarn', execSync(`yarn`)); appendLogs('yarn', execSync(`yarn`));
logp('restarting...') if(!suppressRestart) {
logp('restarting...');
setTimeout(() => { setTimeout(() => {
keepAlive(); keepAlive();
}, 1000); }, 1000);
}
}) })
})(); })();
@ -188,7 +206,11 @@ async function startService() {
}) })
app.get('/logs', (req, res) => { 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()); res.end(Template.realtimeLogs());
}) })
@ -236,12 +258,12 @@ function enterRecoveryMode(message) {
}).listen(config.ports.service); }).listen(config.ports.service);
} }
const Template = { const Template = {
style() {
return `<style> html { background: #0E1419; color: #F8F8F2; } </style>`
},
logs(messages) { logs(messages) {
return ` return `
<style> html { background: #0E1419; color: #F8F8F2; } </style>
<pre> <pre>
${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')} ${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')}
</pre> </pre>
@ -250,7 +272,6 @@ ${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')}
}, },
realtimeLogs() { realtimeLogs() {
return ` return `
<style> html { background: #0E1419; color: #F8F8F2; } </style>
<pre></pre> <pre></pre>
<br><br><br><br><br><br> <br><br><br><br><br><br>
<script> <script>