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.mjs

230 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-04-11 19:53:39 -04:00
import Signale from 'signale';
import { execSync, spawn } from 'child_process';
import Datastore from 'nedb';
import { config } from '../src/lib/config/index.js';
import express from 'express';
2021-04-23 00:24:09 -04:00
import Volatile from 'volatile';
2021-04-11 19:53:39 -04:00
2021-04-23 00:24:09 -04:00
const logLock = new Volatile({});
2021-04-11 20:36:31 -04:00
2021-02-15 09:30:56 -05:00
(async () => {
2021-04-11 19:53:39 -04:00
const log = Signale.scope('SRVC');
2021-02-14 02:15:25 -05:00
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
let proc;
2021-02-07 15:53:48 -05:00
const logs = new Datastore({
filename: 'svc.log',
autoload: true
});
2021-02-10 19:00:15 -05:00
const app = express();
2021-02-07 15:53:48 -05:00
2021-04-11 20:36:31 -04:00
2021-04-01 15:44:39 -04:00
try {
2021-04-11 20:36:31 -04:00
logp('Attempting yarn install...')
2021-04-01 15:44:39 -04:00
appendLogs('yarn', execSync(`yarn`));
} catch (e) {
2021-04-11 19:53:39 -04:00
logp('failed to yarn install...')
2021-04-01 15:44:39 -04:00
}
2021-02-07 15:53:48 -05:00
logp('==================================');
logp('Starting Valnet Node as a Service!');
2021-02-14 02:15:25 -05:00
logp('Syncing to branch: ' + branch);
2021-02-07 15:53:48 -05:00
logp('==================================');
2020-11-11 19:38:39 -05:00
setInterval(function update() {
2021-02-14 02:15:25 -05:00
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();
2020-11-11 21:02:58 -05:00
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-04-11 19:53:39 -04:00
proc = spawn('node', ['./relay/index.mjs'], {
2021-02-07 15:53:48 -05:00
stdio: 'pipe'
});
2021-04-23 00:24:09 -04:00
appendLogs('relay', 'STARTED', 'event');
2021-02-07 15:53:48 -05:00
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-04-23 00:24:09 -04:00
appendLogs('relay', 'STOPPED', 'event');
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`));
2021-02-15 09:38:19 -05:00
appendLogs('update', execSync(`git pull`));
2021-02-07 15:53:48 -05:00
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);
2021-02-15 09:30:56 -05:00
appendLogs('service', message + '\n')
2021-02-07 15:53:48 -05:00
}
2020-11-11 19:38:39 -05:00
2021-02-07 15:53:48 -05:00
function appendLogs(source, data, type = 'output') {
2021-04-23 00:24:09 -04:00
logLock.lock(function(lock) {
return new Promise(res => {
logs.insert({
message: data.toString(),
type: type,
src: source,
timestamp: new Date().getTime()
}, (err, doc) => {
res(lock);
})
})
2021-02-07 15:53:48 -05:00
})
}
2020-11-11 19:38:39 -05:00
2021-04-23 00:24:09 -04:00
function getSessions() {
return new Promise(res => {
logs.find({
type: 'event',
message: { $in: ['STARTED', 'STOPPED'] }
}, {}, (err, docs) => {
const sessions = [];
let start = null;
for(const event of docs) {
if(event.message === 'STARTED') {
if (start !== null) {
sessions.push({
started: start,
stopped: event.timestamp
});
}
start = event.timestamp;
}
if(event.message === 'STOPPED' && start !== null) {
sessions.push({
started: start,
stopped: event.timestamp
});
start = null;
}
}
sessions.sort((a, b) => a.started > b.started);
res(sessions);
});
});
}
2021-02-10 19:00:15 -05:00
app.get('/', (req, res) => {
2021-04-23 00:24:09 -04:00
res.end(`
<a href="/logs">Logs</a><br>
<a href="/api/sessions">Sessions</a><br>
<a href="/restart">Restart</a>
`);
})
app.get('/restart', async (req, res) => {
proc.kill();
res.redirect('/');
})
app.get('/api/sessions.json', async (req, res) => {
res.json(await getSessions());
2021-02-21 22:58:05 -05:00
})
app.get('/logs', (req, res) => {
res.redirect(`/logs/${Date.now() - (1000 * 60 * 60 * 24)}`)
})
app.get('/logs/:time', (req, res) => {
2021-02-10 19:00:15 -05:00
logs.find({
2021-02-21 22:58:05 -05:00
timestamp: { $gt: parseInt(req.params.time) }
2021-02-15 09:30:56 -05:00
}, {}).sort({
timestamp: -1
}).limit(100).exec((err, docs) => {
2021-02-10 19:00:15 -05:00
res.end(Template.logs(docs.reverse().map(v => v.message)));
2021-02-21 22:58:05 -05:00
2021-02-10 19:00:15 -05:00
if(err) {
res.end(err.toString());
return;
}
2021-02-15 09:30:56 -05:00
// ${new Date(logItem.timestamp).toLocaleString().padStart(40)}:
res.end();
})
});
const Template = {
logs(messages) {
return `<html>
2021-02-15 09:30:56 -05:00
<head>
<meta charset="UTF-16">
</head>
<body>
<style>
table {
border-spacing: 0px;
font-size: 13px;
}
tr {
vertical-align: top;
}
2021-02-21 23:15:54 -05:00
html {
background: #0E1419;
color: #F8F8F2;
}
2021-02-15 09:30:56 -05:00
</style>
<pre>
${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')}
2021-02-15 09:30:56 -05:00
</pre>
2021-02-21 23:15:54 -05:00
<br><br><br><br><br><br>
<script>
2021-04-01 15:10:22 -04:00
// requestAnimationFrame(_ => {
// requestAnimationFrame(_ => {
// window.scrollTo(0,document.body.scrollHeight);
// });
// });
// setTimeout(_ => {
// location.reload();
// }, 2000);
2021-02-21 23:15:54 -05:00
</script>
2021-02-15 09:30:56 -05:00
</body>
</html>`;
2021-04-23 00:24:09 -04:00
},
Json(obj) {
return `<pre>
${JSON.stringify(obj, null, 2)}
</pre>`
}
};
2020-11-11 19:38:39 -05:00
2021-02-14 02:21:35 -05:00
app.listen(config.ports.service);
2020-11-11 19:38:39 -05:00
})();