source: trip-planner-front/node_modules/qjobs/examples/simple.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1
2// My non blocking main job
3var 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
22var myQueueJobs = new require('../qjobs')();
23
24// Let's add 30 job and add them to the queue
25for (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
30myQueueJobs.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
36myQueueJobs.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
43myQueueJobs.on('jobStart',function(args) {
44 console.log('jobStart',args);
45});
46
47// I want to know when each job has ended
48myQueueJobs.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
63myQueueJobs.on('pause',function(since) {
64 console.log('in pause since '+since+' milliseconds');
65});
66
67
68// JOBS !! leeeeeeeeeet's staaaaaaaart !
69myQueueJobs.run();
70
71var statId = setInterval(function() {
72 console.log(JSON.stringify(myQueueJobs.stats()));
73},1000);
Note: See TracBrowser for help on using the repository browser.