-
Notifications
You must be signed in to change notification settings - Fork 0
/
co-worker.js
61 lines (51 loc) · 2.53 KB
/
co-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
let taskRunner = require('../index'), // require('tasks-runner') to use it as dependency of your project
co = require('co');
// examples of connection url on http://mongodb.github.io/node-mongodb-native/2.0/tutorials/connecting/
// connect to set of mongos proxies
// let url = 'mongodb://localhost:50000,localhost:50001/myproject';
// connect to a ReplicaSet
// let url = 'mongodb://localhost:27017,localhost:27018/myproject?replicaSet=foo';
// connect to single server
let url = 'mongodb://localhost:27017/test';
// Set url for connection to mongo. Real connection will be created as soon as it will try to execute any query
taskRunner.connect(url);
co(function* () {
'use strict';
let taskProcessorFactory = function(taskName) {
// name contains task.name
// use it to decide what processor should you return to process this task
console.log('Providing processor for task with name: ' + taskName);
switch (taskName) {
case 'example 1':
return function* (data, previousTaskResult, extendedTaskInfo) {
console.log('Passed data during task scheduling: ' + data);
console.log('Result of previous task of the same group: ' + previousTaskResult);
console.log('Extended information about current task: ' + extendedTaskInfo);
};
case 'example 2':
return {
someMethod: function() {
console.log('do something');
},
run: function* (data, previousTaskResult, extendedTaskInfo) {
this.someMethod();
console.log('Passed data during task scheduling: ' + data);
console.log('Result of previous task of the same group: ' + previousTaskResult);
console.log('Extended information about current task: ' + extendedTaskInfo);
}
};
default:
throw new Error('Task processor is not defined for task: ' + taskName);
}
};
yield taskRunner.run({
scanInterval: 5, // 60 seconds
lockInterval: 60, // 60 seconds
tasksPerScanning: 1000,
taskProcessorFactory: taskProcessorFactory
});
console.log('First scanning iteration was finished, second scanning iteration was scheduled in scanInterval seconds');
});
process.on('SIGTERM', taskRunner.stop.bind(taskRunner));
process.on('SIGINT', taskRunner.stop.bind(taskRunner));