Improve tools for monitoring plugin

This commit is contained in:
yflory 2024-11-05 18:44:57 +01:00
parent e7f3816445
commit 7a9cb07bb8
3 changed files with 33 additions and 1 deletions

View File

@ -797,6 +797,19 @@ COMMANDS.VALIDATE_LOGIN_BLOCK = function (data, cb) {
Block.validateLoginBlock(Env, data.publicKey, data.signature, data.block, cb);
};
Object.keys(plugins || {}).forEach(name => {
let plugin = plugins[name];
if (!plugin.addWorkerCommands) { return; }
try {
let events = plugin.addWorkerCommands(Env);
Object.keys(events || {}).forEach(cmd => {
if (typeof(events[cmd]) !== "function") { return; }
if (COMMANDS[cmd]) { return; }
COMMANDS[cmd] = events[cmd];
});
} catch (e) {}
});
process.on('message', function (data) {
if (!data || !data.txid || !data.pid) {
return void process.send({
@ -805,6 +818,13 @@ process.on('message', function (data) {
});
}
const command = COMMANDS[data.command];
// Command broadcasted to all workers: no callback expected
if (data.type === 'broadcast') {
return void command(data);
}
const cb = function (err, value) {
process.send({
error: Util.serializeError(err),
@ -822,7 +842,6 @@ process.on('message', function (data) {
});
}
const command = COMMANDS[data.command];
if (typeof(command) !== 'function') {
return void cb("E_BAD_COMMAND");
}

View File

@ -315,6 +315,18 @@ Workers.initialize = function (Env, config, _cb) {
}));
});
}).nThen(function () {
Env.broadcastWorkerCommand = (data) => {
workers.forEach(state => {
state.worker.send({
type: 'broadcast',
pid: PID,
command: data.command,
txid: data.txid
});
});
return workers;
};
Env.computeIndex = function (Env, channel, cb) {
Env.store.getWeakLock(channel, function (next) {
sendCommand({

View File

@ -185,6 +185,7 @@ nThen(function (w) {
for (const worker of Object.values(Cluster.workers)) {
sendCommand(worker, command, data /*, cb */);
}
return Object.values(Cluster.workers);
};
var throttledEnvChange = Util.throttle(function () {