[6a3a178] | 1 |
|
---|
| 2 | // My non blocking main job
|
---|
| 3 | var myjob = function(args,next) {
|
---|
| 4 |
|
---|
| 5 | // do nothing now but in 1 sec
|
---|
| 6 |
|
---|
| 7 | setTimeout(function() {
|
---|
| 8 |
|
---|
| 9 | // if i'm job id 10 or 20, let's add
|
---|
| 10 | // another job dynamicaly in the queue.
|
---|
| 11 | // It can be usefull for network operation (retry on timeout)
|
---|
| 12 |
|
---|
| 13 | if (args._jobId==10||args._jobId==20) {
|
---|
| 14 | myQueueJobs.add(myjob,[999,'bla '+args._jobId]);
|
---|
| 15 | }
|
---|
| 16 | next();
|
---|
| 17 | },Math.random(1000)*2000);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | // Notice the "new" before require, to be able to use more
|
---|
| 21 | // than one queue independently
|
---|
| 22 | var myQueueJobs = new require('../qjobs')();
|
---|
| 23 |
|
---|
| 24 | // Let's add 30 job and add them to the queue
|
---|
| 25 | for (var i = 0; i<30; i++) {
|
---|
| 26 | myQueueJobs.add(myjob,[i,'test1']);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | // I want to know when the first job has started
|
---|
| 30 | myQueueJobs.on('start',function() {
|
---|
| 31 | console.log('starting ...');
|
---|
| 32 | console.log(JSON.stringify(myQueueJobs.stats()));
|
---|
| 33 | });
|
---|
| 34 |
|
---|
| 35 | // I want to know when the last job has ended
|
---|
| 36 | myQueueJobs.on('end',function() {
|
---|
| 37 | clearInterval(statId);
|
---|
| 38 | console.log('end');
|
---|
| 39 | console.log(JSON.stringify(myQueueJobs.stats()));
|
---|
| 40 | });
|
---|
| 41 |
|
---|
| 42 | // I want to know when each job has started
|
---|
| 43 | myQueueJobs.on('jobStart',function(args) {
|
---|
| 44 | console.log('jobStart',args);
|
---|
| 45 | });
|
---|
| 46 |
|
---|
| 47 | // I want to know when each job has ended
|
---|
| 48 | myQueueJobs.on('jobEnd',function(args) {
|
---|
| 49 |
|
---|
| 50 | console.log('jobEnd',args);
|
---|
| 51 |
|
---|
| 52 | // If i'm jobId 10, then make a pause of 5 sec
|
---|
| 53 |
|
---|
| 54 | if (args._jobId == 10) {
|
---|
| 55 | myQueueJobs.pause(true);
|
---|
| 56 | setTimeout(function() {
|
---|
| 57 | myQueueJobs.pause(false);
|
---|
| 58 | },5000);
|
---|
| 59 | }
|
---|
| 60 | });
|
---|
| 61 |
|
---|
| 62 | // I want to know if queue is in pause every sec
|
---|
| 63 | myQueueJobs.on('pause',function(since) {
|
---|
| 64 | console.log('in pause since '+since+' milliseconds');
|
---|
| 65 | });
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | // JOBS !! leeeeeeeeeet's staaaaaaaart !
|
---|
| 69 | myQueueJobs.run();
|
---|
| 70 |
|
---|
| 71 | var statId = setInterval(function() {
|
---|
| 72 | console.log(JSON.stringify(myQueueJobs.stats()));
|
---|
| 73 | },1000);
|
---|