Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

workerpool

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workerpool - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

examples/browser/statistics.html

31

dist/workerpool.js

@@ -7,4 +7,4 @@ /**

*
* @version 2.1.0
* @date 2016-10-11
* @version 2.2.0
* @date 2016-11-26
*

@@ -323,3 +323,3 @@ * @license

* worker to the task.
* @private
* @protected
*/

@@ -390,3 +390,3 @@ Pool.prototype._next = function () {

* @param {WorkerHandler} worker
* @private
* @protected
*/

@@ -423,4 +423,24 @@ Pool.prototype._removeWorker = function(worker) {

/**
* Retrieve statistics on tasks and workers.
* @return {{totalWorkers: number, busyWorkers: number, idleWorkers: number, pendingTasks: number, activeTasks: number}} Returns an object with statistics
*/
Pool.prototype.stats = function () {
var totalWorkers = this.workers.length;
var busyWorkers = this.workers.filter(function (worker) {
return worker.busy();
}).length;
return {
totalWorkers: totalWorkers,
busyWorkers: busyWorkers,
idleWorkers: totalWorkers - busyWorkers,
pendingTasks: this.tasks.length,
activeTasks: busyWorkers
};
};
/**
* Ensures that a minimum of minWorkers is up and running
* @private
* @protected
*/

@@ -949,3 +969,2 @@ Pool.prototype._ensureMinWorkers = function() {

resolver.promise
//.catch(Promise.CancellationError, function(error) { // TODO: not yet supported
.catch(function (error) {

@@ -952,0 +971,0 @@ if (error instanceof Promise.CancellationError || error instanceof Promise.TimeoutError) {

6

dist/workerpool.min.js

@@ -7,4 +7,4 @@ /**

*
* @version 2.1.0
* @date 2016-10-11
* @version 2.2.0
* @date 2016-11-26
*

@@ -26,3 +26,3 @@ * @license

*/
!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.workerpool=e():r.workerpool=e()}(this,function(){return function(r){function e(o){if(t[o])return t[o].exports;var n=t[o]={exports:{},id:o,loaded:!1};return r[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var t={};return e.m=r,e.c=t,e.p="",e(0)}([function(r,e,t){var o=t(1);e.pool=function(r,e){var o=t(3);return new o(r,e)},e.worker=function r(e){var r=t(8);r.add(e)},e.Promise=t(4),e.platform=o.platform,e.isMainThread=o.isMainThread,e.cpus=o.cpus},function(r,e,t){var o={require:t(2)};r.exports.platform="undefined"!=typeof Window||"undefined"!=typeof WorkerGlobalScope?"browser":"node",r.exports.isMainThread="browser"===r.exports.platform?"undefined"!=typeof Window:!process.connected,r.exports.cpus="browser"===r.exports.platform?self.navigator.hardwareConcurrency:o.require("os").cpus().length},function(r,e,t){function o(r){return t(n(r))}function n(r){return i[r]||function(){throw new Error("Cannot find module '"+r+"'.")}()}var i={"./Pool":3,"./Pool.js":3,"./Promise":4,"./Promise.js":4,"./WorkerHandler":5,"./WorkerHandler.js":5,"./environment":1,"./environment.js":1,"./generated/embeddedWorker":6,"./generated/embeddedWorker.js":6,"./header":7,"./header.js":7,"./worker":8,"./worker.js":8};o.keys=function(){return Object.keys(i)},o.resolve=n,r.exports=o,o.id=2},function(r,e,t){function o(r,e){if("string"==typeof r?this.script=r||null:(this.script=null,e=r),this.workers=[],this.tasks=[],e&&"maxWorkers"in e){if(!n(e.maxWorkers)||!i(e.maxWorkers)||e.maxWorkers<1)throw new TypeError("Option maxWorkers must be a positive integer number");this.maxWorkers=e.maxWorkers}else this.maxWorkers=Math.max((u.cpus||4)-1,1);if(e&&"minWorkers"in e){if("max"===e.minWorkers)this.minWorkers=Math.max((u.cpus||4)-1,1);else{if(!n(e.minWorkers)||!i(e.minWorkers)||e.minWorkers<0)throw new TypeError("Option minWorkers must be a positive integer number");this.minWorkers=e.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)}this._ensureMinWorkers()}}function n(r){return"number"==typeof r}function i(r){return Math.round(r)==r}var s=t(4),c=t(5),u=t(1);o.prototype.exec=function(r,e){if(e&&!Array.isArray(e))throw new TypeError('Array expected as argument "params"');if("string"==typeof r){var t=s.defer();return this.tasks.push({method:r,params:e,resolver:t}),this._next(),t.promise}if("function"==typeof r)return this.exec("run",[String(r),e]);throw new TypeError('Function or string expected as argument "method"')},o.prototype.proxy=function(){if(arguments.length>0)throw new Error("No arguments expected");var r=this;return this.exec("methods").then(function(e){var t={};return e.forEach(function(e){t[e]=function(){return r.exec(e,Array.prototype.slice.call(arguments))}}),t})},o.prototype._next=function(){if(this.tasks.length>0){var r=this._getWorker();if(r){var e=this,t=this.tasks.shift();t.resolver.promise.pending&&r.exec(t.method,t.params,t.resolver).then(function(){e._next()}).catch(function(){r.terminated&&(e._removeWorker(r),e._ensureMinWorkers()),e._next()})}}},o.prototype._getWorker=function(){for(var r=0,e=this.workers.length;r<e;r++){var t=this.workers[r];if(!t.busy())return t}return this.workers.length<this.maxWorkers?(t=new c(this.script),this.workers.push(t),t):null},o.prototype._removeWorker=function(r){r.terminate();var e=this.workers.indexOf(r);e!=-1&&this.workers.splice(e,1)},o.prototype.clear=function(r){this.workers.forEach(function(e){e.terminate(r)}),this.workers=[]},o.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var r=this.workers.length;r<this.minWorkers;r++)this.workers.push(new c(this.script))},r.exports=o},function(r,e){"use strict";function t(r,e){var s=this;if(!(this instanceof t))throw new SyntaxError("Constructor must be called with the new operator");if("function"!=typeof r)throw new SyntaxError("Function parameter handler(resolve, reject) missing");var c=[],u=[];this.resolved=!1,this.rejected=!1,this.pending=!0;var a=function(r,e){c.push(r),u.push(e)};this.then=function(r,e){return new t(function(t,n){var i=r?o(r,t,n):t,s=e?o(e,t,n):n;a(i,s)},s)};var f=function(r){return s.resolved=!0,s.rejected=!1,s.pending=!1,c.forEach(function(e){e(r)}),a=function(e,t){e(r)},f=p=function(){throw new Error("Promise is already resolved")},s},p=function(r){return s.resolved=!1,s.rejected=!0,s.pending=!1,u.forEach(function(e){e(r)}),a=function(e,t){t(r)},f=p=function(){throw new Error("Promise is already resolved")},s};this.cancel=function(){return e?e.cancel():p(new n),s},this.timeout=function(r){if(e)e.timeout(r);else{var t=setTimeout(function(){p(new i("Promise timed out after "+r+" ms"))},r);s.always(function(){clearTimeout(t)})}return s},r(function(r){f(r)},function(r){p(r)})}function o(r,e,t){return function(o){try{var n=r(o);n&&"function"==typeof n.then&&"function"==typeof n.catch?n.then(e,t):e(n)}catch(r){t(r)}}}function n(r){this.message=r||"promise cancelled",this.stack=(new Error).stack}function i(r){this.message=r||"timeout exceeded",this.stack=(new Error).stack}t.prototype.catch=function(r){return this.then(null,r)},t.prototype.always=function(r){return this.then(r,r)},t.all=function(r){return new t(function(e,t){var o=r.length,n=[];o?r.forEach(function(r,i){r.then(function(r){n[i]=r,o--,0==o&&e(n)},function(r){o=0,t(r)})}):e(n)})},t.defer=function(){var r={};return r.promise=new t(function(e,t){r.resolve=e,r.reject=t}),r},n.prototype=new Error,n.prototype.constructor=Error,n.prototype.name="CancellationError",t.CancellationError=n,i.prototype=new Error,i.prototype.constructor=Error,i.prototype.name="TimeoutError",t.TimeoutError=i,r.exports=t},function(r,e,t){function o(){if("browser"==c.platform){if("undefined"==typeof Blob)throw new Error("Blob not supported by the browser");if(!window.URL||"function"!=typeof window.URL.createObjectURL)throw new Error("URL.createObjectURL not supported by the browser");var r=new Blob([t(6)],{type:"text/javascript"});return window.URL.createObjectURL(r)}return __dirname+"/worker.js"}function n(r){for(var e=new Error(""),t=Object.keys(r),o=0;o<t.length;o++)e[t[o]]=r[t[o]];return e}function i(r){function e(r){i.terminated=!0;for(var e in i.processing)i.processing.hasOwnProperty(e)&&i.processing[e].resolver.reject(r);i.processing={}}function t(){i.requestQueue.forEach(i.worker.send.bind(i.worker)),i.requestQueue=[]}if(this.script=r||o(),"browser"==c.platform){if("function"!=typeof Worker&&("object"!=typeof Worker||"function"!=typeof Worker.prototype.constructor))throw new Error("Web workers not supported by the browser");this.worker=new Worker(this.script),this.worker.on=function(r,e){this.addEventListener(r,function(r){e(r.data)})},this.worker.send=function(r){this.postMessage(r)}}else this.worker=u.require("child_process").fork(this.script);var i=this;r||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",function(r){if("string"==typeof r&&"ready"===r)i.worker.ready=!0,t();else{var e=r.id,o=i.processing[e];o&&(delete i.processing[e],i.terminating&&i.terminate(),r.error?o.resolver.reject(n(r.error)):o.resolver.resolve(r.result))}}),this.worker.on("error",e),this.worker.on("exit",function(){var r=new Error("Worker terminated unexpectedly");e(r)}),this.processing={},this.terminating=!1,this.terminated=!1,this.lastId=0}var s=t(4),c=t(1),u={require:t(2)};i.prototype.methods=function(){return this.exec("methods")},i.prototype.exec=function(r,e,t){t||(t=s.defer());var o=++this.lastId;this.processing[o]={id:o,resolver:t};var n={id:o,method:r,params:e};this.terminated?t.reject(new Error("Worker is terminated")):this.worker.ready?this.worker.send(n):this.requestQueue.push(n);var i=this;return t.promise.catch(function(r){(r instanceof s.CancellationError||r instanceof s.TimeoutError)&&(delete i.processing[o],i.terminate(!0))}),t.promise},i.prototype.busy=function(){return Object.keys(this.processing).length>0},i.prototype.terminate=function(r){if(r){for(var e in this.processing)this.processing.hasOwnProperty(e)&&this.processing[e].resolver.reject(new Error("Worker terminated"));this.processing={}}if(this.busy())this.terminating=!0;else{if(this.worker){if("function"==typeof this.worker.kill)this.worker.kill();else{if("function"!=typeof this.worker.terminate)throw new Error("Failed to terminate worker");this.worker.terminate()}this.worker=null}this.terminating=!1,this.terminated=!0}},r.exports=i},function(r,e){r.exports='!function(r){function e(n){if(o[n])return o[n].exports;var t=o[n]={exports:{},id:n,loaded:!1};return r[n].call(t.exports,t,t.exports,e),t.loaded=!0,t.exports}var o={};return e.m=r,e.c=o,e.p="",e(0)}([function(module,exports,__webpack_require__){function convertError(r){return Object.getOwnPropertyNames(r).reduce(function(e,o){return Object.defineProperty(e,o,{value:r[o],enumerable:!0})},{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}var worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,function(r){e(r.data)})},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");worker.on=process.on.bind(process),worker.send=process.send.bind(process)}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error(\'Unknown method "\'+r.method+\'"\');var o=e.apply(e,r.params);isPromise(o)?o.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:o,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},exports.add=worker.register}]);'},function(r,e){},function(module,exports,__webpack_require__){function convertError(r){return Object.getOwnPropertyNames(r).reduce(function(e,t){return Object.defineProperty(e,t,{value:r[t],enumerable:!0})},{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}var worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,function(r){e(r.data)})},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");worker.on=process.on.bind(process),worker.send=process.send.bind(process)}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error('Unknown method "'+r.method+'"');var t=e.apply(e,r.params);isPromise(t)?t.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:t,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},exports.add=worker.register}])});
!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.workerpool=e():r.workerpool=e()}(this,function(){return function(r){function e(o){if(t[o])return t[o].exports;var n=t[o]={exports:{},id:o,loaded:!1};return r[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var t={};return e.m=r,e.c=t,e.p="",e(0)}([function(r,e,t){var o=t(1);e.pool=function(r,e){var o=t(3);return new o(r,e)},e.worker=function r(e){var r=t(8);r.add(e)},e.Promise=t(4),e.platform=o.platform,e.isMainThread=o.isMainThread,e.cpus=o.cpus},function(r,e,t){var o={require:t(2)};r.exports.platform="undefined"!=typeof Window||"undefined"!=typeof WorkerGlobalScope?"browser":"node",r.exports.isMainThread="browser"===r.exports.platform?"undefined"!=typeof Window:!process.connected,r.exports.cpus="browser"===r.exports.platform?self.navigator.hardwareConcurrency:o.require("os").cpus().length},function(r,e,t){function o(r){return t(n(r))}function n(r){return s[r]||function(){throw new Error("Cannot find module '"+r+"'.")}()}var s={"./Pool":3,"./Pool.js":3,"./Promise":4,"./Promise.js":4,"./WorkerHandler":5,"./WorkerHandler.js":5,"./environment":1,"./environment.js":1,"./generated/embeddedWorker":6,"./generated/embeddedWorker.js":6,"./header":7,"./header.js":7,"./worker":8,"./worker.js":8};o.keys=function(){return Object.keys(s)},o.resolve=n,r.exports=o,o.id=2},function(r,e,t){function o(r,e){if("string"==typeof r?this.script=r||null:(this.script=null,e=r),this.workers=[],this.tasks=[],e&&"maxWorkers"in e){if(!n(e.maxWorkers)||!s(e.maxWorkers)||e.maxWorkers<1)throw new TypeError("Option maxWorkers must be a positive integer number");this.maxWorkers=e.maxWorkers}else this.maxWorkers=Math.max((c.cpus||4)-1,1);if(e&&"minWorkers"in e){if("max"===e.minWorkers)this.minWorkers=Math.max((c.cpus||4)-1,1);else{if(!n(e.minWorkers)||!s(e.minWorkers)||e.minWorkers<0)throw new TypeError("Option minWorkers must be a positive integer number");this.minWorkers=e.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)}this._ensureMinWorkers()}}function n(r){return"number"==typeof r}function s(r){return Math.round(r)==r}var i=t(4),u=t(5),c=t(1);o.prototype.exec=function(r,e){if(e&&!Array.isArray(e))throw new TypeError('Array expected as argument "params"');if("string"==typeof r){var t=i.defer();return this.tasks.push({method:r,params:e,resolver:t}),this._next(),t.promise}if("function"==typeof r)return this.exec("run",[String(r),e]);throw new TypeError('Function or string expected as argument "method"')},o.prototype.proxy=function(){if(arguments.length>0)throw new Error("No arguments expected");var r=this;return this.exec("methods").then(function(e){var t={};return e.forEach(function(e){t[e]=function(){return r.exec(e,Array.prototype.slice.call(arguments))}}),t})},o.prototype._next=function(){if(this.tasks.length>0){var r=this._getWorker();if(r){var e=this,t=this.tasks.shift();t.resolver.promise.pending&&r.exec(t.method,t.params,t.resolver).then(function(){e._next()}).catch(function(){r.terminated&&(e._removeWorker(r),e._ensureMinWorkers()),e._next()})}}},o.prototype._getWorker=function(){for(var r=0,e=this.workers.length;r<e;r++){var t=this.workers[r];if(!t.busy())return t}return this.workers.length<this.maxWorkers?(t=new u(this.script),this.workers.push(t),t):null},o.prototype._removeWorker=function(r){r.terminate();var e=this.workers.indexOf(r);e!=-1&&this.workers.splice(e,1)},o.prototype.clear=function(r){this.workers.forEach(function(e){e.terminate(r)}),this.workers=[]},o.prototype.stats=function(){var r=this.workers.length,e=this.workers.filter(function(r){return r.busy()}).length;return{totalWorkers:r,busyWorkers:e,idleWorkers:r-e,pendingTasks:this.tasks.length,activeTasks:e}},o.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var r=this.workers.length;r<this.minWorkers;r++)this.workers.push(new u(this.script))},r.exports=o},function(r,e){"use strict";function t(r,e){var i=this;if(!(this instanceof t))throw new SyntaxError("Constructor must be called with the new operator");if("function"!=typeof r)throw new SyntaxError("Function parameter handler(resolve, reject) missing");var u=[],c=[];this.resolved=!1,this.rejected=!1,this.pending=!0;var a=function(r,e){u.push(r),c.push(e)};this.then=function(r,e){return new t(function(t,n){var s=r?o(r,t,n):t,i=e?o(e,t,n):n;a(s,i)},i)};var f=function(r){return i.resolved=!0,i.rejected=!1,i.pending=!1,u.forEach(function(e){e(r)}),a=function(e,t){e(r)},f=p=function(){throw new Error("Promise is already resolved")},i},p=function(r){return i.resolved=!1,i.rejected=!0,i.pending=!1,c.forEach(function(e){e(r)}),a=function(e,t){t(r)},f=p=function(){throw new Error("Promise is already resolved")},i};this.cancel=function(){return e?e.cancel():p(new n),i},this.timeout=function(r){if(e)e.timeout(r);else{var t=setTimeout(function(){p(new s("Promise timed out after "+r+" ms"))},r);i.always(function(){clearTimeout(t)})}return i},r(function(r){f(r)},function(r){p(r)})}function o(r,e,t){return function(o){try{var n=r(o);n&&"function"==typeof n.then&&"function"==typeof n.catch?n.then(e,t):e(n)}catch(r){t(r)}}}function n(r){this.message=r||"promise cancelled",this.stack=(new Error).stack}function s(r){this.message=r||"timeout exceeded",this.stack=(new Error).stack}t.prototype.catch=function(r){return this.then(null,r)},t.prototype.always=function(r){return this.then(r,r)},t.all=function(r){return new t(function(e,t){var o=r.length,n=[];o?r.forEach(function(r,s){r.then(function(r){n[s]=r,o--,0==o&&e(n)},function(r){o=0,t(r)})}):e(n)})},t.defer=function(){var r={};return r.promise=new t(function(e,t){r.resolve=e,r.reject=t}),r},n.prototype=new Error,n.prototype.constructor=Error,n.prototype.name="CancellationError",t.CancellationError=n,s.prototype=new Error,s.prototype.constructor=Error,s.prototype.name="TimeoutError",t.TimeoutError=s,r.exports=t},function(r,e,t){function o(){if("browser"==u.platform){if("undefined"==typeof Blob)throw new Error("Blob not supported by the browser");if(!window.URL||"function"!=typeof window.URL.createObjectURL)throw new Error("URL.createObjectURL not supported by the browser");var r=new Blob([t(6)],{type:"text/javascript"});return window.URL.createObjectURL(r)}return __dirname+"/worker.js"}function n(r){for(var e=new Error(""),t=Object.keys(r),o=0;o<t.length;o++)e[t[o]]=r[t[o]];return e}function s(r){function e(r){s.terminated=!0;for(var e in s.processing)s.processing.hasOwnProperty(e)&&s.processing[e].resolver.reject(r);s.processing={}}function t(){s.requestQueue.forEach(s.worker.send.bind(s.worker)),s.requestQueue=[]}if(this.script=r||o(),"browser"==u.platform){if("function"!=typeof Worker&&("object"!=typeof Worker||"function"!=typeof Worker.prototype.constructor))throw new Error("Web workers not supported by the browser");this.worker=new Worker(this.script),this.worker.on=function(r,e){this.addEventListener(r,function(r){e(r.data)})},this.worker.send=function(r){this.postMessage(r)}}else this.worker=c.require("child_process").fork(this.script);var s=this;r||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",function(r){if("string"==typeof r&&"ready"===r)s.worker.ready=!0,t();else{var e=r.id,o=s.processing[e];o&&(delete s.processing[e],s.terminating&&s.terminate(),r.error?o.resolver.reject(n(r.error)):o.resolver.resolve(r.result))}}),this.worker.on("error",e),this.worker.on("exit",function(){var r=new Error("Worker terminated unexpectedly");e(r)}),this.processing={},this.terminating=!1,this.terminated=!1,this.lastId=0}var i=t(4),u=t(1),c={require:t(2)};s.prototype.methods=function(){return this.exec("methods")},s.prototype.exec=function(r,e,t){t||(t=i.defer());var o=++this.lastId;this.processing[o]={id:o,resolver:t};var n={id:o,method:r,params:e};this.terminated?t.reject(new Error("Worker is terminated")):this.worker.ready?this.worker.send(n):this.requestQueue.push(n);var s=this;return t.promise.catch(function(r){(r instanceof i.CancellationError||r instanceof i.TimeoutError)&&(delete s.processing[o],s.terminate(!0))}),t.promise},s.prototype.busy=function(){return Object.keys(this.processing).length>0},s.prototype.terminate=function(r){if(r){for(var e in this.processing)this.processing.hasOwnProperty(e)&&this.processing[e].resolver.reject(new Error("Worker terminated"));this.processing={}}if(this.busy())this.terminating=!0;else{if(this.worker){if("function"==typeof this.worker.kill)this.worker.kill();else{if("function"!=typeof this.worker.terminate)throw new Error("Failed to terminate worker");this.worker.terminate()}this.worker=null}this.terminating=!1,this.terminated=!0}},r.exports=s},function(r,e){r.exports='!function(r){function e(n){if(o[n])return o[n].exports;var t=o[n]={exports:{},id:n,loaded:!1};return r[n].call(t.exports,t,t.exports,e),t.loaded=!0,t.exports}var o={};return e.m=r,e.c=o,e.p="",e(0)}([function(module,exports,__webpack_require__){function convertError(r){return Object.getOwnPropertyNames(r).reduce(function(e,o){return Object.defineProperty(e,o,{value:r[o],enumerable:!0})},{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}var worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,function(r){e(r.data)})},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");worker.on=process.on.bind(process),worker.send=process.send.bind(process)}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error(\'Unknown method "\'+r.method+\'"\');var o=e.apply(e,r.params);isPromise(o)?o.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:o,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},exports.add=worker.register}]);'},function(r,e){},function(module,exports,__webpack_require__){function convertError(r){return Object.getOwnPropertyNames(r).reduce(function(e,t){return Object.defineProperty(e,t,{value:r[t],enumerable:!0})},{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}var worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,function(r){e(r.data)})},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");worker.on=process.on.bind(process),worker.send=process.send.bind(process)}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error('Unknown method "'+r.method+'"');var t=e.apply(e,r.params);isPromise(t)?t.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:t,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},exports.add=worker.register}])});
//# sourceMappingURL=workerpool.map

@@ -5,2 +5,7 @@ # workerpool history

## 2016-11-26, version 2.2.0
- Implemented #18: method `pool.stats()`.
## 2016-10-11, version 2.1.0

@@ -7,0 +12,0 @@

@@ -153,3 +153,3 @@ var Promise = require('./Promise');

* worker to the task.
* @private
* @protected
*/

@@ -220,3 +220,3 @@ Pool.prototype._next = function () {

* @param {WorkerHandler} worker
* @private
* @protected
*/

@@ -253,4 +253,24 @@ Pool.prototype._removeWorker = function(worker) {

/**
* Retrieve statistics on tasks and workers.
* @return {{totalWorkers: number, busyWorkers: number, idleWorkers: number, pendingTasks: number, activeTasks: number}} Returns an object with statistics
*/
Pool.prototype.stats = function () {
var totalWorkers = this.workers.length;
var busyWorkers = this.workers.filter(function (worker) {
return worker.busy();
}).length;
return {
totalWorkers: totalWorkers,
busyWorkers: busyWorkers,
idleWorkers: totalWorkers - busyWorkers,
pendingTasks: this.tasks.length,
activeTasks: busyWorkers
};
};
/**
* Ensures that a minimum of minWorkers is up and running
* @private
* @protected
*/

@@ -257,0 +277,0 @@ Pool.prototype._ensureMinWorkers = function() {

@@ -201,3 +201,2 @@ var Promise = require('./Promise');

resolver.promise
//.catch(Promise.CancellationError, function(error) { // TODO: not yet supported
.catch(function (error) {

@@ -204,0 +203,0 @@ if (error instanceof Promise.CancellationError || error instanceof Promise.TimeoutError) {

{
"name": "workerpool",
"version": "2.1.0",
"version": "2.2.0",
"description": "Offload tasks to a pool of workers on node.js and in the browser",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/josdejong/workerpool",

@@ -183,4 +183,21 @@ # workerpool

- When `method` is a function, the provided function `fn` will be stringified, send to the worker, and executed there with the provided parameters. The provided function must be static, it must not depend on variables in a surrounding scope.
- `Pool.proxy() : Promise.<Object, Error>`<br>
Create a proxy for the worker pool. The proxy contains a proxy for all methods available on the worker. All methods return promises resolving the methods result.
- `Pool.stats() : Object`<br>
Retrieve statistics on workers, and active and pending tasks.
Returns an object containing the following properties:
```json
{
totalWorkers: 0,
busyWorkers: 0,
idleWorkers: 0,
pendingTasks: 0,
activeTasks: 0
}
```
- `Pool.clear([force: boolean])`<br>

@@ -187,0 +204,0 @@ Clear all workers from the pool. If parameter `force` is false (default), workers will finish the tasks they are working on before terminating themselves. When `force` is true, all workers are terminated immediately without finishing running tasks.

@@ -471,2 +471,52 @@ var assert = require('assert'),

it('should return statistics', function () {
var pool = new Pool({maxWorkers: 4});
function test() {
return new Promise(function (resolve, reject) {
setTimeout(resolve, 100);
});
}
function testError() {
return new Promise(function (resolve, reject) {
throw new Error('Test error')
});
}
assert.deepEqual(pool.stats(), {totalWorkers: 0, busyWorkers: 0, idleWorkers: 0, pendingTasks: 0, activeTasks: 0});
var promise = pool.exec(test)
.then(function () {
assert.deepEqual(pool.stats(), {totalWorkers: 1, busyWorkers: 0, idleWorkers: 1, pendingTasks: 0, activeTasks: 0 });
// start six tasks (max workers is 4, so we should get pending tasks)
var all = Promise.all([
pool.exec(test),
pool.exec(test),
pool.exec(test),
pool.exec(test),
pool.exec(test),
pool.exec(test)
]);
assert.deepEqual(pool.stats(), {totalWorkers: 4, busyWorkers: 4, idleWorkers: 0, pendingTasks: 2, activeTasks: 4});
return all;
})
.then(function () {
assert.deepEqual(pool.stats(), {totalWorkers: 4, busyWorkers: 0, idleWorkers: 4, pendingTasks: 0, activeTasks: 0 });
return pool.exec(testError)
})
.catch(function () {
assert.deepEqual(pool.stats(), {totalWorkers: 4, busyWorkers: 0, idleWorkers: 4, pendingTasks: 0, activeTasks: 0});
});
assert.deepEqual(pool.stats(), {totalWorkers: 1, busyWorkers: 1, idleWorkers: 0, pendingTasks: 0, activeTasks: 1});
return promise;
});
it('should throw an error in case of wrong type of arguments in function exec', function () {

@@ -473,0 +523,0 @@ var pool = new Pool();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc