mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Some improvements on AMQP nodes
This commit is contained in:
parent
64e36f04b0
commit
8432a8774e
|
@ -1,5 +1,9 @@
|
||||||
import { ContainerOptions, Dictionary, EventContext } from 'rhea';
|
import {
|
||||||
import rhea = require('rhea');
|
create_container,
|
||||||
|
ContainerOptions,
|
||||||
|
Dictionary,
|
||||||
|
EventContext,
|
||||||
|
} from 'rhea';
|
||||||
|
|
||||||
import { IExecuteFunctions } from 'n8n-core';
|
import { IExecuteFunctions } from 'n8n-core';
|
||||||
import {
|
import {
|
||||||
|
@ -51,6 +55,13 @@ export class Amqp implements INodeType {
|
||||||
placeholder: 'Add Option',
|
placeholder: 'Add Option',
|
||||||
default: {},
|
default: {},
|
||||||
options: [
|
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',
|
displayName: 'Data as Object',
|
||||||
name: 'dataAsObject',
|
name: 'dataAsObject',
|
||||||
|
@ -58,6 +69,20 @@ export class Amqp implements INodeType {
|
||||||
default: false,
|
default: false,
|
||||||
description: 'Send the data as an object.',
|
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',
|
displayName: 'Send property',
|
||||||
name: 'sendOnlyProperty',
|
name: 'sendOnlyProperty',
|
||||||
|
@ -65,27 +90,6 @@ 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',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -100,11 +104,11 @@ 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 containerId = options.containerId as string;
|
||||||
const containerReconnect = options.reconnect as boolean || true;
|
const containerReconnect = options.reconnect as boolean || true;
|
||||||
const containerReconnectLimit = options.reconnectLimit as number || 50;
|
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 !== '') {
|
if (typeof applicationProperties === 'string' && applicationProperties !== '') {
|
||||||
headerProperties = JSON.parse(applicationProperties);
|
headerProperties = JSON.parse(applicationProperties);
|
||||||
} else {
|
} else {
|
||||||
|
@ -115,7 +119,7 @@ export class Amqp implements INodeType {
|
||||||
throw new Error('Queue or Topic required!');
|
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
|
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,
|
username: credentials.username ? credentials.username : undefined,
|
||||||
password: credentials.password ? credentials.password : undefined,
|
password: credentials.password ? credentials.password : undefined,
|
||||||
transport: credentials.transportType ? credentials.transportType : undefined,
|
transport: credentials.transportType ? credentials.transportType : undefined,
|
||||||
container_id: container_id ? container_id : undefined,
|
container_id: containerId ? containerId : undefined,
|
||||||
id: container_id ? container_id : undefined,
|
id: containerId ? containerId : undefined,
|
||||||
};
|
};
|
||||||
const conn = container.connect(connectOptions);
|
const conn = container.connect(connectOptions);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
import { ContainerOptions, EventContext, Message, ReceiverOptions } from 'rhea';
|
import {
|
||||||
import rhea = require("rhea");
|
create_container,
|
||||||
|
ContainerOptions,
|
||||||
|
EventContext,
|
||||||
|
Message,
|
||||||
|
ReceiverOptions,
|
||||||
|
} from 'rhea';
|
||||||
|
|
||||||
import { ITriggerFunctions } from 'n8n-core';
|
import { ITriggerFunctions } from 'n8n-core';
|
||||||
import {
|
import {
|
||||||
|
@ -55,20 +60,6 @@ export class AmqpTrigger implements INodeType {
|
||||||
placeholder: 'for durable/persistent topic subscriptions, example: "order-worker"',
|
placeholder: 'for durable/persistent topic subscriptions, example: "order-worker"',
|
||||||
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',
|
||||||
|
@ -76,6 +67,13 @@ export class AmqpTrigger implements INodeType {
|
||||||
placeholder: 'Add Option',
|
placeholder: 'Add Option',
|
||||||
default: {},
|
default: {},
|
||||||
options: [
|
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',
|
displayName: 'Convert Body To String',
|
||||||
name: 'jsonConvertByteArrayToString',
|
name: 'jsonConvertByteArrayToString',
|
||||||
|
@ -90,6 +88,13 @@ export class AmqpTrigger implements INodeType {
|
||||||
default: false,
|
default: false,
|
||||||
description: 'Parse the body to an object.',
|
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',
|
displayName: 'Only Body',
|
||||||
name: 'onlyBody',
|
name: 'onlyBody',
|
||||||
|
@ -98,11 +103,18 @@ export class AmqpTrigger implements INodeType {
|
||||||
description: 'Returns only the body property.',
|
description: 'Returns only the body property.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Messages per Cicle',
|
displayName: 'Reconnect',
|
||||||
name: 'pullMessagesNumber',
|
name: 'reconnect',
|
||||||
|
type: 'boolean',
|
||||||
|
default: true,
|
||||||
|
description: 'Automatically reconnect if disconnected',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Reconnect Limit',
|
||||||
|
name: 'reconnectLimit',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
default: 100,
|
default: 50,
|
||||||
description: 'Number of messages to pull from the bus for every cicle',
|
description: 'Maximum number of reconnect attempts',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Sleep Time',
|
displayName: 'Sleep Time',
|
||||||
|
@ -111,30 +123,9 @@ 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',
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -150,7 +141,7 @@ export class AmqpTrigger implements INodeType {
|
||||||
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 as number || 100;
|
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 containerReconnect = options.reconnect as boolean || true;
|
||||||
const containerReconnectLimit = options.reconnectLimit as number || 50;
|
const containerReconnectLimit = options.reconnectLimit as number || 50;
|
||||||
|
|
||||||
|
@ -164,7 +155,7 @@ export class AmqpTrigger implements INodeType {
|
||||||
durable = true;
|
durable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = rhea.create_container();
|
const container = create_container();
|
||||||
|
|
||||||
let lastMsgId: string | number | Buffer | undefined = undefined;
|
let lastMsgId: string | number | Buffer | undefined = undefined;
|
||||||
const self = this;
|
const self = this;
|
||||||
|
@ -176,8 +167,9 @@ export class AmqpTrigger implements INodeType {
|
||||||
container.on('message', (context: EventContext) => {
|
container.on('message', (context: EventContext) => {
|
||||||
|
|
||||||
// No message in the context
|
// No message in the context
|
||||||
if(!context.message)
|
if (!context.message) {
|
||||||
return;
|
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) {
|
||||||
|
@ -189,7 +181,7 @@ export class AmqpTrigger implements INodeType {
|
||||||
|
|
||||||
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.
|
||||||
let cont = JSON.stringify(data.body.content);
|
const cont = JSON.stringify(data.body.content);
|
||||||
data.body = String.fromCharCode.apply(null, JSON.parse(cont).data);
|
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()) {
|
if (!context.receiver?.has_credit()) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -234,12 +226,12 @@ export class AmqpTrigger implements INodeType {
|
||||||
username: credentials.username ? credentials.username : undefined,
|
username: credentials.username ? credentials.username : undefined,
|
||||||
password: credentials.password ? credentials.password : undefined,
|
password: credentials.password ? credentials.password : undefined,
|
||||||
transport: credentials.transportType ? credentials.transportType : undefined,
|
transport: credentials.transportType ? credentials.transportType : undefined,
|
||||||
container_id: container_id ? container_id : undefined,
|
container_id: containerId ? containerId : undefined,
|
||||||
id: container_id ? container_id : undefined,
|
id: containerId ? containerId : undefined,
|
||||||
};
|
};
|
||||||
const connection = container.connect(connectOptions);
|
const connection = container.connect(connectOptions);
|
||||||
|
|
||||||
let clientOptions : ReceiverOptions = {
|
const clientOptions: ReceiverOptions = {
|
||||||
name: subscription ? subscription : undefined,
|
name: subscription ? subscription : undefined,
|
||||||
source: {
|
source: {
|
||||||
address: sink,
|
address: sink,
|
||||||
|
@ -268,7 +260,7 @@ 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.'));
|
||||||
}, 3000);
|
}, 30000);
|
||||||
container.on('message', (context: EventContext) => {
|
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
|
||||||
|
@ -277,7 +269,7 @@ export class AmqpTrigger implements INodeType {
|
||||||
if (Object.keys(message)[0] === 'body' && Object.keys(message).length === 1) {
|
if (Object.keys(message)[0] === 'body' && Object.keys(message).length === 1) {
|
||||||
self.emit([self.helpers.returnJsonArray([message.body])]);
|
self.emit([self.helpers.returnJsonArray([message.body])]);
|
||||||
} else {
|
} else {
|
||||||
self.emit([self.helpers.returnJsonArray([message as any])]);
|
self.emit([self.helpers.returnJsonArray([message as any])]); // tslint:disable-line:no-any
|
||||||
}
|
}
|
||||||
clearTimeout(timeoutHandler);
|
clearTimeout(timeoutHandler);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
|
Loading…
Reference in a new issue