add sessions in service

nightly
Marcus 2021-04-23 00:24:09 -04:00
parent 00485b29cd
commit 1b115dad56
3 changed files with 72 additions and 1639 deletions

View File

@ -271,7 +271,8 @@
"signale": "^1.4.0", "signale": "^1.4.0",
"source-map-support": "^0.5.19", "source-map-support": "^0.5.19",
"supervisor": "^0.12.0", "supervisor": "^0.12.0",
"uuid": "^8.3.2" "uuid": "^8.3.2",
"volatile": "^7.0.1"
}, },
"devEngines": { "devEngines": {
"node": ">=10.x", "node": ">=10.x",

View File

@ -3,7 +3,9 @@ import { execSync, spawn } from 'child_process';
import Datastore from 'nedb'; import Datastore from 'nedb';
import { config } from '../src/lib/config/index.js'; import { config } from '../src/lib/config/index.js';
import express from 'express'; import express from 'express';
import Volatile from 'volatile';
const logLock = new Volatile({});
(async () => { (async () => {
@ -56,6 +58,7 @@ setInterval(function update() {
proc = spawn('node', ['./relay/index.mjs'], { proc = spawn('node', ['./relay/index.mjs'], {
stdio: 'pipe' stdio: 'pipe'
}); });
appendLogs('relay', 'STARTED', 'event');
proc.stdout.on('data', (data) => { proc.stdout.on('data', (data) => {
process.stdout.write(data); process.stdout.write(data);
@ -68,6 +71,7 @@ setInterval(function update() {
}); });
proc.on('exit', () => { proc.on('exit', () => {
appendLogs('relay', 'STOPPED', 'event');
logp('relay exitted'); logp('relay exitted');
logp('attempting to fetch new version'); logp('attempting to fetch new version');
@ -88,16 +92,67 @@ function logp(message, type = 'info') {
} }
function appendLogs(source, data, type = 'output') { function appendLogs(source, data, type = 'output') {
logs.insert({ logLock.lock(function(lock) {
message: data.toString(), return new Promise(res => {
type: type, logs.insert({
src: source, message: data.toString(),
timestamp: new Date().getTime() type: type,
src: source,
timestamp: new Date().getTime()
}, (err, doc) => {
res(lock);
})
})
}) })
} }
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);
});
});
}
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.end('<a href="/logs">Logs</a>'); 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());
}) })
app.get('/logs', (req, res) => { app.get('/logs', (req, res) => {
@ -159,6 +214,11 @@ ${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')}
</script> </script>
</body> </body>
</html>`; </html>`;
},
Json(obj) {
return `<pre>
${JSON.stringify(obj, null, 2)}
</pre>`
} }
}; };

1636
yarn.lock

File diff suppressed because it is too large Load Diff