This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
valnet/relay/service.js

87 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-11-11 19:38:39 -05:00
(() => {
2020-11-11 22:06:45 -05:00
const log = require('signale').scope('service');
const { execSync, spawn } = require('child_process');
2020-11-11 19:38:39 -05:00
const branch = 'master';
let proc;
2021-02-07 15:53:48 -05:00
const Datastore = require('nedb');
const logs = new Datastore({
filename: 'svc.log',
autoload: true
});
const { config } = require('../package.json');
logp('==================================');
logp('Starting Valnet Node as a Service!');
logp('==================================');
2020-11-11 19:38:39 -05:00
setInterval(function update() {
2020-11-11 21:02:58 -05:00
const remoteHash = execSync('git ls-remote https://github.com/marcus13345/valnet.git').toString().split(/[\t\n]/g)[0].trim();
const localHash = execSync(`git rev-parse ${branch}`).toString().trim();
if(remoteHash !== localHash) {
2021-02-07 15:53:48 -05:00
logp(`remote hash: ${remoteHash}`);
logp(`local hash: ${localHash}`);
2020-11-11 21:42:23 -05:00
2021-02-07 15:53:48 -05:00
logp('killing relay...');
try {
proc.kill();
} catch (e) {
2021-02-07 15:53:48 -05:00
logp('failed to kill active relay...', 'error');
logp(e, 'error');
}
2020-11-11 21:02:58 -05:00
}
}, 5000);
2020-11-11 19:38:39 -05:00
(function keepAlive() {
2021-02-07 15:53:48 -05:00
proc = spawn('node', ['relay'], {
stdio: 'pipe'
});
proc.stdout.on('data', (data) => {
process.stdout.write(data);
appendLogs('relay', data.toString(), 'stdout');
});
proc.stderr.on('data', (data) => {
process.stderr.write(data);
appendLogs('relay', data.toString(), 'stderr');
});
2020-11-11 19:38:39 -05:00
proc.on('exit', () => {
2021-02-07 15:53:48 -05:00
logp('relay exitted');
logp('attempting to fetch new version');
2021-02-07 15:53:48 -05:00
appendLogs('fetch', execSync(`git fetch`));
appendLogs('pull', execSync(`git pull`));
appendLogs('yarn', execSync(`yarn`));
2021-02-07 15:53:48 -05:00
logp('restarting...')
setTimeout(() => {
keepAlive();
}, 1000);
})
})();
2020-11-11 19:38:39 -05:00
2021-02-07 15:53:48 -05:00
function logp(message, type = 'info') {
log[type](message);
appendLogs('service', message)
}
2020-11-11 19:38:39 -05:00
2021-02-07 15:53:48 -05:00
function appendLogs(source, data, type = 'output') {
logs.insert({
message: data.toString(),
type: type,
src: source,
timestamp: new Date().getTime()
})
}
2020-11-11 19:38:39 -05:00
})();