Async and parallel execution of jobs, tasks and processes with a queue manager
npm install jobq
bash
npm install --save jobq
`Usage
`js
var jobQ = require('jobq')var queue = new jobQ({
process: function(x, callback){
setTimeout(function() {
callback(null, x)
}, 3000)
},
source: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
maxProceses: 2
})
queue.start()
`Options
* source: required View source section.
* process: required View process section.
* maxProceses: `` indicates how many jobs will run in parallel. A value of `0` means 'no limit'. Default `1`
* debug: `` enables or disables debug. Default `false`
* stopOnError: `` indicates if jobs will stop after first error or continue. If enabled, `processFinish` will be called with status `error` if an error occurs. Default `false`
* pooling: `` pooling timeout in milliseconds. When enabled, JobQ will continue to try and fetch data from the source (`Function` only). Default: No poolingEvents
`js
var queue = new jobQ({
process: myProcess,
source: mySource,
maxProceses: 2
})queue.on('start', function(){})
queue.on('jobFetch', function(){})
queue.on('jobRun', function(){})
queue.on('jobFinish', function(){})
queue.on('processFinish', function(){})
queue.on('pooling', function(){})
queue.on('pause', function(){})
queue.on('resume', function(){})
queue.on('error', function(){})
queue.start()
`#### start
Emited once after calling
`start()` with an object containing:
* startTime: `` date when start was called
* processed: `` jobs processed so far
* errors: `` jobs errored so far
* maxProceses: `` maxProceses passed to constructor. Default `1`
* stopOnError: `` stopOnError passed to constructor. Default `false`
* sourceType: `` Detected source type (`array`, `function`, `promise` or `stream`).
* status: `` queue status. will always be running at this point.#### jobFetch
Emited once for each job before fetching it from the queue with an object containing:
* jobsRunning:
`` current amount of processing jobs. It will not count the one that triggered the event.#### jobRun
Emited once for each job when it starts running with:
*
`` job id (autoincrement)#### jobFinish
Emited once after calling
`start()` with an object containing:
* jobId: `` job id
* jobStartTime: `` date when job started to process
* jobEndTime: `` date when job finishes to process
* result: `` job result received
* jobsRunning: `` current amount of processing jobs. It will count the one that triggered the event.#### processFinish
Emited once after calling
`start()` with an object containing:
* startTime: `` date when start was called
* endTime: `` date when queue was fully processed
* processed: `` total amount of jobs processed
* errors: `` total amount of errors
* maxProceses: `` maxProceses passed to constructor. Default `1`
* stopOnError: `` stopOnError passed to constructor. Default `false`
* sourceType: `` Detected source type (`array`, `function`, `promise` or `stream`).
* status: `` queue status. will always be `finished` or `error`.#### pooling
Emited every time the source function returns
`null` as value and JobQ starts waiting to check again. Only emited if `pooling` is enabled, with an object containing:
* startTime: `` date when start was called
* processed: `` jobs processed so far
* errors: `` jobs errored so far
* maxProceses: `` maxProceses passed to constructor. Default `1`
* stopOnError: `` stopOnError passed to constructor. Default `false`
* sourceType: `` Detected source type (`array`, `function`, `promise` or `stream`).
* status: `` queue status. will always be pooling at this point.#### pause
Emited once after calling
`pause()` with an object containing:
* startTime: `` date when start was called
* processed: `` jobs processed so far
* errors: `` jobs errored so far
* maxProceses: `` maxProceses passed to constructor. Default `1`
* stopOnError: `` stopOnError passed to constructor. Default `false`
* sourceType: `` Detected source type (`array`, `function`, `promise` or `stream`).
* status: `` queue status. will always be paused at this point.#### resume
Emited once after calling
`resume()` with an object containing:
* startTime: `` date when start was called
* processed: `` jobs processed so far
* errors: `` jobs errored so far
* maxProceses: `` maxProceses passed to constructor. Default `1`
* stopOnError: `` stopOnError passed to constructor. Default `false`
* sourceType: `` Detected source type (`array`, `function`, `promise` or `stream`).
* status: `` queue status. will always be running at this point.#### error
Emited once for each job error:
*
`` error receivedSource
Source is where data will be fetched in order to be processed. It can be one of the following:
* `` like `[1, 2, 3]`.
* `` of promise like `[promise1, promise2, promise3]`.
* `` returning a value.
* `` returning a promise.
* `` returning nothing and passing data to `callback` with error as the first parameter and response as the second one.
* `` that supports `on('readable')` and `read()`.
* `` that resolves to any of the previous source types.IMPORTANT: When using
`Function` and `Promise` sources, you must pass `null` as value to stop execution.Process
Process function receives a value from the queue and be anby of the following:
* `` that returns a value
* `` that returns a Promise wich resolves to a value
* `` that returns nothing and executes a `callback`` with error as the first parameter and response as the second one.