-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider04.coffee
99 lines (85 loc) · 2.42 KB
/
provider04.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
93
94
95
96
97
98
'use strict'
###
WorkQueueMgr Example -- provider04
For each URL in the urls list, this app puts a work request in 'demo:urlq' queue to be
consumed by worker04. It then waits for the results to be returned in 'demo:urlshaq01'
or whatever result queue, depending on the providerId parameter.
Usage:
cd demo/lib
node provider04.js <providerId> [clear]
or
node provider04.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 provider04.js 01 clear
node provider04.js
node provider04.js
node provider04.js stop
Use this app in conjunction with worker02.js. See the worker02 source code
for more details.
###
WorkQueueMgr = require('node-redis-queue').WorkQueueMgr
urlQueueName = 'demo:urlq'
urlQueue = null
resultQueue = null
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
mgr = new WorkQueueMgr()
mgr.connect ->
console.log 'connected'
initEventHandlers()
createWorkQueues()
if stopWorker
stopOneWorker()
else if clearInitially
clearQueues()
else
sendURLs()
consumeResultQueue()
initEventHandlers = ->
mgr.on 'end', () ->
console.log 'provider04 finished'
process.exit()
mgr.on 'error', (error) ->
console.log 'provider01 stopping due to: ' + error
process.exit()
createWorkQueues = ->
urlQueue = mgr.createQueue urlQueueName
resultQueue = mgr.createQueue resultQueueName
return
clearQueues = ->
urlQueue.clear ->
console.log 'cleared "' + urlQueueName + '"'
resultQueue.clear ->
console.log 'cleared "' + resultQueueName + '"'
mgr.disconnect()
sendURLs = ->
unless stopWorker
for url in urls
console.log 'Publishing "' + url + '"'
urlQueue.send {url: url, q: resultQueueName}
++resultsExpected
console.log 'waiting for results from worker...'
consumeResultQueue = ->
resultQueue.consume (result, ack) ->
console.log 'result = ', result
ack()
mgr.end() unless --resultsExpected
stopOneWorker = ->
console.log 'Stopping worker'
urlQueue.send '***stop***'
mgr.disconnect()