Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (55 loc) · 2.03 KB
/
Copy pathapp.js
File metadata and controls
65 lines (55 loc) · 2.03 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
// example/app.js
// Qb Example App
// Provides an email-to-list service and an add-email-to-list service
// vendor
var QB = require('qb'),
config = require('config'), // simple config package
services = require('./services'),
redis = require('redis');
// Stick redis in the init options
var options = config.qb;
options.redis = redis.createClient();
// Setup
var qb = QB.init(options)
// Services provided
.can('email-to-list', 10 /* max concurrent callbacks */, services.emailToList /* callback func */)
.can('add-email-to-list', 50 /* max concurrent callbacks */, services.addEmailToList /* callback func */)
// Communication
.speaks(require('qb-http'), config.http)
.speaks(require('qb-messageq'), config.messageq)
// Middleware
.pre('push').use(QB.mdw.setTimestamp('received'))
.pre('process').use(QB.mdw.setTimestamp('process'))
.pre('finish')
.use(function (type, task, next) { // custom middleware
task.processing_time = Date.now() - task.process;
next();
})
// .use(function (type, task, next) {
// console.log('Finished processing a task in ' + type + ' at ' + task.process + ' in ' + task.processing_time + 'ms');
// })
.on('fail', function (err, type, task, next) {
qb.log.error('FAIL: Type %s: %s', type, err);
})
.on('process').use(endWhenIShould)
// Start!
.start()
// For messageq, you have to listen on channels rather than service-specific things
qb.contact('messageq://new-email')
.subscribe('add-email-to-list') // This will pass the message as a task to the service
qb.contact('messageq://mq-email')
// You can use a function, and have as many callbacks as you like.
.subscribe(function (msg) { msg.list = 'mq-list' }, 'email-to-list');
qb.log.info('qb started!')
// End during processing so we can see graceful ending
var shouldEnd = false;
function endWhenIShould (type, task, next) {
if (shouldEnd) {
shouldEnd = false;
process.nextTick(qb.end.bind(qb, function (){}));
}
next();
}
setTimeout(function () {
shouldEnd = true
}, 7000);