Some improvements on AMQP nodes

This commit is contained in:
Jan Oberhauser 2021-01-20 13:20:48 +01:00
parent 64e36f04b0
commit 8432a8774e
2 changed files with 92 additions and 96 deletions

View file

@ -1,5 +1,9 @@
import { ContainerOptions, Dictionary, EventContext } from 'rhea';
import rhea = require('rhea');
import {
create_container,
ContainerOptions,
Dictionary,
EventContext,
} from 'rhea';
import { IExecuteFunctions } from 'n8n-core';
import {
@ -51,6 +55,13 @@ export class Amqp implements INodeType {
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Container ID',
name: 'containerId',
type: 'string',
default: '',
description: 'Will be used to pass to the RHEA Backend as container_id',
},
{
displayName: 'Data as Object',
name: 'dataAsObject',
@ -58,6 +69,20 @@ export class Amqp implements INodeType {
default: false,
description: 'Send the data as an object.',
},
{
displayName: 'Reconnect',
name: 'reconnect',
type: 'boolean',
default: true,
description: 'Automatically reconnect if disconnected',
},
{
displayName: 'Reconnect Limit',
name: 'reconnectLimit',
type: 'number',
default: 50,
description: 'Maximum number of reconnect attempts',
},
{
displayName: 'Send property',
name: 'sendOnlyProperty',
@ -65,27 +90,6 @@ 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',
},
],
},
],
@ -100,11 +104,11 @@ 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 containerId = options.containerId as string;
const containerReconnect = options.reconnect as boolean || true;
const containerReconnectLimit = options.reconnectLimit as number || 50;
let headerProperties : Dictionary<any>;
let headerProperties: Dictionary<any>; // tslint:disable-line:no-any
if (typeof applicationProperties === 'string' && applicationProperties !== '') {
headerProperties = JSON.parse(applicationProperties);
} else {
@ -115,7 +119,7 @@ export class Amqp implements INodeType {
throw new Error('Queue or Topic required!');
}
const container = rhea.create_container();
const container = create_container();
/*
Values are documentet here: https://github.com/amqp/rhea#container
@ -129,8 +133,8 @@ export class Amqp implements INodeType {
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,
container_id: containerId ? containerId : undefined,
id: containerId ? containerId : undefined,
};
const conn = container.connect(connectOptions);

View file

@ -1,5 +1,10 @@
import { ContainerOptions, EventContext, Message, ReceiverOptions } from 'rhea';
import rhea = require("rhea");
import {
create_container,
ContainerOptions,
EventContext,
Message,
ReceiverOptions,
} from 'rhea';
import { ITriggerFunctions } from 'n8n-core';
import {
@ -55,20 +60,6 @@ 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',
@ -76,6 +67,13 @@ export class AmqpTrigger implements INodeType {
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Container ID',
name: 'containerId',
type: 'string',
default: '',
description: 'Will be used to pass to the RHEA Backend as container_id',
},
{
displayName: 'Convert Body To String',
name: 'jsonConvertByteArrayToString',
@ -90,6 +88,13 @@ export class AmqpTrigger implements INodeType {
default: false,
description: 'Parse the body to an object.',
},
{
displayName: 'Messages per Cicle',
name: 'pullMessagesNumber',
type: 'number',
default: 100,
description: 'Number of messages to pull from the bus for every cicle',
},
{
displayName: 'Only Body',
name: 'onlyBody',
@ -98,11 +103,18 @@ export class AmqpTrigger implements INodeType {
description: 'Returns only the body property.',
},
{
displayName: 'Messages per Cicle',
name: 'pullMessagesNumber',
displayName: 'Reconnect',
name: 'reconnect',
type: 'boolean',
default: true,
description: 'Automatically reconnect if disconnected',
},
{
displayName: 'Reconnect Limit',
name: 'reconnectLimit',
type: 'number',
default: 100,
description: 'Number of messages to pull from the bus for every cicle',
default: 50,
description: 'Maximum number of reconnect attempts',
},
{
displayName: 'Sleep Time',
@ -111,30 +123,9 @@ 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',
},
],
},
]
],
};
@ -150,7 +141,7 @@ export class AmqpTrigger implements INodeType {
const subscription = this.getNodeParameter('subscription', '') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
const pullMessagesNumber = options.pullMessagesNumber as number || 100;
const container_id = options.containerID as string;
const containerId = options.containerId as string;
const containerReconnect = options.reconnect as boolean || true;
const containerReconnectLimit = options.reconnectLimit as number || 50;
@ -164,7 +155,7 @@ export class AmqpTrigger implements INodeType {
durable = true;
}
const container = rhea.create_container();
const container = create_container();
let lastMsgId: string | number | Buffer | undefined = undefined;
const self = this;
@ -176,8 +167,9 @@ export class AmqpTrigger implements INodeType {
container.on('message', (context: EventContext) => {
// No message in the context
if(!context.message)
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) {
@ -189,7 +181,7 @@ export class AmqpTrigger implements INodeType {
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);
const cont = JSON.stringify(data.body.content);
data.body = String.fromCharCode.apply(null, JSON.parse(cont).data);
}
@ -213,7 +205,7 @@ export class AmqpTrigger implements INodeType {
}
self.emit([self.helpers.returnJsonArray([data as any])]);
self.emit([self.helpers.returnJsonArray([data as any])]); // tslint:disable-line:no-any
if (!context.receiver?.has_credit()) {
setTimeout(() => {
@ -234,12 +226,12 @@ export class AmqpTrigger implements INodeType {
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,
container_id: containerId ? containerId : undefined,
id: containerId ? containerId : undefined,
};
const connection = container.connect(connectOptions);
let clientOptions : ReceiverOptions = {
const clientOptions: ReceiverOptions = {
name: subscription ? subscription : undefined,
source: {
address: sink,
@ -268,7 +260,7 @@ 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.'));
}, 3000);
}, 30000);
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
@ -277,7 +269,7 @@ export class AmqpTrigger implements INodeType {
if (Object.keys(message)[0] === 'body' && Object.keys(message).length === 1) {
self.emit([self.helpers.returnJsonArray([message.body])]);
} else {
self.emit([self.helpers.returnJsonArray([message as any])]);
self.emit([self.helpers.returnJsonArray([message as any])]); // tslint:disable-line:no-any
}
clearTimeout(timeoutHandler);
resolve(true);