From: Alessandro Pio Ardizio Date: Thu, 21 May 2020 07:48:22 +0000 (+0200) Subject: Merge pull request #31 from NeoyeElf/feat/support-async-func X-Git-Tag: 1.1.0~4 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=60400df2370f4fc2fb00357f94b7ad0a0098959b;hp=23d46b3a84f250d3a5f9c20f8f875bedb5ebb871;p=poolifier.git Merge pull request #31 from NeoyeElf/feat/support-async-func [feat] support exec async func in worker threads --- diff --git a/lib/workers.js b/lib/workers.js index 103061aa..abd60b4f 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -16,6 +16,7 @@ class ThreadWorker extends AsyncResource { super('worker-thread-pool:pioardi') this.opts = opts || {} this.maxInactiveTime = this.opts.maxInactiveTime || (1000 * 60) + this.async = !!this.opts.async this.lastTask = Date.now() if (!fn) throw new Error('Fn parameter is mandatory') // keep the worker active @@ -27,7 +28,11 @@ class ThreadWorker extends AsyncResource { if (value && value.data && value._id) { // here you will receive messages // console.log('This is the main thread ' + isMainThread) - this.runInAsyncScope(this._run.bind(this), this, fn, value) + if (this.async) { + this.runInAsyncScope(this._runAsync.bind(this), this, fn, value) + } else { + this.runInAsyncScope(this._run.bind(this), this, fn, value) + } } else if (value.parent) { // save the port to communicate with the main thread // this will be received once @@ -56,6 +61,16 @@ class ThreadWorker extends AsyncResource { this.lastTask = Date.now() } } + + _runAsync (fn, value) { + fn(value.data).then(res => { + this.parent.postMessage({ data: res, _id: value._id }) + this.lastTask = Date.now() + }).catch(e => { + this.parent.postMessage({ error: e, _id: value._id }) + this.lastTask = Date.now() + }) + } } module.exports.ThreadWorker = ThreadWorker diff --git a/package-lock.json b/package-lock.json index 2ed66bfa..e345b17f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "node-thread-pool", + "name": "poolifier", "version": "1.0.0", "lockfileVersion": 1, "requires": true, diff --git a/tests/fixed.test.js b/tests/fixed.test.js index e56e15ff..2ed97444 100644 --- a/tests/fixed.test.js +++ b/tests/fixed.test.js @@ -7,6 +7,7 @@ const pool = new FixedThreadPool(numThreads, const emptyPool = new FixedThreadPool(1, './tests/workers/emptyWorker.js') const echoPool = new FixedThreadPool(1, './tests/workers/echoWorker.js') const errorPool = new FixedThreadPool(1, './tests/workers/errorWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') }) +const asyncPool = new FixedThreadPool(1, './tests/workers/asyncWorker.js') describe('Fixed thread pool test suite ', () => { it('Choose worker round robin test', async () => { @@ -54,6 +55,16 @@ describe('Fixed thread pool test suite ', () => { expect(inError.message).toBeTruthy() }) + it('Verify that async function is working properly', async () => { + const data = { f: 10 } + const startTime = new Date().getTime() + const result = await asyncPool.execute(data) + const usedTime = new Date().getTime() - startTime + expect(result).toBeTruthy() + expect(result.f).toBe(data.f) + expect(usedTime).toBeGreaterThanOrEqual(2000) + }) + it('Shutdown test', async () => { let closedThreads = 0 pool.workers.forEach(w => { diff --git a/tests/workers/asyncWorker.js b/tests/workers/asyncWorker.js new file mode 100644 index 00000000..098774e0 --- /dev/null +++ b/tests/workers/asyncWorker.js @@ -0,0 +1,10 @@ +'use strict' +const { ThreadWorker } = require('../../lib/workers') + +async function sleep (data) { + return new Promise((resolve, reject) => { + setTimeout(() => resolve(data), 2000) + }) +} + +module.exports = new ThreadWorker(sleep, { maxInactiveTime: 500, async: true })