-
Notifications
You must be signed in to change notification settings - Fork 8
/
mount.coffee
375 lines (314 loc) · 11.9 KB
/
mount.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
common = require './common'
newrelic = require './newrelic'
runtime = require './runtime'
path = require 'path'
noflo = require 'noflo'
async = require 'async'
msgflo = require 'msgflo-nodejs'
uuid = require 'uuid'
trace = require('noflo-runtime-base').trace
debug = require('debug')('noflo-runtime-msgflo:mount')
debugError = require('debug')('noflo-runtime-msgflo:error')
wrapInport = (transactions, instance, port) ->
debug 'wrapInport', port, Object.keys instance.inPorts?.ports
socket = noflo.internalSocket.createSocket()
instance.inPorts[port].attach socket
onMessage = (msg) ->
groupId = msg?.amqp?.fields?.deliveryTag
groupId = uuid.v4() if not groupId
debug 'onInMessage', typeof msg.data, msg.data, groupId
return unless msg.data
transactions.open groupId, port
socket.connect()
socket.beginGroup groupId
socket.send msg.data
socket.endGroup groupId
socket.disconnect()
return onMessage
wrapOutport = (transactions, client, instance, port, queueName, callback) ->
debug 'wrapOutport', port, queueName
socket = noflo.internalSocket.createSocket()
instance.outPorts[port].attach socket
groups = []
dataReceived = false
# TODO: NACK or kill when output is inproperly grouped
socket.on 'begingroup', (group) ->
debug 'beginGroup', port, group
groups.push group
socket.on 'endgroup', (group) ->
debug 'endGroup', port, group
groups.pop()
return if groups.length
return unless dataReceived
dataReceived = false
# ack/nack
msg =
amqp:
fields:
deliveryTag: group
data: null
result =
id: group
type: null
if port is 'error'
client.nackMessage msg, false, false
result.type = 'error'
else
client.ackMessage msg
result.type = 'ok'
transactions.close group, port
callback result
socket.on 'disconnect', ->
debug 'onDisconnect', port, groups
groups = []
socket.on 'data', (data) ->
debug 'onOutMessage', port, typeof data, groups
dataReceived = true if groups.length
debugError 'ERROR', data if port == 'error'
debugError 'STACK', '\n'+data.stack if port == 'error' and data?.stack
# Send to outport
return if not queueName # hidden
client.sendTo 'outqueue', queueName, data, (err) ->
debug 'sent output data', queueName, err, data
# Execute all packets using the existing NoFlo network
wrapPortsOnExisting = (transactions, client, instance, definition) ->
for port in definition.inports
continue unless port.queue
continue unless port.id
# Normal mode is to reuse same network for each message
onMessage = wrapInport transactions, instance, port.id
client.subscribeToQueue port.queue, onMessage, (err) ->
throw err if err
for port in definition.outports
wrapOutport transactions, client, instance, port.id, port.queue, ->
# Execute each packet using a dedicated NoFlo network
wrapPortsDedicated = (transactions, client, loader, instances, definition, options) ->
definition.inports.forEach (port) ->
return unless port.queue
return unless port.id
onMessage = (msg) ->
loadAndStartGraph loader, options.graph, options.iips, (err, instance) ->
throw err if err
debug 'wrapPortsDedicated network started'
instances.push instance
for outPort in definition.outports
wrapOutport transactions, client, instance, outPort.id, outPort.queue, (result) ->
setTimeout ->
debug 'wrapPortsDedicated network shutdown'
instance.shutdown (err) ->
throw err if err
instances.splice instances.indexOf(instance), 1
, 1
wrappedIn = wrapInport transactions, instance, port.id
wrappedIn msg
client.subscribeToQueue port.queue, onMessage, (err) ->
throw err if err
setupQueues = (client, def, callback) ->
setupIn = (port, cb) ->
client.createQueue 'inqueue', port.queue, cb
setupOut = (port, cb) ->
client.createQueue 'outqueue', port.queue, cb
inports = def.inports.filter (p) -> not p.hidden
outports = def.outports.filter (p) -> not p.hidden
async.map inports, setupIn, (err) ->
return callback err if err
async.map outports, setupOut, callback
setupDeadLettering = (client, inports, deadletters, callback) ->
# NOTE: relies on msgflo setting up the deadletter exchange with matching naming convention
setupDeadletter = (name, cb) ->
port = inports.filter((p) -> p.id == name)[0]
deadletter = 'dead-'+port.queue
client.createQueue 'inqueue', deadletter, cb
async.map deadletters, setupDeadletter, callback
sendIIPs = (instance, iips) ->
for port, data of iips
socket = noflo.internalSocket.createSocket()
instance.inPorts[port].attach socket
socket.connect()
socket.send data
socket.disconnect()
loadAndStartGraph = (loader, graphName, iips, callback) ->
loader.load graphName, (err, instance) ->
return callback err if err
onReady = () ->
onStarted = (err) ->
return callback err if err
# needs to happen after NoFlo network has sent its IIPs
debug 'sending IIPs', Object.keys(iips)
sendIIPs instance, iips
return callback null, instance
if instance.isSubgraph() and instance.network
# Subgraphs need process-error handling
instance.network.on 'process-error', (err) ->
console.log err.id, err.error?.message, err.error?.stack
setTimeout ->
# Need to not throw syncronously to avoid cascading affects
throw err.error
, 0
# Tell Network to start sending IIPs
instance.start onStarted
if instance.isReady()
onReady()
else
instance.once 'ready', onReady
getDefinition = (instance, options) ->
definition =
component: options.graph
icon: instance.getIcon()
label: instance.getDescription()
inports: []
outports: []
# TODO: read out type annotations
for name in Object.keys instance.inPorts.ports
port =
id: name
type: 'all'
definition.inports.push port
for name in Object.keys instance.outPorts.ports
port =
id: name
type: 'all'
definition.outports.push port
# merge in port overrides from options
for id, port of options.inports
def = definition.inports.filter((p) -> p.id == id)[0]
for k, v of port
def[k] = v if v
for id, port of options.outports
def = definition.outports.filter((p) -> p.id == id)[0]
for k, v of port
def[k] = v if v
def = msgflo.participant.instantiateDefinition definition, options.name
return def
applyOption = (obj, option) ->
tokens = option.split '='
if tokens.length > 1
value = tokens.slice(1).join('=')
key = tokens[0]
keys = key.split '.'
target = obj
for k,i in keys
last = (i == keys.length-1)
if last
target[k] = value
else
target[k] = {} if not target[k]
target = target[k]
exports.normalizeOptions = normalizeOptions = (opt) ->
options = common.clone opt
options.deadletter = options.deadletter.split(',') if options.deadletter
# defaults
options.deadletter = [] if not options.deadletter
options.inports = {} if not options.inports
options.outports = {} if not options.outports
options.basedir = process.cwd() if not options.basedir
options.prefetch = 1 if not options.prefetch
options.iips = '{}' if not options.iips
options.dedicatedNetwork = false unless options.dedicatedNetwork
options.discoveryInterval = process.env['MSGFLO_DISCOVERY_PERIOD'] unless options.discoveryInterval
options.broker = process.env['MSGFLO_BROKER'] if not options.broker
options.broker = process.env['CLOUDAMQP_URL'] if not options.broker
options.discoveryInterval = 60 unless options.discoveryInterval
options.iips = JSON.parse options.iips
if options.attr and options.attr.length
for option in options.attr
applyOption options, option
delete options.option
return options
class Mounter
constructor: (options) ->
@options = normalizeOptions options
@client = msgflo.transport.getClient @options.broker, { prefetch: @options.prefetch }
loader = options.loader or new noflo.ComponentLoader @options.basedir, { cache: @options.cache }
@loader = loader
@tracer = new trace.Tracer {}
@instances = [] # noflo.Component instances
@transactions = null # loaded with instance
@coordinator = null
@discoveryInterval = null
start: (callback) ->
debug 'starting'
@client.connect (err) =>
return callback err if err
loadAndStartGraph @loader, @options.graph, @options.iips, (err, instance) =>
return callback err if err
debug 'started graph', @options.graph
@instances.push instance
@tracer.attach instance.network if @options.trace
definition = @getDefinition instance
@transactions = new newrelic.Transactions definition
@coordinator = new runtime.SendReceivePair @client, ".fbp.#{definition.id}"
@coordinator.onReceive = (data) =>
@handleFbpMessage data
@coordinator.create (err) =>
return callback err if err
@setupQueuesForComponent instance, definition, (err) =>
return callback err if err
# Connect queues to instance
if @options.dedicatedNetwork
debug 'setup in dedicated network mode'
wrapPortsDedicated @transactions, @client, @loader, @instances, definition, @options
else
debug 'setup in single network mode'
wrapPortsOnExisting @transactions, @client, instance, definition
# Send discovery package, first once
@sendParticipant definition, (err) =>
# then periodically
@discoveryInterval = setInterval () =>
@sendParticipant definition, (err) ->
console.log 'WARNING: Failed to send Msgflo discovery', err if err
, @options.discoveryInterval*1000/2.2
return callback err, @options
stop: (callback) ->
return callback null if not @instances.length
clearInterval @discoveryInterval
debug "stopping #{@instances.length} instances"
async.each @instances, (instance, done) ->
instance.shutdown done
, (err) =>
@instances = []
debug 'stopped component instances', err
return callback err if not @coordinator
@coordinator.destroy (err) =>
debug 'coordinator connection destroyed', err
@coordinator = null
return callback null if not @client
@client.disconnect (err) =>
debug 'disconnected client', err
@client = null
return callback err
setupQueuesForComponent: (instance, definition, callback) ->
setupQueues @client, definition, (err) =>
debug 'queues set up', err
return callback err if err
setupDeadLettering @client, definition.inports, @options.deadletter, (err) =>
return callback err if err
return callback null
getDefinition: () ->
return null if not @instances.length
return getDefinition @instances[0], @options
sendParticipant: (definition, callback) ->
debug 'sendParticipant', definition.id
@client.registerParticipant definition, callback
handleFbpMessage: (data) ->
{ protocol, command, payload } = data
return if protocol != 'trace'
# Handle trace subprotocol
switch command
when 'start'
for instance in @instances
@tracer.attach instance.network
@coordinator?.send data
when 'stop'
for instance in @instances
@tracer.detach instance.network
@coordinator?.send data
when 'clear'
null # FIXME: implement
@coordinator?.send data
when 'dump'
@tracer.dumpString (err, trace) =>
reply = common.clone data
reply.payload.flowtrace = trace
@coordinator?.send reply
exports.Mounter = Mounter