-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider02.js
122 lines (99 loc) · 3.09 KB
/
provider02.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Generated by CoffeeScript 1.8.0
(function() {
'use strict';
/*
Channel Example -- provider02
For each URL in the urls list, this app puts a work request in 'demo:urlq' queue
consumed by worker02 and waits for the results to be returned in 'demo:urlshaq01'
or whatever, depending on the providerId parameter.
Usage:
cd demo/lib
node provider02.js <providerId> [clear]
or
node provider02.js stop
where <providerId> is something to make this provider instance unique,
such as "01", "02", "foo", "bar", or whatever.
Example usage:
cd demo/lib
node provider02.js 01 clear
node provider02.js
node provider02.js
node provider02.js stop
Use this app in conjunction with worker02.js. See the worker02 source code
for more details.
*/
var Channel, channel, clearInitially, enqueueURLs, initEventHandlers, main, onData, providerId, resultQueueName, resultsExpected, shutDown, stopWorker, urlQueueName, urls;
Channel = require('node-redis-queue').Channel;
urlQueueName = 'demo:urlq';
providerId = process.argv[2];
if (!providerId) {
console.log('Missing provider id argument');
process.exit();
}
resultQueueName = 'demo:urlshaq' + providerId;
clearInitially = process.argv[3] === 'clear';
stopWorker = process.argv[2] === 'stop';
urls = ['http://www.google.com', 'http://www.yahoo.com', 'http://www.google.com/robots.txt', 'https://code.google.com'];
resultsExpected = 0;
channel = new Channel();
channel.connect(function() {
console.log('connected');
initEventHandlers();
return main();
});
initEventHandlers = function() {
channel.on('end', function() {
console.log('provider01 finished');
return shutDown();
});
return channel.on('error', function(error) {
console.log('provider01 stopping due to: ' + error);
return shutDown();
});
};
main = function() {
if (clearInitially) {
return channel.clear(urlQueueName, function() {
console.log('Cleared "' + urlQueueName + '"');
return channel.clear(resultQueueName, function() {
console.log('Cleared "' + resultQueueName + '"');
return shutDown();
});
});
} else {
if (!stopWorker) {
return enqueueURLs();
} else {
console.log('Stopping worker');
channel.push(urlQueueName, '***stop***');
return shutDown();
}
}
};
enqueueURLs = function() {
var url, _i, _len;
for (_i = 0, _len = urls.length; _i < _len; _i++) {
url = urls[_i];
console.log('Pushing "' + url + '"');
channel.push(urlQueueName, {
url: url,
q: resultQueueName
});
++resultsExpected;
}
channel.pop(resultQueueName, onData);
return console.log('waiting for responses from worker...');
};
onData = function(result) {
console.log('result = ', result);
if (--resultsExpected) {
return channel.pop(resultQueueName, onData);
} else {
return shutDown();
}
};
shutDown = function() {
channel.end();
return process.exit();
};
}).call(this);