-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider01.coffee
62 lines (55 loc) · 1.44 KB
/
provider01.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
'use strict'
###
Channel Example -- provider01
For each URL in the urls list, this app pushes it into the 'urlq' queue
for consumption by worker01. When done with that, it quits.
Usage:
cd demo/lib
node provider01.js clear
node provider01.js
...
node provider01.js stop
Use this app in conjunction with worker01.js. See the worker01 source code
for more details.
###
Channel = require('node-redis-queue').Channel
urlQueueName = 'demo:urlq'
channel = null
clearInitially = process.argv[2] 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'
]
channel = new Channel()
channel.connect ->
console.log 'connected'
initEventHandlers()
main()
initEventHandlers = ->
channel.on 'end', () ->
console.log 'provider01 finished'
process.exit()
.on 'error', (error) ->
console.log 'provider01 stopping due to: ' + error
process.exit()
main = ->
if clearInitially
channel.clear urlQueueName, () ->
console.log 'Cleared "' + urlQueueName + '"'
enqueueURLs()
channel.disconnect()
else
unless stopWorker
enqueueURLs()
else
console.log 'Stopping worker'
channel.push urlQueueName, '***stop***'
channel.disconnect()
enqueueURLs = ->
for url in urls
console.log 'Pushing "' + url + '" to queue "' + urlQueueName + '"'
channel.push urlQueueName, url
return