mirror of
https://github.com/affaan-m/ECC.git
synced 2026-10-20 06:51:44 +02:00
Preserve the original contributor histories and apply the exact independently reviewed repair.
Source-Parent: 5785183108
Review-Manifest-SHA256: e223d98c24e357715527b786257901d2ef0bebadb75ac61e5c000fe60da10f9a
66 lines
2.1 KiB
JavaScript
66 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
const workerThreads = require('worker_threads');
|
|
const OriginalWorker = workerThreads.Worker;
|
|
const metrics = { active: 0, peak: 0, started: 0, exited: 0, received: 0, disconnected: 0 };
|
|
function send(message) {
|
|
if (process.connected) process.send(message, error => { if (error) process.exitCode = 1; });
|
|
}
|
|
function report() { send({ type: 'workers', ...metrics }); }
|
|
workerThreads.Worker = class ObservedWorker extends OriginalWorker {
|
|
constructor(...args) {
|
|
super(...args);
|
|
metrics.active += 1;
|
|
metrics.started += 1;
|
|
metrics.peak = Math.max(metrics.peak, metrics.active);
|
|
report();
|
|
this.once('exit', () => { metrics.active -= 1; metrics.exited += 1; report(); });
|
|
}
|
|
};
|
|
const { createControlPaneServer } = require('../../../scripts/lib/control-pane/server');
|
|
|
|
async function main() {
|
|
const app = createControlPaneServer({
|
|
host: '127.0.0.1', port: 0, stateDbPath: process.argv[2],
|
|
dbPath: `${process.argv[2]}.ecc2`, allowActions: process.argv[3] !== 'read-only'
|
|
});
|
|
app.server.prependListener('request', (req, res) => {
|
|
if (req.method === 'POST') {
|
|
metrics.received += 1;
|
|
report();
|
|
send({ type: 'mutation-received' });
|
|
res.once('close', () => {
|
|
if (!res.writableFinished) { metrics.disconnected += 1; report(); }
|
|
});
|
|
}
|
|
});
|
|
let closing;
|
|
let startupFailed = false;
|
|
const close = () => {
|
|
if (!closing) closing = Promise.resolve().then(() => app.close());
|
|
return closing;
|
|
};
|
|
const finish = async () => {
|
|
try { await close(); if (!startupFailed) process.exitCode = 0; }
|
|
catch (error) { console.error(error); process.exitCode = 1; }
|
|
if (process.connected) process.disconnect();
|
|
};
|
|
process.on('disconnect', finish);
|
|
process.on('message', message => { if (message === 'close') finish(); });
|
|
try {
|
|
await app.listen();
|
|
send({ type: 'ready', url: app.url });
|
|
} catch (error) {
|
|
startupFailed = true;
|
|
try { await close(); } catch (_cleanupError) { /* Preserve startup error. */ }
|
|
throw error;
|
|
}
|
|
|
|
}
|
|
process.exitCode = 1;
|
|
main().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
if (process.connected) process.disconnect();
|
|
});
|