forked from senecajs/seneca-redis-queue-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis-queue-transport.js
181 lines (149 loc) · 4.91 KB
/
redis-queue-transport.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright (c) 2015 Cristian Ianto, Richard Rodger MIT License */
'use strict'
var _ = require('lodash')
var Redis = require('redis')
var internals = {
defaults: {
'redis-queue': {
timeout: 22222,
type: 'redis-queue',
host: 'localhost',
port: 6379
}
}
}
module.exports = function (options) {
var seneca = this
var plugin = 'redis-queue-transport'
var so = seneca.options()
internals.defaults.timeout = so.timeout ? (so.timeout - 555) : internals.defaults.timeout
options = seneca.util.deepextend(internals.defaults, so.transport, options)
var tu = seneca.export('transport/utils')
seneca.add({role: 'transport', hook: 'listen', type: 'redis-queue'}, hook_listen_redis)
seneca.add({role: 'transport', hook: 'client', type: 'redis-queue'}, hook_client_redis)
function hook_listen_redis (args, done) {
var seneca = this
var type = args.type
var listen_options = seneca.util.clean(_.extend({}, options[type], args))
var redis_in = Redis.createClient(listen_options.port, listen_options.host)
var redis_out = Redis.createClient(listen_options.port, listen_options.host)
handle_events(redis_in)
handle_events(redis_out)
function on_message (topic, msgstr) {
var data = tu.parseJSON(seneca, 'listen-' + type, msgstr)
tu.handle_request(seneca, data, listen_options, function (out) {
if (out === null) {
return
}
var outstr = tu.stringifyJSON(seneca, 'listen-' + type, out)
redis_out.lpush(topic + '_res' + '/' + data.origin, outstr, function (err, reply) {
if (err) {
seneca.log.error('transport', 'redis-queue', err)
}
})
})
}
tu.listen_topics(seneca, args, listen_options, function (topic) {
seneca.log.debug('listen', 'brpop', topic + '_act', listen_options, seneca)
var waiting = false
function next () {
waiting = true
redis_in.brpop(topic + '_act', 0, function (err, reply) {
if (err) {
seneca.log.error('transport', 'server-redis-brpop', err)
waiting = false
return
}
if (!reply.length) {
waiting = false
return
}
on_message(topic, reply[1])
setImmediate(next)
})
}
redis_in.on('idle', function () {
setImmediate(function () {
if (!waiting) {
next()
}
})
})
})
seneca.add('role:seneca,cmd:close', function (close_args, done) {
var closer = this
redis_in.end()
redis_out.end()
closer.prior(close_args, done)
})
seneca.log.info('listen', 'open', listen_options, seneca)
done()
}
function hook_client_redis (args, clientdone) {
var seneca = this
var type = args.type
var client_options = seneca.util.clean(_.extend({}, options[type], args))
tu.make_client(make_send, client_options, clientdone)
function make_send (spec, topic, send_done) {
var redis_in = Redis.createClient(client_options.port, client_options.host)
var redis_out = Redis.createClient(client_options.port, client_options.host)
handle_events(redis_in)
handle_events(redis_out)
function on_message (topic, msgstr) {
var input = tu.parseJSON(seneca, 'client-' + type, msgstr)
tu.handle_response(seneca, input, client_options)
}
seneca.log.debug('client', 'brpop', topic + '_res', client_options, seneca)
var waiting = false
function next () {
waiting = true
redis_in.brpop(topic + '_res' + '/' + seneca.id, 0, function (err, reply) {
if (err) {
seneca.log.error('transport', 'client-redis-brpop', err)
waiting = false
return
}
if (!reply.length) {
waiting = false
return
}
on_message(topic, reply[1])
setImmediate(next)
})
}
redis_in.on('idle', function () {
setImmediate(function () {
if (!waiting) {
next()
}
})
})
send_done(null, function (args, done) {
var outmsg = tu.prepare_request(this, args, done)
var outstr = tu.stringifyJSON(seneca, 'client-' + type, outmsg)
redis_out.lpush(topic + '_act', outstr, function (err, reply) {
if (err) {
seneca.log.error('transport', 'redis-queue', err)
}
})
})
seneca.add('role:seneca,cmd:close', function (close_args, done) {
var closer = this
redis_in.end()
redis_out.end()
closer.prior(close_args, done)
})
}
}
function handle_events (redisclient) {
// Die if you can't connect initially
redisclient.on('ready', function () {
redisclient.on('error', function (err) {
seneca.log.error('transport', 'redis', err)
})
})
}
return {
name: plugin
}
}