mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
🔀 Merge branch 'AMQP-Connection-Parameters' of https://github.com/erbg/n8n into erbg-AMQP-Connection-Parameters
This commit is contained in:
commit
64e36f04b0
|
@ -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 { IExecuteFunctions } from 'n8n-core';
|
||||||
import {
|
import {
|
||||||
|
@ -64,6 +65,27 @@ export class Amqp implements INodeType {
|
||||||
default: '',
|
default: '',
|
||||||
description: 'The only property to send. If empty the whole item will be sent.',
|
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 sink = this.getNodeParameter('sink', 0, '') as string;
|
||||||
const applicationProperties = this.getNodeParameter('headerParametersJson', 0, {}) as string | object;
|
const applicationProperties = this.getNodeParameter('headerParametersJson', 0, {}) as string | object;
|
||||||
const options = this.getNodeParameter('options', 0, {}) as IDataObject;
|
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 !== '') {
|
if (typeof applicationProperties === 'string' && applicationProperties !== '') {
|
||||||
headerProperties = JSON.parse(applicationProperties);
|
headerProperties = JSON.parse(applicationProperties);
|
||||||
}
|
} else {
|
||||||
|
headerProperties = applicationProperties as object;
|
||||||
|
}
|
||||||
|
|
||||||
if (sink === '') {
|
if (sink === '') {
|
||||||
throw new Error('Queue or Topic required!');
|
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 = {
|
const connectOptions: ContainerOptions = {
|
||||||
host: credentials.hostname,
|
host: credentials.hostname,
|
||||||
hostname: credentials.hostname,
|
hostname: credentials.hostname,
|
||||||
port: credentials.port,
|
port: credentials.port,
|
||||||
reconnect: true, // this id the default anyway
|
reconnect: containerReconnect,
|
||||||
reconnect_limit: 50, // try for max 50 times, based on a back-off algorithm
|
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 conn = container.connect(connectOptions);
|
||||||
|
|
||||||
const sender = conn.open_sender(sink);
|
const sender = conn.open_sender(sink);
|
||||||
|
|
||||||
const responseData: IDataObject[] = await new Promise((resolve) => {
|
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 returnData = [];
|
||||||
|
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
|
@ -129,12 +155,12 @@ export class Amqp implements INodeType {
|
||||||
body = JSON.stringify(body);
|
body = JSON.stringify(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = context.sender.send({
|
const result = context.sender?.send({
|
||||||
application_properties: headerProperties,
|
application_properties: headerProperties,
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
|
|
||||||
returnData.push({ id: result.id });
|
returnData.push({ id: result?.id });
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(returnData);
|
resolve(returnData);
|
||||||
|
|
|
@ -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 { ITriggerFunctions } from 'n8n-core';
|
||||||
import {
|
import {
|
||||||
|
@ -55,6 +56,20 @@ export class AmqpTrigger implements INodeType {
|
||||||
description: 'Leave empty for non-durable topic subscriptions or queues',
|
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',
|
displayName: 'Options',
|
||||||
name: 'options',
|
name: 'options',
|
||||||
type: 'collection',
|
type: 'collection',
|
||||||
|
@ -96,9 +111,30 @@ export class AmqpTrigger implements INodeType {
|
||||||
default: 10,
|
default: 10,
|
||||||
description: 'Milliseconds to sleep after every cicle.',
|
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 clientname = this.getNodeParameter('clientname', '') as string;
|
||||||
const subscription = this.getNodeParameter('subscription', '') as string;
|
const subscription = this.getNodeParameter('subscription', '') as string;
|
||||||
const options = this.getNodeParameter('options', {}) as IDataObject;
|
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 === '') {
|
if (sink === '') {
|
||||||
throw new Error('Queue or Topic required!');
|
throw new Error('Queue or Topic required!');
|
||||||
|
@ -125,34 +164,21 @@ export class AmqpTrigger implements INodeType {
|
||||||
durable = true;
|
durable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = require('rhea');
|
const container = rhea.create_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
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
let lastMsgId: number | undefined = undefined;
|
let lastMsgId: string| number | Buffer | undefined = undefined;
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
container.on('receiver_open', (context: any) => { // tslint:disable-line:no-any
|
container.on('receiver_open', (context: EventContext) => {
|
||||||
context.receiver.add_credit(pullMessagesNumber);
|
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
|
// 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) {
|
if (context.message.message_id && context.message.message_id === lastMsgId) {
|
||||||
return;
|
return;
|
||||||
|
@ -160,6 +186,12 @@ export class AmqpTrigger implements INodeType {
|
||||||
lastMsgId = context.message.message_id;
|
lastMsgId = context.message.message_id;
|
||||||
|
|
||||||
let data = context.message;
|
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) {
|
if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
|
||||||
// The buffer is not ready... Stringify and parse back to load it.
|
// The buffer is not ready... Stringify and parse back to load it.
|
||||||
|
@ -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(() => {
|
setTimeout(() => {
|
||||||
context.receiver.add_credit(pullMessagesNumber);
|
context.receiver?.add_credit(pullMessagesNumber);
|
||||||
}, options.sleepTime as number || 10);
|
}, 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);
|
const connection = container.connect(connectOptions);
|
||||||
let clientOptions = undefined;
|
|
||||||
if (durable) {
|
let clientOptions : ReceiverOptions = {
|
||||||
clientOptions = {
|
name: subscription ? subscription : undefined,
|
||||||
name: subscription,
|
source: {
|
||||||
source: {
|
address: sink,
|
||||||
address: sink,
|
durable: (durable ? 2 : undefined),
|
||||||
durable: 2,
|
expiry_policy: (durable ? 'never' : undefined),
|
||||||
expiry_policy: 'never',
|
},
|
||||||
},
|
credit_window: 0, // prefetch 1
|
||||||
credit_window: 0, // prefetch 1
|
};
|
||||||
};
|
|
||||||
} else {
|
|
||||||
clientOptions = {
|
|
||||||
source: {
|
|
||||||
address: sink,
|
|
||||||
},
|
|
||||||
credit_window: 0, // prefetch 1
|
|
||||||
};
|
|
||||||
}
|
|
||||||
connection.open_receiver(clientOptions);
|
connection.open_receiver(clientOptions);
|
||||||
|
|
||||||
|
|
||||||
|
@ -220,7 +258,7 @@ export class AmqpTrigger implements INodeType {
|
||||||
container.removeAllListeners('message');
|
container.removeAllListeners('message');
|
||||||
connection.close();
|
connection.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The "manualTriggerFunction" function gets called by n8n
|
// The "manualTriggerFunction" function gets called by n8n
|
||||||
// when a user is in the workflow editor and starts the
|
// when a user is in the workflow editor and starts the
|
||||||
// workflow manually.
|
// workflow manually.
|
||||||
|
@ -230,15 +268,16 @@ export class AmqpTrigger implements INodeType {
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
const timeoutHandler = setTimeout(() => {
|
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.'));
|
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);
|
}, 3000);
|
||||||
container.on('message', (context: any) => { // tslint:disable-line:no-any
|
container.on('message', (context: EventContext) => {
|
||||||
// Check if the only property present in the message is body
|
// Check if the only property present in the message is body
|
||||||
// in which case we only emit the content of the body property
|
// in which case we only emit the content of the body property
|
||||||
// otherwise we emit all properties and their content
|
// otherwise we emit all properties and their content
|
||||||
if (Object.keys(context.message)[0] === 'body' && Object.keys(context.message).length === 1) {
|
const message = context.message as Message;
|
||||||
self.emit([self.helpers.returnJsonArray([context.message.body])]);
|
if (Object.keys(message)[0] === 'body' && Object.keys(message).length === 1) {
|
||||||
|
self.emit([self.helpers.returnJsonArray([message.body])]);
|
||||||
} else {
|
} else {
|
||||||
self.emit([self.helpers.returnJsonArray([context.message])]);
|
self.emit([self.helpers.returnJsonArray([message as any])]);
|
||||||
}
|
}
|
||||||
clearTimeout(timeoutHandler);
|
clearTimeout(timeoutHandler);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
|
Loading…
Reference in a new issue