From c768665047ef4472ce53c8222cced4b20cd7b6a6 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 7 Oct 2024 18:22:13 +0200 Subject: [PATCH] Fix worker substitution issues after crash --- lib/workers/index.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/workers/index.js b/lib/workers/index.js index 55487c848..d383cb59b 100644 --- a/lib/workers/index.js +++ b/lib/workers/index.js @@ -152,7 +152,18 @@ Workers.initialize = function (Env, config, _cb) { // default to timing out affter 180s if no explicit timeout is passed var timeout = typeof(opt.timeout) !== 'undefined'? opt.timeout: DEFAULT_QUERY_TIMEOUT; response.expect(txid, cb, timeout); + + delete msg._cb; + delete msg._opt; state.worker.send(msg); + + // Add original callback to message data in case we need + // to resend the command. setTimeout to avoid interfering + // with worker.send + setTimeout(function () { + msg._cb = _cb; + msg._opt = opt; + }); }; var handleResponse = function (state, res) { @@ -237,26 +248,36 @@ Workers.initialize = function (Env, config, _cb) { const cb = response.expectation(txid); if (typeof(cb) !== 'function') { return; } const task = state.tasks[txid]; - if (!(task && task.msg)) { return; } + if (!task) { return; } response.clear(txid); - Log.info('DB_WORKER_RESEND', task.msg); - sendCommand(task.msg, cb); + Log.info('DB_WORKER_RESEND', task); + sendCommand(task, task._cb || cb, task._opt); }); var w = fork(DB_PATH); - initWorker(w, function (err, state) { + initWorker(w, function (err) { if (err) { throw new Error(err); } - workers.push(state); }); }); - worker.on('exit', substituteWorker); - worker.on('close', substituteWorker); + worker.on('exit', function () { + substituteWorker(); + Env.Log.error("DB_WORKER_EXIT", { + pid: state.pid, + }); + }); + worker.on('close', function () { + substituteWorker(); + Env.Log.error("DB_WORKER_CLOSE", { + pid: state.pid, + }); + }); worker.on('error', function (err) { substituteWorker(); Env.Log.error("DB_WORKER_ERROR", { + pid: state.pid, error: err, }); });