diff --git a/lib/workers/db-worker.js b/lib/workers/db-worker.js index 4f20c76a8..e6af79d62 100644 --- a/lib/workers/db-worker.js +++ b/lib/workers/db-worker.js @@ -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"); } diff --git a/lib/workers/index.js b/lib/workers/index.js index 70965d9f5..7ddbf93cf 100644 --- a/lib/workers/index.js +++ b/lib/workers/index.js @@ -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({ diff --git a/server.js b/server.js index 0bff20dca..407f10f43 100644 --- a/server.js +++ b/server.js @@ -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 () {