1 | [![Build Status](https://secure.travis-ci.org/franck34/qjobs.png)](http://travis-ci.org/franck34/qjobs)
|
---|
2 |
|
---|
3 | **qjobs**
|
---|
4 | ==================
|
---|
5 | ***Efficient queue job manager module for nodejs.***
|
---|
6 |
|
---|
7 | Features
|
---|
8 | --------------
|
---|
9 | * Concurrency limiter
|
---|
10 | * Dynamic queue, a job can be added while the queue is running
|
---|
11 | * Optional delay before continuing after max concurrency has been reached
|
---|
12 | * Support of pause/unpause
|
---|
13 | * Events emitter based: start, end, sleep, continu, jobStart, jobEnd
|
---|
14 | * Quick statistic function, so you can know where the queue is, at regular interval
|
---|
15 |
|
---|
16 | For what it can be usefull ?
|
---|
17 | ---------------------
|
---|
18 | Jobs which needs to run in parallels, but in a controled maner, example:
|
---|
19 | * Network scanners
|
---|
20 | * Parallels monitoring jobs
|
---|
21 | * Images/Videos related jobs
|
---|
22 |
|
---|
23 |
|
---|
24 | Compatibility :
|
---|
25 | ------------------
|
---|
26 | * not tested with nodejs < 0.10
|
---|
27 |
|
---|
28 |
|
---|
29 | Examples
|
---|
30 | --------------------
|
---|
31 |
|
---|
32 | (take a look at tests directory if you are looking for running samples)
|
---|
33 |
|
---|
34 |
|
---|
35 | ```
|
---|
36 | var qjobs = new require('./qjobs');
|
---|
37 |
|
---|
38 | // My non blocking main job
|
---|
39 | var myjob = function(args,next) {
|
---|
40 | setTimeout(function() {
|
---|
41 | console.log('Do something interesting here',args);
|
---|
42 | next();
|
---|
43 | },1000);
|
---|
44 | }
|
---|
45 |
|
---|
46 | var q = new qjobs({maxConcurrency:10});
|
---|
47 |
|
---|
48 | // Let's add 30 job to the queue
|
---|
49 | for (var i = 0; i<30; i++) {
|
---|
50 | q.add(myjob,[i,'test '+i]);
|
---|
51 | }
|
---|
52 |
|
---|
53 | q.on('start',function() {
|
---|
54 | console.log('Starting ...');
|
---|
55 | });
|
---|
56 |
|
---|
57 | q.on('end',function() {
|
---|
58 | console.log('... All jobs done');
|
---|
59 | });
|
---|
60 |
|
---|
61 | q.on('jobStart',function(args) {
|
---|
62 | console.log('jobStart',args);
|
---|
63 | });
|
---|
64 |
|
---|
65 | q.on('jobEnd',function(args) {
|
---|
66 |
|
---|
67 | console.log('jobend',args);
|
---|
68 |
|
---|
69 | // If i'm jobId 10, then make a pause of 5 sec
|
---|
70 |
|
---|
71 | if (args._jobId == 10) {
|
---|
72 | q.pause(true);
|
---|
73 | setTimeout(function() {
|
---|
74 | q.pause(false);
|
---|
75 | },5000);
|
---|
76 | }
|
---|
77 | });
|
---|
78 |
|
---|
79 | q.on('pause',function(since) {
|
---|
80 | console.log('in pause since '+since+' milliseconds');
|
---|
81 | });
|
---|
82 |
|
---|
83 | q.on('unpause',function() {
|
---|
84 | console.log('pause end, continu ..');
|
---|
85 | });
|
---|
86 |
|
---|
87 | q.run();
|
---|
88 |
|
---|
89 | //q.abort() will empty jobs list
|
---|
90 |
|
---|
91 | ```
|
---|
92 |
|
---|