poolifier
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -8,2 +8,8 @@ # Changelog | ||
## [2.0.2] - 2021-12-05 | ||
### Bug fixes | ||
- Fix `busy` event emission on fixed pool type | ||
## [2.0.1] - 2021-16-03 | ||
@@ -10,0 +16,0 @@ |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("events"),t=require("cluster"),r=require("worker_threads"),s=require("async_hooks");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i,n=o(e);!function(e){e.FIXED="fixed",e.DYNAMIC="dynamic"}(i||(i={}));class a extends n.default{}const h=Object.freeze({SOFT:"SOFT",HARD:"HARD"});const c=Object.freeze({ROUND_ROBIN:"ROUND_ROBIN",LESS_RECENTLY_USED:"LESS_RECENTLY_USED"});class k{constructor(e){this.pool=e,this.nextWorkerIndex=0}choose(){const e=this.pool.workers[this.nextWorkerIndex];return this.nextWorkerIndex=this.pool.workers.length-1===this.nextWorkerIndex?0:this.nextWorkerIndex+1,e}}class l{constructor(e){this.pool=e}choose(){const e=this.pool.type===i.DYNAMIC;let t,r=1/0;for(const[s,o]of this.pool.tasks){if(!e&&0===o)return s;o<r&&(t=s,r=o)}return t}}class u{constructor(e,t,r=c.ROUND_ROBIN){this.pool=e,this.createDynamicallyWorkerCallback=t,this.workerChoiceStrategy=d.getWorkerChoiceStrategy(this.pool,r)}choose(){const e=this.pool.findFreeTasksMapEntry();return e?e[0]:this.pool.busy?this.workerChoiceStrategy.choose():this.createDynamicallyWorkerCallback()}}class p{constructor(e,t,r=c.ROUND_ROBIN){this.pool=e,this.createDynamicallyWorkerCallback=t,this.setWorkerChoiceStrategy(r)}getPoolWorkerChoiceStrategy(e=c.ROUND_ROBIN){return this.pool.type===i.DYNAMIC?new u(this.pool,this.createDynamicallyWorkerCallback,e):d.getWorkerChoiceStrategy(this.pool,e)}setWorkerChoiceStrategy(e){this.workerChoiceStrategy=this.getPoolWorkerChoiceStrategy(e)}execute(){return this.workerChoiceStrategy.choose()}}class d{static getWorkerChoiceStrategy(e,t=c.ROUND_ROBIN){switch(t){case c.ROUND_ROBIN:return new k(e);case c.LESS_RECENTLY_USED:return new l(e);default:throw new Error(`Worker choice strategy '${t}' not found`)}}}const w=()=>{};class W{constructor(e,t,r){if(this.numberOfWorkers=e,this.filePath=t,this.opts=r,this.workers=[],this.tasks=new Map,this.promiseMap=new Map,this.nextMessageId=0,!this.isMain())throw new Error("Cannot start a pool from a worker!");this.checkNumberOfWorkers(this.numberOfWorkers),this.checkFilePath(this.filePath),this.checkPoolOptions(this.opts),this.setupHook();for(let e=1;e<=this.numberOfWorkers;e++)this.createAndSetupWorker();this.opts.enableEvents&&(this.emitter=new a),this.workerChoiceStrategyContext=new p(this,(()=>{const e=this.createAndSetupWorker();return this.registerWorkerMessageListener(e,(t=>{const r=this.tasks.get(e);var s;s=h.HARD,(t.kill===s||0===r)&&this.destroyWorker(e)})),e}),this.opts.workerChoiceStrategy)}checkFilePath(e){if(!e)throw new Error("Please specify a file with a worker implementation")}checkNumberOfWorkers(e){if(null==e)throw new Error("Cannot instantiate a pool without specifying the number of workers");if(!Number.isSafeInteger(e))throw new Error("Cannot instantiate a pool with a non integer number of workers");if(e<0)throw new Error("Cannot instantiate a pool with a negative number of workers");if(this.type===i.FIXED&&0===e)throw new Error("Cannot instantiate a fixed pool with no worker")}checkPoolOptions(e){var t,r;this.opts.workerChoiceStrategy=null!==(t=e.workerChoiceStrategy)&&void 0!==t?t:c.ROUND_ROBIN,this.opts.enableEvents=null===(r=e.enableEvents)||void 0===r||r}get numberOfRunningTasks(){return this.promiseMap.size}setWorkerChoiceStrategy(e){this.opts.workerChoiceStrategy=e,this.workerChoiceStrategyContext.setWorkerChoiceStrategy(e)}internalGetBusyStatus(){return this.numberOfRunningTasks>=this.numberOfWorkers&&!1===this.findFreeTasksMapEntry()}findFreeTasksMapEntry(){for(const[e,t]of this.tasks)if(0===t)return[e,t];return!1}execute(e){const t=this.chooseWorker();this.increaseWorkersTask(t),this.checkAndEmitBusy();const r=++this.nextMessageId,s=this.internalExecute(t,r);return this.sendToWorker(t,{data:e||{},id:r}),s}async destroy(){await Promise.all(this.workers.map((e=>this.destroyWorker(e))))}setupHook(){}increaseWorkersTask(e){this.stepWorkerNumberOfTasks(e,1)}decreaseWorkersTasks(e){this.stepWorkerNumberOfTasks(e,-1)}stepWorkerNumberOfTasks(e,t){const r=this.tasks.get(e);if(void 0===r)throw Error("Worker could not be found in tasks map");this.tasks.set(e,r+t)}removeWorker(e){const t=this.workers.indexOf(e);this.workers.splice(t,1),this.tasks.delete(e)}chooseWorker(){return this.workerChoiceStrategyContext.execute()}internalExecute(e,t){return new Promise(((r,s)=>{this.promiseMap.set(t,{resolve:r,reject:s,worker:e})}))}createAndSetupWorker(){var e,t,r;const s=this.createWorker();return s.on("error",null!==(e=this.opts.errorHandler)&&void 0!==e?e:w),s.on("online",null!==(t=this.opts.onlineHandler)&&void 0!==t?t:w),s.on("exit",null!==(r=this.opts.exitHandler)&&void 0!==r?r:w),s.once("exit",(()=>this.removeWorker(s))),this.workers.push(s),this.tasks.set(s,0),this.afterWorkerSetup(s),s}workerListener(){return e=>{if(e.id){const t=this.promiseMap.get(e.id);t&&(this.decreaseWorkersTasks(t.worker),e.error?t.reject(e.error):t.resolve(e.data),this.promiseMap.delete(e.id))}}}checkAndEmitBusy(){var e;this.opts.enableEvents&&this.busy&&(null===(e=this.emitter)||void 0===e||e.emit("busy"))}}class y extends W{constructor(e,t,r={}){super(e,t,r),this.opts=r}setupHook(){t.setupMaster({exec:this.filePath})}isMain(){return t.isMaster}destroyWorker(e){this.sendToWorker(e,{kill:1}),e.kill()}sendToWorker(e,t){e.send(t)}registerWorkerMessageListener(e,t){e.on("message",t)}createWorker(){return t.fork(this.opts.env)}afterWorkerSetup(e){this.registerWorkerMessageListener(e,super.workerListener())}get type(){return i.FIXED}get busy(){return this.internalGetBusyStatus()}}class g extends W{constructor(e,t,r={}){super(e,t,r)}isMain(){return r.isMainThread}async destroyWorker(e){this.sendToWorker(e,{kill:1}),await e.terminate()}sendToWorker(e,t){e.postMessage(t)}registerWorkerMessageListener(e,t){var r;null===(r=e.port2)||void 0===r||r.on("message",t)}createWorker(){return new r.Worker(this.filePath,{env:r.SHARE_ENV})}afterWorkerSetup(e){const{port1:t,port2:s}=new r.MessageChannel;e.postMessage({parent:t},[t]),e.port1=t,e.port2=s,this.registerWorkerMessageListener(e,super.workerListener())}get type(){return i.FIXED}get busy(){return this.internalGetBusyStatus()}}const m=h.SOFT;class f extends s.AsyncResource{constructor(e,t,r,s,o={killBehavior:m,maxInactiveTime:6e4}){var i,n,a;super(e),this.mainWorker=s,this.opts=o,this.killBehavior=null!==(i=this.opts.killBehavior)&&void 0!==i?i:m,this.maxInactiveTime=null!==(n=this.opts.maxInactiveTime)&&void 0!==n?n:6e4,this.async=!!this.opts.async,this.lastTask=Date.now(),this.checkFunctionInput(r),t||(this.interval=setInterval(this.checkAlive.bind(this),this.maxInactiveTime/2),this.checkAlive.bind(this)()),null===(a=this.mainWorker)||void 0===a||a.on("message",(e=>{(null==e?void 0:e.data)&&e.id?this.async?this.runInAsyncScope(this.runAsync.bind(this),this,r,e):this.runInAsyncScope(this.run.bind(this),this,r,e):e.parent?this.mainWorker=e.parent:e.kill&&(this.interval&&clearInterval(this.interval),this.emitDestroy())}))}checkFunctionInput(e){if(!e)throw new Error("fn parameter is mandatory")}getMainWorker(){if(!this.mainWorker)throw new Error("Main worker was not set");return this.mainWorker}checkAlive(){Date.now()-this.lastTask>this.maxInactiveTime&&this.sendToMainWorker({kill:this.killBehavior})}handleError(e){return e}run(e,t){try{const r=e(t.data);this.sendToMainWorker({data:r,id:t.id}),this.lastTask=Date.now()}catch(e){const r=this.handleError(e);this.sendToMainWorker({error:r,id:t.id}),this.lastTask=Date.now()}}runAsync(e,t){e(t.data).then((e=>(this.sendToMainWorker({data:e,id:t.id}),this.lastTask=Date.now(),null))).catch((e=>{const r=this.handleError(e);this.sendToMainWorker({error:r,id:t.id}),this.lastTask=Date.now()}))}}exports.AbstractWorker=f,exports.ClusterWorker=class extends f{constructor(e,r={}){super("worker-cluster-pool:pioardi",t.isMaster,e,t.worker,r)}sendToMainWorker(e){this.getMainWorker().send(e)}handleError(e){return e instanceof Error?e.message:e}},exports.DynamicClusterPool=class extends y{constructor(e,t,r,s={}){super(e,r,s),this.max=t}get type(){return i.DYNAMIC}get busy(){return this.workers.length===this.max}},exports.DynamicThreadPool=class extends g{constructor(e,t,r,s={}){super(e,r,s),this.max=t}get type(){return i.DYNAMIC}get busy(){return this.workers.length===this.max}},exports.FixedClusterPool=y,exports.FixedThreadPool=g,exports.KillBehaviors=h,exports.ThreadWorker=class extends f{constructor(e,t={}){super("worker-thread-pool:pioardi",r.isMainThread,e,r.parentPort,t)}sendToMainWorker(e){this.getMainWorker().postMessage(e)}},exports.WorkerChoiceStrategies=c; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("events"),r=require("cluster"),t=require("worker_threads"),s=require("async_hooks");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i,n=o(e);!function(e){e.FIXED="fixed",e.DYNAMIC="dynamic"}(i||(i={}));class a extends n.default{}const h=()=>{},c=Object.freeze({SOFT:"SOFT",HARD:"HARD"});const l=Object.freeze({ROUND_ROBIN:"ROUND_ROBIN",LESS_RECENTLY_USED:"LESS_RECENTLY_USED"});class k{constructor(e){this.pool=e,this.nextWorkerIndex=0}choose(){const e=this.pool.workers[this.nextWorkerIndex];return this.nextWorkerIndex=this.pool.workers.length-1===this.nextWorkerIndex?0:this.nextWorkerIndex+1,e}}class u{constructor(e){this.pool=e}choose(){const e=this.pool.type===i.DYNAMIC;let r,t=1/0;for(const[s,o]of this.pool.tasks){if(!e&&0===o)return s;o<t&&(r=s,t=o)}return r}}class p{constructor(e,r,t=l.ROUND_ROBIN){this.pool=e,this.createDynamicallyWorkerCallback=r,this.workerChoiceStrategy=y.getWorkerChoiceStrategy(this.pool,t)}choose(){const e=this.pool.findFreeTasksMapEntry();return e?e[0]:this.pool.busy?this.workerChoiceStrategy.choose():this.createDynamicallyWorkerCallback()}}class d{constructor(e,r,t=l.ROUND_ROBIN){this.pool=e,this.createDynamicallyWorkerCallback=r,this.setWorkerChoiceStrategy(t)}getPoolWorkerChoiceStrategy(e=l.ROUND_ROBIN){return this.pool.type===i.DYNAMIC?new p(this.pool,this.createDynamicallyWorkerCallback,e):y.getWorkerChoiceStrategy(this.pool,e)}setWorkerChoiceStrategy(e){this.workerChoiceStrategy=this.getPoolWorkerChoiceStrategy(e)}execute(){return this.workerChoiceStrategy.choose()}}class y{static getWorkerChoiceStrategy(e,r=l.ROUND_ROBIN){switch(r){case l.ROUND_ROBIN:return new k(e);case l.LESS_RECENTLY_USED:return new u(e);default:throw new Error(`Worker choice strategy '${r}' not found`)}}}class W{constructor(e,r,t){if(this.numberOfWorkers=e,this.filePath=r,this.opts=t,this.workers=[],this.tasks=new Map,this.promiseMap=new Map,this.nextMessageId=0,!this.isMain())throw new Error("Cannot start a pool from a worker!");this.checkNumberOfWorkers(this.numberOfWorkers),this.checkFilePath(this.filePath),this.checkPoolOptions(this.opts),this.setupHook();for(let e=1;e<=this.numberOfWorkers;e++)this.createAndSetupWorker();this.opts.enableEvents&&(this.emitter=new a),this.workerChoiceStrategyContext=new d(this,(()=>{const e=this.createAndSetupWorker();return this.registerWorkerMessageListener(e,(async r=>{const t=this.tasks.get(e);var s;s=c.HARD,(r.kill===s||0===t)&&await this.destroyWorker(e)})),e}),this.opts.workerChoiceStrategy)}checkFilePath(e){if(!e)throw new Error("Please specify a file with a worker implementation")}checkNumberOfWorkers(e){if(null==e)throw new Error("Cannot instantiate a pool without specifying the number of workers");if(!Number.isSafeInteger(e))throw new Error("Cannot instantiate a pool with a non integer number of workers");if(e<0)throw new Error("Cannot instantiate a pool with a negative number of workers");if(this.type===i.FIXED&&0===e)throw new Error("Cannot instantiate a fixed pool with no worker")}checkPoolOptions(e){var r,t;this.opts.workerChoiceStrategy=null!==(r=e.workerChoiceStrategy)&&void 0!==r?r:l.ROUND_ROBIN,this.opts.enableEvents=null===(t=e.enableEvents)||void 0===t||t}get numberOfRunningTasks(){return this.promiseMap.size}setWorkerChoiceStrategy(e){this.opts.workerChoiceStrategy=e,this.workerChoiceStrategyContext.setWorkerChoiceStrategy(e)}internalGetBusyStatus(){return this.numberOfRunningTasks>=this.numberOfWorkers&&!1===this.findFreeTasksMapEntry()}findFreeTasksMapEntry(){for(const[e,r]of this.tasks)if(0===r)return[e,r];return!1}execute(e){const r=this.chooseWorker(),t=++this.nextMessageId,s=this.internalExecute(r,t);return this.checkAndEmitBusy(),this.sendToWorker(r,{data:e||{},id:t}),s}async destroy(){await Promise.all(this.workers.map((e=>this.destroyWorker(e))))}setupHook(){}increaseWorkersTask(e){this.stepWorkerNumberOfTasks(e,1)}decreaseWorkersTasks(e){this.stepWorkerNumberOfTasks(e,-1)}stepWorkerNumberOfTasks(e,r){const t=this.tasks.get(e);if(void 0===t)throw Error("Worker could not be found in tasks map");this.tasks.set(e,t+r)}removeWorker(e){const r=this.workers.indexOf(e);this.workers.splice(r,1),this.tasks.delete(e)}chooseWorker(){return this.workerChoiceStrategyContext.execute()}internalExecute(e,r){return this.increaseWorkersTask(e),new Promise(((t,s)=>{this.promiseMap.set(r,{resolve:t,reject:s,worker:e})}))}createAndSetupWorker(){var e,r,t;const s=this.createWorker();return s.on("error",null!==(e=this.opts.errorHandler)&&void 0!==e?e:h),s.on("online",null!==(r=this.opts.onlineHandler)&&void 0!==r?r:h),s.on("exit",null!==(t=this.opts.exitHandler)&&void 0!==t?t:h),s.once("exit",(()=>this.removeWorker(s))),this.workers.push(s),this.tasks.set(s,0),this.afterWorkerSetup(s),s}workerListener(){return e=>{if(e.id){const r=this.promiseMap.get(e.id);r&&(this.decreaseWorkersTasks(r.worker),e.error?r.reject(e.error):r.resolve(e.data),this.promiseMap.delete(e.id))}}}checkAndEmitBusy(){var e;this.opts.enableEvents&&this.busy&&(null===(e=this.emitter)||void 0===e||e.emit("busy"))}}class w extends W{constructor(e,r,t={}){super(e,r,t),this.opts=t}setupHook(){r.setupMaster({exec:this.filePath})}isMain(){return r.isMaster}destroyWorker(e){this.sendToWorker(e,{kill:1}),e.kill()}sendToWorker(e,r){e.send(r)}registerWorkerMessageListener(e,r){e.on("message",r)}createWorker(){return r.fork(this.opts.env)}afterWorkerSetup(e){this.registerWorkerMessageListener(e,super.workerListener())}get type(){return i.FIXED}get busy(){return this.internalGetBusyStatus()}}class g extends W{constructor(e,r,t={}){super(e,r,t)}isMain(){return t.isMainThread}async destroyWorker(e){this.sendToWorker(e,{kill:1}),await e.terminate()}sendToWorker(e,r){e.postMessage(r)}registerWorkerMessageListener(e,r){var t;null===(t=e.port2)||void 0===t||t.on("message",r)}createWorker(){return new t.Worker(this.filePath,{env:t.SHARE_ENV})}afterWorkerSetup(e){const{port1:r,port2:s}=new t.MessageChannel;e.postMessage({parent:r},[r]),e.port1=r,e.port2=s,this.registerWorkerMessageListener(e,super.workerListener())}get type(){return i.FIXED}get busy(){return this.internalGetBusyStatus()}}const m=c.SOFT;class f extends s.AsyncResource{constructor(e,r,t,s,o={killBehavior:m,maxInactiveTime:6e4}){var i,n,a;super(e),this.mainWorker=s,this.opts=o,this.killBehavior=null!==(i=this.opts.killBehavior)&&void 0!==i?i:m,this.maxInactiveTime=null!==(n=this.opts.maxInactiveTime)&&void 0!==n?n:6e4,this.async=!!this.opts.async,this.lastTask=Date.now(),this.checkFunctionInput(t),r||(this.interval=setInterval(this.checkAlive.bind(this),this.maxInactiveTime/2),this.checkAlive.bind(this)()),null===(a=this.mainWorker)||void 0===a||a.on("message",(e=>{(null==e?void 0:e.data)&&e.id?this.async?this.runInAsyncScope(this.runAsync.bind(this),this,t,e):this.runInAsyncScope(this.run.bind(this),this,t,e):e.parent?this.mainWorker=e.parent:e.kill&&(this.interval&&clearInterval(this.interval),this.emitDestroy())}))}checkFunctionInput(e){if(!e)throw new Error("fn parameter is mandatory")}getMainWorker(){if(!this.mainWorker)throw new Error("Main worker was not set");return this.mainWorker}checkAlive(){Date.now()-this.lastTask>this.maxInactiveTime&&this.sendToMainWorker({kill:this.killBehavior})}handleError(e){return e}run(e,r){try{const t=e(r.data);this.sendToMainWorker({data:t,id:r.id})}catch(e){const t=this.handleError(e);this.sendToMainWorker({error:t,id:r.id})}finally{this.lastTask=Date.now()}}runAsync(e,r){e(r.data).then((e=>(this.sendToMainWorker({data:e,id:r.id}),null))).catch((e=>{const t=this.handleError(e);this.sendToMainWorker({error:t,id:r.id})})).finally((()=>{this.lastTask=Date.now()})).catch(h)}}exports.AbstractWorker=f,exports.ClusterWorker=class extends f{constructor(e,t={}){super("worker-cluster-pool:pioardi",r.isMaster,e,r.worker,t)}sendToMainWorker(e){this.getMainWorker().send(e)}handleError(e){return e instanceof Error?e.message:e}},exports.DynamicClusterPool=class extends w{constructor(e,r,t,s={}){super(e,t,s),this.max=r}get type(){return i.DYNAMIC}get busy(){return this.workers.length===this.max}},exports.DynamicThreadPool=class extends g{constructor(e,r,t,s={}){super(e,t,s),this.max=r}get type(){return i.DYNAMIC}get busy(){return this.workers.length===this.max}},exports.FixedClusterPool=w,exports.FixedThreadPool=g,exports.KillBehaviors=c,exports.ThreadWorker=class extends f{constructor(e,r={}){super("worker-thread-pool:pioardi",t.isMainThread,e,t.parentPort,r)}sendToMainWorker(e){this.getMainWorker().postMessage(e)}},exports.WorkerChoiceStrategies=l; |
@@ -41,10 +41,15 @@ /// <reference types="node" /> | ||
constructor(numberOfWorkers: number, filePath: string, opts?: ClusterPoolOptions); | ||
/** @inheritdoc */ | ||
protected setupHook(): void; | ||
/** @inheritdoc */ | ||
protected isMain(): boolean; | ||
/** @inheritdoc */ | ||
destroyWorker(worker: Worker): void; | ||
/** @inheritdoc */ | ||
protected sendToWorker(worker: Worker, message: MessageValue<Data>): void; | ||
/** @inheritdoc */ | ||
registerWorkerMessageListener<Message extends Data | Response>(worker: Worker, listener: (message: MessageValue<Message>) => void): void; | ||
/** @inheritdoc */ | ||
protected createWorker(): Worker; | ||
/** @inheritdoc */ | ||
protected afterWorkerSetup(worker: Worker): void; | ||
@@ -51,0 +56,0 @@ /** @inheritdoc */ |
@@ -33,9 +33,13 @@ /// <reference types="node" /> | ||
constructor(numberOfThreads: number, filePath: string, opts?: PoolOptions<ThreadWorkerWithMessageChannel>); | ||
/** @inheritdoc */ | ||
protected isMain(): boolean; | ||
/** @inheritdoc */ | ||
destroyWorker(worker: ThreadWorkerWithMessageChannel): Promise<void>; | ||
/** @inheritdoc */ | ||
protected sendToWorker(worker: ThreadWorkerWithMessageChannel, message: MessageValue<Data>): void; | ||
/** @inheritdoc */ | ||
registerWorkerMessageListener<Message extends Data | Response>(messageChannel: ThreadWorkerWithMessageChannel, listener: (message: MessageValue<Message>) => void): void; | ||
/** @inheritdoc */ | ||
protected createWorker(): ThreadWorkerWithMessageChannel; | ||
/** @inheritdoc */ | ||
protected afterWorkerSetup(worker: ThreadWorkerWithMessageChannel): void; | ||
@@ -42,0 +46,0 @@ /** @inheritdoc */ |
@@ -29,4 +29,6 @@ /// <reference types="node" /> | ||
constructor(fn: (data: Data) => Response, opts?: WorkerOptions); | ||
/** @inheritdoc */ | ||
protected sendToMainWorker(message: MessageValue<Response>): void; | ||
/** @inheritdoc */ | ||
protected handleError(e: Error | string): string; | ||
} |
@@ -29,3 +29,4 @@ /// <reference types="node" /> | ||
constructor(fn: (data: Data) => Response, opts?: WorkerOptions); | ||
/** @inheritdoc */ | ||
protected sendToMainWorker(message: MessageValue<Response>): void; | ||
} |
{ | ||
"name": "poolifier", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A fast, easy to use Node.js Worker Thread Pool and Cluster Pool implementation", | ||
@@ -66,26 +66,27 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@types/node": "^14.14.35", | ||
"@typescript-eslint/eslint-plugin": "^4.18.0", | ||
"@typescript-eslint/parser": "^4.18.0", | ||
"@types/node": "^14.14.44", | ||
"@typescript-eslint/eslint-plugin": "^4.23.0", | ||
"@typescript-eslint/parser": "^4.23.0", | ||
"benchmark": "^2.1.4", | ||
"eslint": "^7.22.0", | ||
"eslint": "^7.26.0", | ||
"eslint-config-standard": "^16.0.2", | ||
"eslint-define-config": "^1.0.8", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jsdoc": "^32.3.0", | ||
"eslint-plugin-jsdoc": "^34.0.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettierx": "^0.17.1", | ||
"eslint-plugin-promise": "^4.3.1", | ||
"eslint-plugin-promise": "^5.1.0", | ||
"eslint-plugin-spellcheck": "0.0.17", | ||
"expect": "^26.6.2", | ||
"microtime": "^3.0.0", | ||
"mocha": "^8.3.2", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"mocha": "^8.4.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.2.1", | ||
"prettier-plugin-organize-imports": "^1.1.1", | ||
"prettierx": "^0.17.0", | ||
"rollup": "^2.41.3", | ||
"prettier": "^2.3.0", | ||
"prettier-plugin-organize-imports": "^2.0.0", | ||
"prettierx": "^0.18.0", | ||
"rollup": "^2.47.0", | ||
"rollup-plugin-analyzer": "^4.0.0", | ||
"rollup-plugin-command": "^1.1.3", | ||
"rollup-plugin-delete": "^2.0.0", | ||
"rollup-plugin-istanbul": "^3.0.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
@@ -95,4 +96,4 @@ "rollup-plugin-typescript2": "^0.30.0", | ||
"source-map-support": "^0.5.19", | ||
"typedoc": "^0.20.32", | ||
"typescript": "^4.2.3" | ||
"typedoc": "^0.20.36", | ||
"typescript": "^4.2.4" | ||
}, | ||
@@ -99,0 +100,0 @@ "engines": { |
@@ -149,3 +149,3 @@ <div align="center"> | ||
You can use node versions 12.x, 13.x, 14.x | ||
You can use node versions 12.x, 13.x, 14.x, 16.x | ||
@@ -156,10 +156,18 @@ ## API | ||
`numberOfThreads/numberOfWorkers` (mandatory) Num of workers for this worker pool | ||
`numberOfThreads/numberOfWorkers` (mandatory) Number of workers for this pool | ||
`filePath` (mandatory) Path to a file with a worker implementation | ||
`opts` (optional) An object with these properties : | ||
`opts` (optional) An object with these properties: | ||
- `errorHandler` - A function that will listen for error event on each worker | ||
- `onlineHandler` - A function that will listen for online event on each worker | ||
- `exitHandler` - A function that will listen for exit event on each worker | ||
- `errorHandler` (optional) - A function that will listen for error event on each worker | ||
- `onlineHandler` (optional) - A function that will listen for online event on each worker | ||
- `exitHandler` (optional) - A function that will listen for exit event on each worker | ||
- `workerChoiceStrategy` (optional) - The work choice strategy to use in this pool: | ||
- `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in this pool in a round robbin fashion | ||
- `WorkerChoiceStrategies.LESS_RECENTLY_USED`: Submit tasks to the less recently used worker in the pool | ||
Default: `WorkerChoiceStrategies.ROUND_ROBIN` | ||
- `enableEvents` (optional) - Events emission enablement in this pool. Default: true | ||
### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)` | ||
@@ -174,3 +182,3 @@ | ||
Execute method is available on both pool implementations (return type : Promise): | ||
Execute method is available on both pool implementations (return type: Promise): | ||
`data` (mandatory) An object that you want to pass to your worker implementation | ||
@@ -196,6 +204,6 @@ | ||
- `killBehavior` - Dictates if your async unit (worker/process) will be deleted in case that a task is active on it. | ||
**SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **won't** be deleted. | ||
**HARD**: If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted. | ||
**KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **won't** be deleted. | ||
**KillBehaviors.HARD**: If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted. | ||
This option only apply to the newly created workers. | ||
Default: `SOFT` | ||
Default: `KillBehaviors.SOFT` | ||
@@ -202,0 +210,0 @@ ## General guidance |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63831
20
884
267
31