🔀 Merge branch 'AMQP-Connection-Parameters' of https://github.com/erbg/n8n into erbg-AMQP-Connection-Parameters

This commit is contained in:
Jan Oberhauser 2021-01-20 12:41:49 +01:00
commit 64e36f04b0
2 changed files with 138 additions and 73 deletions

View file

@ -1,4 +1,5 @@
import { ContainerOptions, Delivery } from 'rhea';
import { ContainerOptions, Dictionary, EventContext } from 'rhea';
import rhea = require('rhea');
import { IExecuteFunctions } from 'n8n-core';
import {
@ -64,6 +65,27 @@ export class Amqp implements INodeType {
default: '',
description: 'The only property to send. If empty the whole item will be sent.',
},
{
displayName: 'Container ID',
name: 'containerID',
type: 'string',
default: '',
description: 'Will be used to pass to the RHEA Backend as container_id',
},
{
displayName: 'Reconnect',
name: 'reconnect',
type: 'boolean',
default: true,
description: 'If on, the library will automatically attempt to reconnect if disconnected',
},
{
displayName: 'Reconnect limit',
name: 'reconnectLimit',
type: 'number',
default: 50,
description: 'maximum number of reconnect attempts',
},
],
},
],
@ -78,40 +100,44 @@ export class Amqp implements INodeType {
const sink = this.getNodeParameter('sink', 0, '') as string;
const applicationProperties = this.getNodeParameter('headerParametersJson', 0, {}) as string | object;
const options = this.getNodeParameter('options', 0, {}) as IDataObject;
const container_id = options.containerID as string;
const containerReconnect = options.reconnect as boolean || true ;
const containerReconnectLimit = options.reconnectLimit as number || 50;
let headerProperties = applicationProperties;
let headerProperties : Dictionary<any>;
if (typeof applicationProperties === 'string' && applicationProperties !== '') {
headerProperties = JSON.parse(applicationProperties);
} else {
headerProperties = applicationProperties as object;
}
if (sink === '') {
throw new Error('Queue or Topic required!');
}
const container = require('rhea');
const container = rhea.create_container();
/*
Values are documentet here: https://github.com/amqp/rhea#container
*/
const connectOptions: ContainerOptions = {
host: credentials.hostname,
hostname: credentials.hostname,
port: credentials.port,
reconnect: true, // this id the default anyway
reconnect_limit: 50, // try for max 50 times, based on a back-off algorithm
reconnect: containerReconnect,
reconnect_limit: containerReconnectLimit,
username: credentials.username ? credentials.username : undefined,
password: credentials.password ? credentials.password : undefined,
transport: credentials.transportType ? credentials.transportType : undefined,
container_id: container_id ? container_id : undefined,
id: container_id ? container_id : undefined,
};
if (credentials.username || credentials.password) {
container.options.username = credentials.username;
container.options.password = credentials.password;
connectOptions.username = credentials.username;
connectOptions.password = credentials.password;
}
if (credentials.transportType !== '') {
connectOptions.transport = credentials.transportType;
}
const conn = container.connect(connectOptions);
const sender = conn.open_sender(sink);
const responseData: IDataObject[] = await new Promise((resolve) => {
container.once('sendable', (context: any) => { // tslint:disable-line:no-any
container.once('sendable', (context: EventContext) => {
const returnData = [];
const items = this.getInputData();
@ -129,12 +155,12 @@ export class Amqp implements INodeType {
body = JSON.stringify(body);
}
const result = context.sender.send({
const result = context.sender?.send({
application_properties: headerProperties,
body,
});
returnData.push({ id: result.id });
returnData.push({ id: result?.id });
}
resolve(returnData);

View file

@ -1,4 +1,5 @@
import { ContainerOptions } from 'rhea';
import { ContainerOptions, EventContext, Message, ReceiverOptions } from 'rhea';
import rhea = require("rhea");
import { ITriggerFunctions } from 'n8n-core';
import {
@ -54,6 +55,20 @@ export class AmqpTrigger implements INodeType {
placeholder: 'for durable/persistent topic subscriptions, example: "order-worker"',
description: 'Leave empty for non-durable topic subscriptions or queues',
},
{
displayName: 'Pull N Messages per Cicle',
name: 'pullMessagesNumber',
type: 'number',
default: 100,
description: 'Number of messages to pull from the bus for every cicle',
},
{
displayName: 'Sleep time after cicle',
name: 'sleepTime',
type: 'number',
default: 10,
description: 'Milliseconds to sleep after every cicle',
},
{
displayName: 'Options',
name: 'options',
@ -96,9 +111,30 @@ export class AmqpTrigger implements INodeType {
default: 10,
description: 'Milliseconds to sleep after every cicle.',
},
],
{
displayName: 'Container ID',
name: 'containerID',
type: 'string',
default: '',
description: 'Will be used to pass to the RHEA Backend as container_id',
},
{
displayName: 'Reconnect',
name: 'reconnect',
type: 'boolean',
default: true,
description: 'If on, the library will automatically attempt to reconnect if disconnected',
},
{
displayName: 'Reconnect limit',
name: 'reconnectLimit',
type: 'number',
default: 50,
description: 'maximum number of reconnect attempts',
},
],
},
]
};
@ -113,7 +149,10 @@ export class AmqpTrigger implements INodeType {
const clientname = this.getNodeParameter('clientname', '') as string;
const subscription = this.getNodeParameter('subscription', '') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
const pullMessagesNumber = options.pullMessagesNumber || 100;
const pullMessagesNumber = options.pullMessagesNumber as number || 100;
const container_id = options.containerID as string;
const containerReconnect = options.reconnect as boolean || true ;
const containerReconnectLimit = options.reconnectLimit as number || 50;
if (sink === '') {
throw new Error('Queue or Topic required!');
@ -125,34 +164,21 @@ export class AmqpTrigger implements INodeType {
durable = true;
}
const container = require('rhea');
const connectOptions: ContainerOptions = {
host: credentials.hostname,
hostname: credentials.hostname,
port: credentials.port,
reconnect: true, // this id the default anyway
reconnect_limit: 50, // try for max 50 times, based on a back-off algorithm
container_id: (durable ? clientname : null),
};
if (credentials.username || credentials.password) {
// Old rhea implementation. not shure if it is neccessary
container.options.username = credentials.username;
container.options.password = credentials.password;
connectOptions.username = credentials.username;
connectOptions.password = credentials.password;
}
if (credentials.transportType) {
connectOptions.transport = credentials.transportType;
}
const container = rhea.create_container();
let lastMsgId: number | undefined = undefined;
let lastMsgId: string| number | Buffer | undefined = undefined;
const self = this;
container.on('receiver_open', (context: any) => { // tslint:disable-line:no-any
context.receiver.add_credit(pullMessagesNumber);
container.on('receiver_open', (context: EventContext) => {
context.receiver?.add_credit(pullMessagesNumber);
});
container.on('message', (context: any) => { // tslint:disable-line:no-any
container.on('message', (context: EventContext) => {
// No message in the context
if(!context.message)
return;
// ignore duplicate message check, don't think it's necessary, but it was in the rhea-lib example code
if (context.message.message_id && context.message.message_id === lastMsgId) {
return;
@ -161,6 +187,12 @@ export class AmqpTrigger implements INodeType {
let data = context.message;
if(options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
// The buffer is not ready... Stringify and parse back to load it.
let cont = JSON.stringify(data.body.content);
data.body = String.fromCharCode.apply(null,JSON.parse(cont).data);
}
if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
// The buffer is not ready... Stringify and parse back to load it.
const cont = JSON.stringify(data.body.content);
@ -181,35 +213,41 @@ export class AmqpTrigger implements INodeType {
}
self.emit([self.helpers.returnJsonArray([data])]);
self.emit([self.helpers.returnJsonArray([data as any])]);
if (context.receiver.credit === 0) {
if (!context.receiver?.has_credit()) {
setTimeout(() => {
context.receiver.add_credit(pullMessagesNumber);
context.receiver?.add_credit(pullMessagesNumber);
}, options.sleepTime as number || 10);
}
});
/*
Values are documentet here: https://github.com/amqp/rhea#container
*/
const connectOptions: ContainerOptions = {
host: credentials.hostname,
hostname: credentials.hostname,
port: credentials.port,
reconnect: containerReconnect,
reconnect_limit: containerReconnectLimit,
username: credentials.username ? credentials.username : undefined,
password: credentials.password ? credentials.password : undefined,
transport: credentials.transportType ? credentials.transportType : undefined,
container_id: container_id ? container_id : undefined,
id: container_id ? container_id : undefined,
};
const connection = container.connect(connectOptions);
let clientOptions = undefined;
if (durable) {
clientOptions = {
name: subscription,
let clientOptions : ReceiverOptions = {
name: subscription ? subscription : undefined,
source: {
address: sink,
durable: 2,
expiry_policy: 'never',
durable: (durable ? 2 : undefined),
expiry_policy: (durable ? 'never' : undefined),
},
credit_window: 0, // prefetch 1
};
} else {
clientOptions = {
source: {
address: sink,
},
credit_window: 0, // prefetch 1
};
}
connection.open_receiver(clientOptions);
@ -230,15 +268,16 @@ export class AmqpTrigger implements INodeType {
await new Promise((resolve, reject) => {
const timeoutHandler = setTimeout(() => {
reject(new Error('Aborted, no message received within 30secs. This 30sec timeout is only set for "manually triggered execution". Active Workflows will listen indefinitely.'));
}, 30000);
container.on('message', (context: any) => { // tslint:disable-line:no-any
}, 3000);
container.on('message', (context: EventContext) => {
// Check if the only property present in the message is body
// in which case we only emit the content of the body property
// otherwise we emit all properties and their content
if (Object.keys(context.message)[0] === 'body' && Object.keys(context.message).length === 1) {
self.emit([self.helpers.returnJsonArray([context.message.body])]);
const message = context.message as Message;
if (Object.keys(message)[0] === 'body' && Object.keys(message).length === 1) {
self.emit([self.helpers.returnJsonArray([message.body])]);
} else {
self.emit([self.helpers.returnJsonArray([context.message])]);
self.emit([self.helpers.returnJsonArray([message as any])]);
}
clearTimeout(timeoutHandler);
resolve(true);