-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider02.coffee
92 lines (80 loc) · 2.25 KB
/
provider02.coffee
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
'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.
###
Channel = require('node-redis-queue').Channel
urlQueueName = 'demo:urlq'
providerId = process.argv[2]
unless providerId
console.log 'Missing provider id argument'
process.exit()
resultQueueName = 'demo:urlshaq' + providerId
clearInitially = process.argv[3] is 'clear'
stopWorker = process.argv[2] is '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 ->
console.log 'connected'
initEventHandlers()
main()
initEventHandlers = ->
channel.on 'end', ->
console.log 'provider01 finished'
shutDown()
channel.on 'error', (error) ->
console.log 'provider01 stopping due to: ' + error
shutDown()
main = ->
if clearInitially
channel.clear urlQueueName, ->
console.log 'Cleared "' + urlQueueName + '"'
channel.clear resultQueueName, ->
console.log 'Cleared "' + resultQueueName + '"'
shutDown()
else
unless stopWorker
enqueueURLs()
else
console.log 'Stopping worker'
channel.push urlQueueName, '***stop***'
shutDown()
enqueueURLs = ->
for url in urls
console.log 'Pushing "' + url + '"'
channel.push urlQueueName, {url: url, q: resultQueueName}
++resultsExpected
channel.pop resultQueueName, onData
console.log 'waiting for responses from worker...'
onData = (result) ->
console.log 'result = ', result
if --resultsExpected
channel.pop resultQueueName, onData
else
shutDown()
shutDown = ->
channel.end()
process.exit()