diff --git a/package.json b/package.json index 2bdcc23..194a35e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-parallelizer", - "version": "2.0.0", + "version": "2.1.1", "description": "A NodeJS package for running code in parallel. Initially created to provide multiprocessing in an AWS Lambda function, but it can be used in any NodeJS environment.", "main": "src/index.js", "scripts": { diff --git a/src/child-process.js b/src/child-process.js index 9178bb9..bd8edf4 100644 --- a/src/child-process.js +++ b/src/child-process.js @@ -73,7 +73,7 @@ class ChildProcess { async _processBatchesInForks(batches) { const batchesCount = batches.length; const childResponses = { - childResponses: [], + responses: [], failures: [] }; @@ -145,7 +145,7 @@ class ChildProcess { }) childResponses.failures.push(errorMessage); } else if (status == 'SUCCESS') { - childResponses.childResponses.push(reponse); + childResponses.responses.push(reponse); } if (++responsesReceived == batchesCount) { diff --git a/src/index.js b/src/index.js index f6ec06a..3e72e54 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const WorkerThreads = require("./worker-thread"); const PARALLELIZER_CHILD = 'child-process'; const PARALLELIZER_THREADS = 'worker-threads'; + class Parallelizer { constructor(params) { const parallelizer = params.type; @@ -14,4 +15,4 @@ class Parallelizer { } } } -module.exports = { ChildProcess, WorkerThreads, Parallelizer }; \ No newline at end of file +module.exports = { ChildProcess, WorkerThreads, Parallelizer, PARALLELIZER_CHILD, PARALLELIZER_THREADS }; \ No newline at end of file diff --git a/src/worker-thread.js b/src/worker-thread.js index 0b70696..9ff7937 100644 --- a/src/worker-thread.js +++ b/src/worker-thread.js @@ -48,16 +48,16 @@ class WorkerThreads { await new Promise((resolve, reject) => { for (let id = 0; id < batchesCount; id++) { const worker = new Worker(this.tmpPath, { workerData: { id, batch: batches[id] } }); - worker.on('error', reject); - worker.on('exit', (code) => { + worker.on('error', (error) => { logger({ - message: `Worker Thread #${id} exited with code: ${code}`, + message: `Thread #${id} error message: ${error.message}`, params: { thread_id: id, - exit_code: code + error_message: error.message }, debug: this.debug }) + threadResponses.failures.push(error.message); // In case a thread exists without sending a message. if (++responsesReceived == batchesCount) {