-
Notifications
You must be signed in to change notification settings - Fork 4
/
sailor.js
664 lines (577 loc) · 25.6 KB
/
sailor.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
const uuid = require('uuid');
const ComponentReader = require('./component_reader.js').ComponentReader;
const amqp = require('./amqp.js');
const TaskExec = require('./executor.js').TaskExec;
const log = require('./logging.js');
const _ = require('lodash');
const hooksData = require('./hooksData');
const Encryptor = require('../lib/encryptor');
const RestApiClient = require('elasticio-rest-node');
const assert = require('assert');
const co = require('co');
const pThrottle = require('p-throttle');
const { ObjectStorage } = require('@elastic.io/maester-client');
const { Readable } = require('stream');
const AMQP_HEADER_META_PREFIX = 'x-eio-meta-';
const OBJECT_ID_HEADER = 'x-ipaas-object-storage-id';
function convertSettingsToCamelCase(settings) {
return _.mapKeys(settings, (value, key) => _.camelCase(key));
}
function getAdditionalHeadersFromSettings(settings) {
return convertSettingsToCamelCase(settings.additionalVars);
}
class Sailor {
static get OBJECT_ID_HEADER() {
return OBJECT_ID_HEADER;
}
constructor(settings) {
this.settings = settings;
this.messagesCount = 0;
this.amqpConnection = new amqp.Amqp(settings);
this.componentReader = new ComponentReader();
this.snapshot = {};
this.stepData = {};
this.shutdownCallback = null;
//eslint-disable-next-line new-cap
this.apiClient = RestApiClient(
settings.API_USERNAME,
settings.API_KEY,
{
retryCount: settings.API_REQUEST_RETRY_ATTEMPTS,
retryDelay: settings.API_REQUEST_RETRY_DELAY
});
const objectStorage = new ObjectStorage({
uri: settings.OBJECT_STORAGE_URI,
jwtSecret: settings.OBJECT_STORAGE_TOKEN
});
const encryptor = new Encryptor(settings.MESSAGE_CRYPTO_PASSWORD, settings.MESSAGE_CRYPTO_IV);
this.objectStorage = objectStorage.use(
() => encryptor.createCipher(),
() => encryptor.createDecipher()
);
this.throttles = {
// 100 Messages per Second
data: pThrottle(() => Promise.resolve(true),
settings.DATA_RATE_LIMIT,
settings.RATE_INTERVAL),
error: pThrottle(() => Promise.resolve(true),
settings.ERROR_RATE_LIMIT,
settings.RATE_INTERVAL),
snapshot: pThrottle(() => Promise.resolve(true),
settings.SNAPSHOT_RATE_LIMIT,
settings.RATE_INTERVAL)
};
}
async connect() {
return this.amqpConnection.connect(this.settings.AMQP_URI);
}
async prepare() {
const {
settings: {
COMPONENT_PATH: compPath,
FLOW_ID: flowId,
STEP_ID: stepId
},
apiClient,
componentReader
} = this;
const stepData = await apiClient.tasks.retrieveStep(flowId, stepId);
log.debug('Received step data');
assert(stepData);
Object.assign(this, {
snapshot: stepData.snapshot || {},
stepData
});
this.stepData = stepData;
await componentReader.init(compPath);
}
async disconnect() {
log.debug('Disconnecting, %s messages in processing', this.messagesCount);
return this.amqpConnection.disconnect();
}
reportError(err) {
const headers = Object.assign({}, getAdditionalHeadersFromSettings(this.settings), {
execId: this.settings.EXEC_ID,
taskId: this.settings.FLOW_ID,
workspaceId: this.settings.WORKSPACE_ID,
containerId: this.settings.CONTAINER_ID,
userId: this.settings.USER_ID,
stepId: this.settings.STEP_ID,
compId: this.settings.COMP_ID,
function: this.settings.FUNCTION
});
return this.amqpConnection.sendError(err, headers);
}
startup() {
return co(function* doStartup() {
log.debug('Starting up component');
const result = yield this.invokeModuleFunction('startup');
log.trace('Startup data received');
const handle = hooksData.startup(this.settings);
try {
const state = _.isEmpty(result) ? {} : result;
yield handle.create(state);
} catch (e) {
if (e.statusCode === 409) {
log.warn('Startup data already exists. Rewriting.');
yield handle.delete();
yield handle.create(result);
} else {
log.warn('Component starting error');
throw e;
}
}
log.debug('Component started up');
return result;
}.bind(this));
}
runHookShutdown() {
return co(function* doShutdown() {
log.debug('About to shut down');
const handle = hooksData.startup(this.settings);
const state = yield handle.retrieve();
yield this.invokeModuleFunction('shutdown', state);
yield handle.delete();
log.debug('Shut down successfully');
}.bind(this));
}
runHookInit() {
return co(function* doInit() {
log.debug('About to initialize component for execution');
const res = yield this.invokeModuleFunction('init');
log.debug('Component execution initialized successfully');
return res;
}.bind(this));
}
invokeModuleFunction(moduleFunction, data) {
const settings = this.settings;
const stepData = this.stepData;
return co(function* gen() {
const module = yield this.componentReader.loadTriggerOrAction(settings.FUNCTION);
if (!module[moduleFunction]) {
log.warn(`invokeModuleFunction – ${moduleFunction} is not found`);
return Promise.resolve();
}
const cfg = _.cloneDeep(stepData.config) || {};
return new Promise((resolve, reject) => {
try {
resolve(module[moduleFunction](cfg, data));
} catch (e) {
reject(e);
}
});
}.bind(this));
}
run() {
const incomingQueue = this.settings.LISTEN_MESSAGES_ON;
const handler = this.processMessageAndMaybeShutdownCallback.bind(this);
log.debug('Start listening for messages on %s', incomingQueue);
return this.amqpConnection.listenQueue(incomingQueue, handler);
}
async processMessageAndMaybeShutdownCallback(payload, message) {
try {
return await this.processMessage(payload, message);
} catch (e) {
log.error('Something very bad happened during message processing');
} finally {
if (this.shutdownCallback) {
if (this.messagesCount === 0) {
// there is no another processMessage invocation, so it's time to call shutdownCallback
log.debug('About to invoke shutdownCallback');
this.shutdownCallback();
this.shutdownCallback = null;
} else {
// there is another not finished processMessage invocation
log.debug('No shutdownCallback since messagesCount is not zero');
}
}
}
}
async scheduleShutdown() {
if (this.shutdownCallback) {
log.debug('scheduleShutdown – shutdown is already scheduled, do nothing');
return new Promise(resolve => this.shutdownCallback = resolve);
}
await this.amqpConnection.stopConsume();
if (this.messagesCount === 0) {
// there is no unfinished processMessage invocation, let's just resolve scheduleShutdown now
log.debug('scheduleShutdown – about to shutdown immediately');
return Promise.resolve();
}
// at least one processMessage invocation is not finished yet
// let's return a Promise, which will be resolved by processMessageAndMaybeShutdownCallback
log.debug('scheduleShutdown – shutdown is scheduled');
return new Promise(resolve => this.shutdownCallback = resolve);
}
readIncomingMessageHeaders(message) {
const { headers } = message.properties;
// Get meta headers
const metaHeaderNames = Object.keys(headers)
.filter(key => key.toLowerCase().startsWith(AMQP_HEADER_META_PREFIX));
const metaHeaders = _.pick(headers, metaHeaderNames);
const metaHeadersLowerCased = _.mapKeys(metaHeaders, (value, key) => key.toLowerCase());
const result = {
stepId: headers.stepId, // the only use is passthrough mechanism
...metaHeadersLowerCased,
threadId: headers.threadId || metaHeadersLowerCased['x-eio-meta-trace-id'],
messageId: headers.messageId,
parentMessageId: headers.parentMessageId
};
if (!result.threadId) {
const threadId = uuid.v4();
log.debug({ threadId }, 'Initiate new thread as it is not started ATM');
result.threadId = threadId;
}
if (headers.reply_to) {
result.reply_to = headers.reply_to;
}
return result;
}
async fetchMessageBody(message, logger) {
const { body, headers } = message;
logger.info('Checking if incoming messages is lightweight...');
if (!headers) {
logger.info('Empty headers so not lightweight.');
return body;
}
const { [OBJECT_ID_HEADER]: objectId } = headers;
if (!objectId) {
logger.trace('No object id header so not lightweight.');
return body;
}
logger.info('Object id header found, message is lightweight.', { objectId });
let object;
logger.info('Going to fetch message body.', { objectId });
try {
object = await this.objectStorage.getOne(
objectId,
{ jwtPayloadOrToken: this.settings.OBJECT_STORAGE_TOKEN }
);
} catch (e) {
log.error(e);
throw new Error(`Failed to get message body with id=${objectId}`);
}
logger.info('Successfully obtained message body.', { objectId });
logger.trace('Message body object received');
return object.data;
}
uploadMessageBody(bodyBuf) {
const stream = () => Readable.from(bodyBuf);
return this.objectStorage.add(
stream,
{ jwtPayloadOrToken: this.settings.OBJECT_STORAGE_TOKEN }
);
}
async runExec(module, payload, message, outgoingMessageHeaders, stepData, timeStart, logger) {
const origPassthrough = _.cloneDeep(payload.passthrough) || {};
const incomingMessageHeaders = this.readIncomingMessageHeaders(message);
const settings = this.settings;
const cfg = _.cloneDeep(stepData.config) || {};
const snapshot = _.cloneDeep(this.snapshot);
const { deliveryTag } = message.fields;
const that = this;
await new Promise(resolve => {
let endWasEmitted;
const taskExec = new TaskExec({
loggerOptions: _.pick(incomingMessageHeaders, ['threadId', 'messageId', 'parentMessageId']),
variables: stepData.variables,
services: {
apiClient: this.apiClient,
amqp: this.amqpConnection,
config: this.settings
}
});
taskExec
.on('data', onData)
.on('error', onError)
.on('rebound', onRebound)
.on('snapshot', onSnapshot)
.on('updateSnapshot', onUpdateSnapshot)
.on('updateKeys', onUpdateKeys)
.on('httpReply', onHttpReply)
.on('end', onEnd);
taskExec.process(module, payload, cfg, snapshot);
async function onData(data) {
const headers = _.clone(outgoingMessageHeaders);
headers.messageId = data.id || headers.messageId;
logger.trace({
messagesCount: that.messagesCount,
messageProcessingTime: Date.now() - timeStart
}, 'processMessage emit data');
headers.end = new Date().getTime();
if (stepData.is_passthrough === true) {
data.passthrough = { ...origPassthrough };
if (settings.NO_SELF_PASSTRHOUGH) {
const { stepId } = incomingMessageHeaders;
if (stepId) {
data.passthrough = Object.assign({}, origPassthrough, {
[stepId]: Object.assign({}, _.omit(payload, 'passthrough'))
});
}
}
}
data.headers = data.headers || {};
const { body, passthrough = {} } = data;
if (settings.EMIT_LIGHTWEIGHT_MESSAGE) {
logger.trace('Outgoing lightweight is enabled, going to check size.');
const bodyBuf = Buffer.from(JSON.stringify(body), 'utf-8');
const passthroughBufs = Object.keys(passthrough).map(stepId => ({
stepId,
body: Buffer.from(JSON.stringify(passthrough[stepId].body), 'utf-8'),
id: passthrough[stepId].headers && passthrough[stepId].headers[OBJECT_ID_HEADER]
}));
const totalLength = passthroughBufs.reduce((len, { body }) =>
len + body.length, bodyBuf.length);
if (totalLength > settings.OBJECT_STORAGE_SIZE_THRESHOLD) {
logger.info(
'Message size is above threshold, going to upload',
{
totalLength,
OBJECT_STORAGE_SIZE_THRESHOLD: settings.OBJECT_STORAGE_SIZE_THRESHOLD
}
);
let bodyId;
let passthroughIds;
try {
[bodyId, ...passthroughIds] = await Promise.all([
that.uploadMessageBody(bodyBuf),
...passthroughBufs.map(async ({ stepId, body, id }) => {
const bodyId = id || await that.uploadMessageBody(body);
return { stepId, bodyId };
})
]);
} catch (e) {
logger.error(e, 'Error during message/passthrough body upload');
return onError(new Error('Lightweight message/passthrough body upload error'));
}
logger.info('Message body uploaded', { id: bodyId });
const { headers } = data;
data.body = {};
data.headers = {
...(headers || {}),
[OBJECT_ID_HEADER]: bodyId
};
for (const { stepId, bodyId } of passthroughIds) {
logger.info('Passthrough Message body uploaded', { stepId, id: bodyId });
const { [stepId]: { headers } } = passthrough;
data.passthrough[stepId].body = {};
data.passthrough[stepId].headers = {
...(headers || {}),
[OBJECT_ID_HEADER]: bodyId
};
}
} else {
logger.trace(
'Message size is below threshold.',
{
totalLength,
OBJECT_STORAGE_SIZE_THRESHOLD: settings.OBJECT_STORAGE_SIZE_THRESHOLD
}
);
}
} else if (passthrough) {
logger.trace('Outgoing lightweight is disabled, going to download all bodies.');
try {
await Promise.all(Object.keys(passthrough).map(async stepId => {
logger.trace('Going to check if passthrough for step is lightweight.', { stepId });
// if body is not empty then we've downloaded before processing, no need to redownload
if (!_.isEmpty(data.passthrough[stepId].body)) {
logger.trace('Body is not empty.', { stepId });
return;
}
data.passthrough[stepId].body = await that.fetchMessageBody(
passthrough[stepId],
logger
);
}));
} catch (e) {
return onError(e);
}
}
if (stepData.is_passthrough === true && !settings.NO_SELF_PASSTRHOUGH) {
data.passthrough = Object.assign({}, origPassthrough, {
[settings.STEP_ID]: Object.assign({}, _.omit(data, 'passthrough'))
});
}
log.trace('Going to send outgoing message');
try {
await that.amqpConnection.sendData(data, headers, that.throttles.data);
log.trace('Outgoing message sent');
} catch (err) {
return onError(err);
}
}
async function onHttpReply(reply) {
const headers = _.clone(outgoingMessageHeaders);
logger.trace({
messageProcessingTime: Date.now() - timeStart
}, 'processMessage emit HttpReply');
return that.amqpConnection.sendHttpReply(reply, headers);
}
async function onError(err) {
const headers = _.clone(outgoingMessageHeaders);
err = formatError(err);
taskExec.errorCount++;
logger.trace({
err,
messagesCount: that.messagesCount,
messageProcessingTime: Date.now() - timeStart
}, 'processMessage emit error');
headers.end = new Date().getTime();
return that.amqpConnection.sendError(err, headers, message, that.throttles.error);
}
async function onRebound(err) {
err = formatError(err);
logger.trace({
err,
messagesCount: that.messagesCount,
messageProcessingTime: Date.now() - timeStart
}, 'processMessage emit rebound');
return that.amqpConnection.sendRebound(err, message);
}
async function onSnapshot(data) {
const headers = _.clone(outgoingMessageHeaders);
headers.snapshotEvent = 'snapshot';
that.snapshot = data; //replacing `local` snapshot
return that.amqpConnection.sendSnapshot(data, headers, that.throttles.snapshot);
}
async function onUpdateSnapshot(data) {
const headers = _.clone(outgoingMessageHeaders);
headers.snapshotEvent = 'updateSnapshot';
if (_.isPlainObject(data)) {
if (data.$set) {
return log.warn('ERROR: $set is not supported any more in `updateSnapshot` event');
}
_.extend(that.snapshot, data); //updating `local` snapshot
return that.amqpConnection.sendSnapshot(data, headers);
} else {
log.error('You should pass an object to the `updateSnapshot` event');
}
}
async function onUpdateKeys(keys) {
logger.trace({
messageProcessingTime: Date.now() - timeStart
}, 'processMessage emit updateKeys');
try {
await that.apiClient.accounts.update(cfg._account, { keys: keys });
logger.debug('Successfully updated keys #%s', deliveryTag);
} catch (error) {
logger.error('Failed to updated keys #%s', deliveryTag);
await onError(error);
}
}
function onEnd() {
if (endWasEmitted) {
logger.warn({
messagesCount: that.messagesCount,
errorCount: taskExec.errorCount,
messageProcessingTime: Date.now() - timeStart
}, 'processMessage emit end was called more than once');
return;
}
endWasEmitted = true;
if (taskExec.errorCount > 0) {
that.amqpConnection.reject(message);
} else {
that.amqpConnection.ack(message);
}
that.messagesCount -= 1;
logger.trace({
messagesCount: that.messagesCount,
errorCount: taskExec.errorCount,
messageProcessingTime: Date.now() - timeStart
}, 'processMessage emit end');
resolve();
}
});
function formatError(err) {
if (err instanceof Error || (_.isObject(err) && _.has(err, 'message'))) {
return {
message: err.message,
stack: err.stack || 'Not Available',
name: err.name || 'Error'
};
} else {
return {
message: err || 'Not Available',
stack: 'Not Available',
name: 'Error'
};
}
}
}
async processMessage(payload, message) {
//eslint-disable-next-line consistent-this
const self = this;
const settings = this.settings;
const incomingMessageHeaders = this.readIncomingMessageHeaders(message);
self.messagesCount += 1;
const timeStart = Date.now();
const { deliveryTag } = message.fields;
const logger = log.child({
threadId: incomingMessageHeaders.threadId || 'unknown',
messageId: incomingMessageHeaders.messageId || 'unknown',
parentMessageId: incomingMessageHeaders.parentMessageId || 'unknown',
deliveryTag
});
logger.trace({ messagesCount: this.messagesCount }, 'processMessage received');
const stepData = this.stepData;
log.debug('Trigger or action: %s', settings.FUNCTION);
const outgoingMessageId = uuid.v4();
const outgoingMessageHeaders = {
...incomingMessageHeaders,
...getAdditionalHeadersFromSettings(settings),
parentMessageId: incomingMessageHeaders.messageId,
threadId: incomingMessageHeaders.threadId,
messageId: outgoingMessageId,
execId: settings.EXEC_ID,
taskId: settings.FLOW_ID,
workspaceId: settings.WORKSPACE_ID,
containerId: settings.CONTAINER_ID,
userId: settings.USER_ID,
stepId: settings.STEP_ID,
compId: settings.COMP_ID,
function: settings.FUNCTION,
start: new Date().getTime()
};
let module;
try {
module = await this.componentReader.loadTriggerOrAction(settings.FUNCTION);
} catch (e) {
log.error(e);
outgoingMessageHeaders.end = new Date().getTime();
self.amqpConnection.sendError(e, outgoingMessageHeaders, message);
self.amqpConnection.reject(message);
return;
}
const method = this.componentReader.findTriggerOrActionDefinition(settings.FUNCTION);
if (method.autoResolveObjectReferences) {
const { passthrough } = payload;
try {
await Promise.all([
(async () => {
logger.trace('Going to check if incoming message body is lightweight.');
payload.body = await this.fetchMessageBody(payload, logger);
})(),
...(passthrough
? Object.keys(passthrough).map(async stepId => {
logger.trace('Going to check if passthrough for step is lightweight.', { stepId });
payload.passthrough[stepId].body = await this.fetchMessageBody(
payload.passthrough[stepId],
logger
);
})
: [])
]);
} catch (e) {
logger.error(e);
outgoingMessageHeaders.end = new Date().getTime();
self.amqpConnection.sendError(e, outgoingMessageHeaders, message);
self.amqpConnection.reject(message);
return;
}
}
await this.runExec(module, payload, message, outgoingMessageHeaders, stepData, timeStart, logger);
}
}
exports.Sailor = Sailor;