Comparing version 1.0.7 to 1.0.8
116
index.js
@@ -6,4 +6,4 @@ const uuid = require('node-uuid') | ||
const PROCESS_REQUIRED = 'required paramenter [process] must be a function' | ||
const SOURCE_REQUIRED = 'Source is required to be either a function or an array' | ||
const TYPE_PROCEED_ON_ERROR = 'parameter proceedOnError must be a boolean' | ||
const SOURCE_REQUIRED = 'Source is required to be a function, promise or array' | ||
const TYPE_PROCEED_ON_ERROR = 'parameter stopOnError must be a boolean' | ||
const TYPE_EVENT_HANDLER = 'Event handlers must be functions' | ||
@@ -15,4 +15,10 @@ | ||
if (!config.process || typeof config.process !== 'function') throw new Error(PROCESS_REQUIRED) | ||
if (!config.source || (typeof config.source !== 'function' && !Array.isArray(config.source))) throw new Error(SOURCE_REQUIRED) | ||
if (config.proceedOnError && !typeof config.proceedOnError === 'boolean') throw new Error(TYPE_PROCEED_ON_ERROR) | ||
if ( | ||
!config.source || ( | ||
typeof config.source !== 'function' && | ||
!Array.isArray(config.source) && | ||
!config.source.then | ||
) | ||
) throw new Error(SOURCE_REQUIRED) | ||
if (config.stopOnError && typeof config.stopOnError !== 'boolean') throw new Error(TYPE_PROCEED_ON_ERROR) | ||
this.events = {} | ||
@@ -22,5 +28,9 @@ this.debug = config.debug | ||
this.process = config.process | ||
this.source = config.source | ||
this.proceedOnError = config.proceedOnError || false | ||
this.sourceType = Array.isArray(this.source) ? 'array' : 'function' | ||
this.stopOnError = config.stopOnError || false | ||
this.sourceType = Array.isArray(config.source) ? 'array' : config.source.then ? 'promise' : 'function' | ||
if (this.sourceType === 'array') { | ||
this.source = config.source.slice(0) | ||
} else { | ||
this.source = config.source | ||
} | ||
this.running = {} | ||
@@ -35,5 +45,7 @@ this.jobsFinished = 0 | ||
this.events[event] = handler | ||
return this | ||
} | ||
emit(event, payload) { | ||
if (event === 'error' && this.stopOnError) this.status = 'error' | ||
if (this.debug && console) console.log(`[${new Date()}][${event}]`, payload) | ||
@@ -45,11 +57,36 @@ if (this.events[event]) this.events[event](payload) | ||
this.status = 'running' | ||
this.startTime = new Date() | ||
this.emit('start', { | ||
startTime: this.startTime, | ||
maxProceses: this.maxProceses, | ||
proceedOnError: this.proceedOnError, | ||
sourceType: this.sourceType, | ||
status: this.status | ||
}) | ||
this.fillJobs() | ||
this.startTime = this.startTime || new Date() | ||
if (this.sourceType === 'promise') { | ||
const self = this | ||
this.source.then((data) => { | ||
this.sourceType = Array.isArray(data) ? 'array' : data.then ? 'promise' : 'function' | ||
if (this.sourceType === 'array') { | ||
this.source = data.slice(0) | ||
} else { | ||
this.source = data | ||
} | ||
this.start() | ||
}).catch((err) => { | ||
this.emit('error', err) | ||
this.status = 'error' | ||
this.emit('processFinish', { | ||
startTime: this.startTime, | ||
endTime: new Date(), | ||
processed: this.jobsFinished, | ||
errors: this.jobErrors, | ||
status: this.status | ||
}) | ||
}) | ||
} else { | ||
this.emit('start', { | ||
startTime: this.startTime, | ||
maxProceses: this.maxProceses, | ||
stopOnError: this.stopOnError, | ||
sourceType: this.sourceType, | ||
status: this.status, | ||
type: this.sourceType | ||
}) | ||
this.fillJobs() | ||
} | ||
return this | ||
} | ||
@@ -70,3 +107,3 @@ | ||
let runningCount = Object.keys(this.running).length | ||
if (!runningCount) { | ||
if ((!runningCount && this.status === 'empty') || this.status === 'error') { | ||
this.status = 'finished' | ||
@@ -86,11 +123,13 @@ return this.emit('processFinish', { | ||
jobPromise.then((result) => { | ||
let jobEndTime = new Date() | ||
this.emit('jobFinnish', { | ||
jobId, | ||
jobStartTime, | ||
jobEndTime, | ||
result, | ||
jobsRunning: Object.keys(this.running).length | ||
}) | ||
this.jobsFinished ++ | ||
if (result) { | ||
let jobEndTime = new Date() | ||
this.emit('jobFinish', { | ||
jobId, | ||
jobStartTime, | ||
jobEndTime, | ||
result, | ||
jobsRunning: Object.keys(this.running).length | ||
}) | ||
this.jobsFinished ++ | ||
} | ||
next() | ||
@@ -104,3 +143,3 @@ }) | ||
} | ||
fillJobs () { | ||
@@ -127,3 +166,3 @@ if (this.fillingJobs) return | ||
while ( | ||
Object.keys(this.running).length < this.maxProceses | ||
Object.keys(this.running).length < this.maxProceses | ||
&& this.status === 'running' | ||
@@ -139,2 +178,3 @@ && ((this.sourceType === 'array' && this.source.length) || (this.sourceType !== 'array')) | ||
item = this.source.pop() | ||
if (!this.source.length) this.status = 'empty' | ||
} else { | ||
@@ -146,10 +186,14 @@ item = this.source((err, jobValue) => { | ||
} | ||
if (item && item.then && typeof item.then === 'function') { | ||
item.then((jobValue) => { | ||
resolveJobValue(jobValue, resolve, reject) | ||
}).catch(reject) | ||
} else { | ||
if (item) { | ||
resolveJobValue(item, resolve, reject) | ||
if (undefined !== item) { | ||
if (item && item.then && typeof item.then === 'function') { | ||
item.then((jobValue) => { | ||
resolveJobValue(jobValue, resolve, reject) | ||
}).catch(reject) | ||
} else { | ||
if (item) { | ||
resolveJobValue(item, resolve, reject) | ||
} else { | ||
this.status = 'empty' | ||
resolve() | ||
} | ||
} | ||
@@ -156,0 +200,0 @@ } |
150
lib/index.js
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -14,4 +12,4 @@ | ||
var PROCESS_REQUIRED = 'required paramenter [process] must be a function'; | ||
var SOURCE_REQUIRED = 'Source is required to be either a function or an array'; | ||
var TYPE_PROCEED_ON_ERROR = 'parameter proceedOnError must be a boolean'; | ||
var SOURCE_REQUIRED = 'Source is required to be a function, promise or array'; | ||
var TYPE_PROCEED_ON_ERROR = 'parameter stopOnError must be a boolean'; | ||
var TYPE_EVENT_HANDLER = 'Event handlers must be functions'; | ||
@@ -25,4 +23,4 @@ | ||
if (!config.process || typeof config.process !== 'function') throw new Error(PROCESS_REQUIRED); | ||
if (!config.source || typeof config.source !== 'function' && !Array.isArray(config.source)) throw new Error(SOURCE_REQUIRED); | ||
if (config.proceedOnError && !_typeof(config.proceedOnError) === 'boolean') throw new Error(TYPE_PROCEED_ON_ERROR); | ||
if (!config.source || typeof config.source !== 'function' && !Array.isArray(config.source) && !config.source.then) throw new Error(SOURCE_REQUIRED); | ||
if (config.stopOnError && typeof config.stopOnError !== 'boolean') throw new Error(TYPE_PROCEED_ON_ERROR); | ||
this.events = {}; | ||
@@ -32,5 +30,9 @@ this.debug = config.debug; | ||
this.process = config.process; | ||
this.source = config.source; | ||
this.proceedOnError = config.proceedOnError || false; | ||
this.sourceType = Array.isArray(this.source) ? 'array' : 'function'; | ||
this.stopOnError = config.stopOnError || false; | ||
this.sourceType = Array.isArray(config.source) ? 'array' : config.source.then ? 'promise' : 'function'; | ||
if (this.sourceType === 'array') { | ||
this.source = config.source.slice(0); | ||
} else { | ||
this.source = config.source; | ||
} | ||
this.running = {}; | ||
@@ -47,2 +49,3 @@ this.jobsFinished = 0; | ||
this.events[event] = handler; | ||
return this; | ||
} | ||
@@ -52,2 +55,3 @@ }, { | ||
value: function emit(event, payload) { | ||
if (event === 'error' && this.stopOnError) this.status = 'error'; | ||
if (this.debug && console) console.log('[' + new Date() + '][' + event + ']', payload); | ||
@@ -59,12 +63,39 @@ if (this.events[event]) this.events[event](payload); | ||
value: function start() { | ||
var _this = this; | ||
this.status = 'running'; | ||
this.startTime = new Date(); | ||
this.emit('start', { | ||
startTime: this.startTime, | ||
maxProceses: this.maxProceses, | ||
proceedOnError: this.proceedOnError, | ||
sourceType: this.sourceType, | ||
status: this.status | ||
}); | ||
this.fillJobs(); | ||
this.startTime = this.startTime || new Date(); | ||
if (this.sourceType === 'promise') { | ||
var self = this; | ||
this.source.then(function (data) { | ||
_this.sourceType = Array.isArray(data) ? 'array' : data.then ? 'promise' : 'function'; | ||
if (_this.sourceType === 'array') { | ||
_this.source = data.slice(0); | ||
} else { | ||
_this.source = data; | ||
} | ||
_this.start(); | ||
}).catch(function (err) { | ||
_this.emit('error', err); | ||
_this.status = 'error'; | ||
_this.emit('processFinish', { | ||
startTime: _this.startTime, | ||
endTime: new Date(), | ||
processed: _this.jobsFinished, | ||
errors: _this.jobErrors, | ||
status: _this.status | ||
}); | ||
}); | ||
} else { | ||
this.emit('start', { | ||
startTime: this.startTime, | ||
maxProceses: this.maxProceses, | ||
stopOnError: this.stopOnError, | ||
sourceType: this.sourceType, | ||
status: this.status, | ||
type: this.sourceType | ||
}); | ||
this.fillJobs(); | ||
} | ||
return this; | ||
} | ||
@@ -79,3 +110,3 @@ }, { | ||
value: function runJob(jobPromise) { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -86,17 +117,17 @@ var jobId = uuid.v4(); | ||
var next = function next() { | ||
var self = _this; | ||
var jobToDelete = _this.running[jobId]; | ||
delete _this.running[jobId]; | ||
var runningCount = Object.keys(_this.running).length; | ||
if (!runningCount) { | ||
_this.status = 'finished'; | ||
return _this.emit('processFinish', { | ||
startTime: _this.startTime, | ||
var self = _this2; | ||
var jobToDelete = _this2.running[jobId]; | ||
delete _this2.running[jobId]; | ||
var runningCount = Object.keys(_this2.running).length; | ||
if (!runningCount && _this2.status === 'empty' || _this2.status === 'error') { | ||
_this2.status = 'finished'; | ||
return _this2.emit('processFinish', { | ||
startTime: _this2.startTime, | ||
endTime: new Date(), | ||
processed: _this.jobsFinished, | ||
errors: _this.jobErrors, | ||
status: _this.status | ||
processed: _this2.jobsFinished, | ||
errors: _this2.jobErrors, | ||
status: _this2.status | ||
}); | ||
} | ||
_this.fillJobs(); | ||
_this2.fillJobs(); | ||
}; | ||
@@ -106,15 +137,17 @@ | ||
jobPromise.then(function (result) { | ||
var jobEndTime = new Date(); | ||
_this.emit('jobFinnish', { | ||
jobId: jobId, | ||
jobStartTime: jobStartTime, | ||
jobEndTime: jobEndTime, | ||
result: result, | ||
jobsRunning: Object.keys(_this.running).length | ||
}); | ||
_this.jobsFinished++; | ||
if (result) { | ||
var jobEndTime = new Date(); | ||
_this2.emit('jobFinish', { | ||
jobId: jobId, | ||
jobStartTime: jobStartTime, | ||
jobEndTime: jobEndTime, | ||
result: result, | ||
jobsRunning: Object.keys(_this2.running).length | ||
}); | ||
_this2.jobsFinished++; | ||
} | ||
next(); | ||
}).catch(function (e) { | ||
_this.emit('error', e); | ||
_this.jobErrors++; | ||
_this2.emit('error', e); | ||
_this2.jobErrors++; | ||
next(); | ||
@@ -126,3 +159,3 @@ }); | ||
value: function fillJobs() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -134,3 +167,3 @@ if (this.fillingJobs) return; | ||
if (jobValue) { | ||
var jobPromise = _this2.process(jobValue, function (err, value) { | ||
var jobPromise = _this3.process(jobValue, function (err, value) { | ||
if (err) return reject(err); | ||
@@ -145,3 +178,3 @@ resolve(value); | ||
resolve(); | ||
_this2.status = 'empty'; | ||
_this3.status = 'empty'; | ||
} | ||
@@ -156,6 +189,7 @@ }; | ||
var item = void 0; | ||
if (_this2.sourceType === 'array') { | ||
item = _this2.source.pop(); | ||
if (_this3.sourceType === 'array') { | ||
item = _this3.source.pop(); | ||
if (!_this3.source.length) _this3.status = 'empty'; | ||
} else { | ||
item = _this2.source(function (err, jobValue) { | ||
item = _this3.source(function (err, jobValue) { | ||
if (err) return reject(err); | ||
@@ -165,10 +199,14 @@ resolveJobValue(jobValue, resolve, reject); | ||
} | ||
if (item && item.then && typeof item.then === 'function') { | ||
item.then(function (jobValue) { | ||
resolveJobValue(jobValue, resolve, reject); | ||
}).catch(reject); | ||
} else { | ||
if (item) { | ||
resolveJobValue(item, resolve, reject); | ||
if (undefined !== item) { | ||
if (item && item.then && typeof item.then === 'function') { | ||
item.then(function (jobValue) { | ||
resolveJobValue(jobValue, resolve, reject); | ||
}).catch(reject); | ||
} else { | ||
if (item) { | ||
resolveJobValue(item, resolve, reject); | ||
} else { | ||
_this3.status = 'empty'; | ||
resolve(); | ||
} | ||
} | ||
@@ -175,0 +213,0 @@ } |
{ | ||
"name": "jobq", | ||
"version": "1.0.7", | ||
"description": "job queuer", | ||
"version": "1.0.8", | ||
"description": "Async and parallel execution of jobs, tasks and processes with a queue manager", | ||
"main": "./lib/index.js", | ||
"engine": "node >= 0.10.x", | ||
"scripts": { | ||
"test": "mocha", | ||
"test": "npm run build && istanbul cover _mocha ./tests", | ||
"build": "babel index.js --out-dir lib", | ||
@@ -13,3 +14,7 @@ "prepublish": "npm run build" | ||
"job", | ||
"queuer" | ||
"task", | ||
"queue", | ||
"process", | ||
"async", | ||
"paralel" | ||
], | ||
@@ -20,2 +25,5 @@ "repository": { | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/adleroliveira/jobQ/issues" | ||
}, | ||
"author": "Adler Oliveira", | ||
@@ -29,4 +37,7 @@ "license": "ISC", | ||
"babel-cli": "^6.18.0", | ||
"babel-preset-es2015": "^6.18.0" | ||
"babel-preset-es2015": "^6.18.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.1.2", | ||
"unit.js": "^2.0.0" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/adleroliveira/jobQ.svg?branch=master)](https://travis-ci.org/adleroliveira/jobQ) | ||
# jobQ | ||
@@ -2,0 +4,0 @@ Async and parallel execution of jobs, tasks and processes |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
39899
12
1106
1
61
6