X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=lib%2Fworkers.js;h=405cb3858f212a1f96fefbedf1e2677e158358bf;hb=81b32bff421a11b3ee2dfd18088717df80659908;hp=001c932ae7a92994b46f2a3e260a2affd337f87f;hpb=7784f548560f0e8957b73977137532a1372aa5cb;p=poolifier.git diff --git a/lib/workers.js b/lib/workers.js index 001c932a..405cb385 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -1,7 +1,5 @@ 'use strict' -const { - isMainThread, parentPort -} = require('worker_threads') +const { isMainThread, parentPort } = require('worker_threads') const { AsyncResource } = require('async_hooks') /** @@ -15,16 +13,19 @@ class ThreadWorker extends AsyncResource { constructor (fn, opts) { super('worker-thread-pool:pioardi') this.opts = opts || {} - this.maxInactiveTime = this.opts.maxInactiveTime || (1000 * 60) + 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 if (!isMainThread) { - this.interval = setInterval(this._checkAlive.bind(this), this.maxInactiveTime / 2) + this.interval = setInterval( + this._checkAlive.bind(this), + this.maxInactiveTime / 2 + ) this._checkAlive.bind(this)() } - parentPort.on('message', (value) => { + parentPort.on('message', value => { if (value && value.data && value._id) { // here you will receive messages // console.log('This is the main thread ' + isMainThread) @@ -46,7 +47,7 @@ class ThreadWorker extends AsyncResource { } _checkAlive () { - if ((Date.now() - this.lastTask) > this.maxInactiveTime) { + if (Date.now() - this.lastTask > this.maxInactiveTime) { this.parent.postMessage({ kill: 1 }) } } @@ -62,14 +63,16 @@ class ThreadWorker extends AsyncResource { } } - _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() - }) + _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() + }) } }