-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker02.js
89 lines (75 loc) · 2.42 KB
/
worker02.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
// Generated by CoffeeScript 1.8.0
(function() {
'use strict';
/*
Channel Example -- woker02
This app waits for work requests to become available in the 'urlq' queue.
Then, for each one it receives, the app get the page for the URL, computes an
SHA1 value on the request URL (req.url) and outputs that and the request URL
value (req.url) to the result queue (req.q) specified in the work request.
However, if it receives a '***stop***' message, it closes the connection
and quits immediately.
Usage:
cd demo/lib
node worker02.js
Use this app in conjunction with provider02.js. See the provider02 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 = new Channel();
channel.connect(function() {
initEventHandlers();
channel.pop(urlQueueName, onData);
return console.log('waiting for work...');
});
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(req) {
if (typeof req === 'object') {
console.log('worker01 processing request ', req);
return request(req.url, function(error, response, body) {
var sha1;
if (!error && response.statusCode === 200) {
sha1 = SHA1(body);
console.log(req.url + ' SHA1 = ' + sha1);
channel.push(req.q, {
url: req.url,
sha1: sha1
});
} else {
console.log(error);
channel.push(req.q, {
url: req.url,
err: error,
code: response.statusCode
});
}
return channel.pop(urlQueueName, onData);
});
} else {
if (typeof req === 'string' && req === '***stop***') {
console.log('worker02 stopping');
shutDown();
}
console.log('Unexpected message: ', req);
console.log('Type of message = ' + typeof req);
return shutDown();
}
};
shutDown = function() {
channel.end();
return process.exit();
};
}).call(this);