-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
106 lines (88 loc) · 3.01 KB
/
worker.js
File metadata and controls
106 lines (88 loc) · 3.01 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var BeanstalkdClient = require('./client'),
Counter = require('./counter'),
ExchangeScraper = require('./scraper'),
MongoHandler = require('./db'),
queue = require('./settings').queue_settings,
printer = require('./printer');
var client = new BeanstalkdClient();
var count = new Counter();
var scraper = new ExchangeScraper();
var mongo = new MongoHandler();
var success_counter = {};
var fail_counter = {};
function onceConnected() {
mongo.establishConnection(function() {
client.watchTube();
});
}
function onceWatched() {
client.consumeJob(function(jobid, payload) {
printer('Performing jobid: ' + jobid + ' with payload: ' + payload);
count.contains(payload, function(does_contain) {
if (!does_contain) {
count.initCounter(payload);
}
});
var parsed_payload = JSON.parse(payload);
scraper.getRate(parsed_payload, function(is_success, rate) {
count.getSuccess(payload, is_success, function(is_save) {
if (is_save)
mongo.saveRecord(rate);
client.destroyJob(jobid, [payload, is_success]);
});
});
});
}
function onceDestroyed(forwarded_value) {
var payload = forwarded_value[0],
is_success = forwarded_value[1];
count.increment(payload, is_success, function(is_run) {
if (is_run) {
if (is_success) {
printer('10 successful attempts have been made for the ' +
'payload: ' + payload);
}
else {
printer('3 failed attempts have been made for the payload: ' +
payload);
}
count.remove(payload, function(complete) {
if (complete) {
printer('All payloads have been run to completion');
mongo.endConnection(function() {
count.endClient();
client.endClient();
});
}
else {
onceWatched();
}
});
}
else {
var delay = queue.success_delay;
if (!is_success)
delay = queue.fail_delay;
client.produceJob(queue.priority, delay, queue.ttr,
[payload.toString()]
);
}
});
}
function onceFailed(method) {
printer('', 'The BeanstalkdClient method ' + method +
'has failed (please check settings.js that the input is accurate ' +
'and that you are not experiencing network issues)'
);
printer('Please run destroy.js to remove jobs built up in the queue');
mongo.endConnection(function() {
client.endClient();
});
}
client.createClient();
client
.on('connected', onceConnected)
.on('watched', onceWatched)
.on('destroyed', onceDestroyed)
.on('produced', onceWatched)
.on('failed', onceFailed);