-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker01.js
More file actions
80 lines (65 loc) · 2.07 KB
/
worker01.js
File metadata and controls
80 lines (65 loc) · 2.07 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
// Generated by CoffeeScript 1.8.0
(function() {
'use strict';
/*
Channel Example -- worker01
This app waits for URLs to become available in the 'demo:urlq' queue, as provided
by worker01. Then, for each one it receives, the app gets the page for the URL,
computes an SHA1 value, and outputs it to the console log.
However, if it receives a '***stop***' message, it closes the connection and
quits immediately.
Usage:
cd demo/lib
node worker01.js
Use this app in conjunction with provider01.js. See the provider01 source code
for more details.
*/
var Channel, SHA1, channel, initEventHandlers, onData, request, shutDown, urlQueueName;
Channel = require('node-redis-queue').Channel;
request = require('request');
SHA1 = require('./lib/helpers/tinySHA1.r4.js').SHA1;
urlQueueName = 'demo:urlq';
channel = null;
channel = new Channel();
channel.connect(function() {
initEventHandlers();
channel.pop(urlQueueName, onData);
return console.log('Waiting for data...');
});
initEventHandlers = function() {
channel.on('end', function() {
console.log('worker01 detected Redis connection ended');
return shutDown();
});
return channel.on('error', function(error) {
console.log('worker01 stopping due to: ' + error);
return shutDown();
});
};
onData = function(url) {
console.log('message url = ' + url);
if (typeof url === 'string') {
if (url === '***stop***') {
console.log('worker01 stopping');
shutDown();
}
console.log('worker01 processing URL "' + url + '"');
return request(url, function(error, response, body) {
var sha1;
if (!error && response.statusCode === 200) {
sha1 = SHA1(body);
console.log(url + ' SHA1 = ' + sha1);
channel.pop(urlQueueName, onData);
} else {
console.log(error);
}
});
} else {
return console.log('Unexpected message: ', url);
}
};
shutDown = function() {
channel.end();
return process.exit();
};
}).call(this);