2022-07-04 02:12:08 -07:00
|
|
|
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
2020-12-22 23:05:02 -08:00
|
|
|
import {
|
2022-05-30 03:16:44 -07:00
|
|
|
createDeferredPromise,
|
2020-12-22 23:05:02 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeProperties,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2022-05-30 03:16:44 -07:00
|
|
|
IRun,
|
2020-12-22 23:05:02 -08:00
|
|
|
ITriggerFunctions,
|
|
|
|
ITriggerResponse,
|
2022-05-30 03:16:44 -07:00
|
|
|
LoggerProxy as Logger,
|
2022-07-04 02:12:08 -07:00
|
|
|
NodeOperationError,
|
2020-12-22 23:05:02 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
rabbitDefaultOptions,
|
|
|
|
} from './DefaultOptions';
|
|
|
|
|
|
|
|
import {
|
2022-05-30 03:16:44 -07:00
|
|
|
MessageTracker,
|
2021-01-12 23:57:06 -08:00
|
|
|
rabbitmqConnectQueue,
|
2020-12-22 23:05:02 -08:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-05-30 03:16:44 -07:00
|
|
|
import * as amqplib from 'amqplib';
|
|
|
|
|
2020-12-22 23:05:02 -08:00
|
|
|
export class RabbitMQTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'RabbitMQ Trigger',
|
|
|
|
name: 'rabbitmqTrigger',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-12-22 23:05:02 -08:00
|
|
|
icon: 'file:rabbitmq.png',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Listens to RabbitMQ messages',
|
|
|
|
defaults: {
|
2021-11-30 15:18:57 -08:00
|
|
|
name: 'RabbitMQ Trigger',
|
2020-12-22 23:05:02 -08:00
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'rabbitmq',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Queue / Topic',
|
|
|
|
name: 'queue',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'queue-name',
|
2022-05-30 03:16:44 -07:00
|
|
|
description: 'The name of the queue to read from',
|
2020-12-22 23:05:02 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
default: {},
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
options: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
displayName: 'Content Is Binary',
|
2020-12-22 23:05:02 -08:00
|
|
|
name: 'contentIsBinary',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
description: 'Whether to save the content as binary',
|
2020-12-22 23:05:02 -08:00
|
|
|
},
|
2022-05-30 03:16:44 -07:00
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
displayName: 'Delete From Queue When',
|
2022-05-30 03:16:44 -07:00
|
|
|
name: 'acknowledge',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Execution Finishes',
|
2022-05-30 03:16:44 -07:00
|
|
|
value: 'executionFinishes',
|
|
|
|
description: 'After the workflow execution finished. No matter if the execution was successful or not.',
|
|
|
|
},
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Execution Finishes Successfully',
|
2022-05-30 03:16:44 -07:00
|
|
|
value: 'executionFinishesSuccessfully',
|
|
|
|
description: 'After the workflow execution finished successfully',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Immediately',
|
|
|
|
value: 'immediately',
|
|
|
|
description: 'As soon as the message got received',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'immediately',
|
|
|
|
description: 'When to acknowledge the message',
|
|
|
|
},
|
2020-12-22 23:05:02 -08:00
|
|
|
{
|
|
|
|
displayName: 'JSON Parse Body',
|
|
|
|
name: 'jsonParseBody',
|
|
|
|
type: 'boolean',
|
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
|
|
|
contentIsBinary: [
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
description: 'Whether to parse the body to an object',
|
2020-12-22 23:05:02 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Only Content',
|
|
|
|
name: 'onlyContent',
|
|
|
|
type: 'boolean',
|
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
|
|
|
contentIsBinary: [
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
description: 'Whether to return only the content property',
|
2020-12-22 23:05:02 -08:00
|
|
|
},
|
2022-05-30 03:16:44 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-default-missing
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
displayName: 'Parallel Message Processing Limit',
|
2022-05-30 03:16:44 -07:00
|
|
|
name: 'parallelMessages',
|
|
|
|
type: 'number',
|
|
|
|
default: -1,
|
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
|
|
|
acknowledge: [
|
|
|
|
'immediately',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
description: 'Max number of executions at a time. Use -1 for no limit.',
|
|
|
|
},
|
2020-12-22 23:05:02 -08:00
|
|
|
...rabbitDefaultOptions,
|
|
|
|
].sort((a, b) => {
|
|
|
|
if ((a as INodeProperties).displayName.toLowerCase() < (b as INodeProperties).displayName.toLowerCase()) { return -1; }
|
|
|
|
if ((a as INodeProperties).displayName.toLowerCase() > (b as INodeProperties).displayName.toLowerCase()) { return 1; }
|
|
|
|
return 0;
|
|
|
|
}) as INodeProperties[],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
|
|
|
|
const queue = this.getNodeParameter('queue') as string;
|
|
|
|
const options = this.getNodeParameter('options', {}) as IDataObject;
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
const channel = await rabbitmqConnectQueue.call(this, queue, options);
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
2022-05-30 03:16:44 -07:00
|
|
|
let parallelMessages = (options.parallelMessages !== undefined && options.parallelMessages !== -1) ? parseInt(options.parallelMessages as string, 10) : -1;
|
|
|
|
|
|
|
|
if (parallelMessages === 0 || parallelMessages < -1) {
|
2022-07-04 02:12:08 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Parallel message processing limit must be greater than zero (or -1 for no limit)');
|
2022-05-30 03:16:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.getMode() === 'manual') {
|
|
|
|
// Do only catch a single message when executing manually, else messages will leak
|
|
|
|
parallelMessages = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let acknowledgeMode = options.acknowledge ? options.acknowledge : 'immediately';
|
|
|
|
|
|
|
|
if (parallelMessages !== -1 && acknowledgeMode === 'immediately') {
|
|
|
|
// If parallel message limit is set, then the default mode is "executionFinishes"
|
|
|
|
// unless acknowledgeMode got set specifically. Be aware that the mode "immediately"
|
|
|
|
// can not be supported in this case.
|
|
|
|
acknowledgeMode = 'executionFinishes';
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageTracker = new MessageTracker();
|
|
|
|
let consumerTag: string;
|
|
|
|
|
2020-12-22 23:05:02 -08:00
|
|
|
const startConsumer = async () => {
|
2022-05-30 03:16:44 -07:00
|
|
|
if (parallelMessages !== -1) {
|
|
|
|
channel.prefetch(parallelMessages);
|
|
|
|
}
|
|
|
|
|
|
|
|
const consumerInfo = await channel.consume(queue, async (message) => {
|
2020-12-22 23:05:02 -08:00
|
|
|
if (message !== null) {
|
|
|
|
|
2022-05-30 03:16:44 -07:00
|
|
|
try {
|
|
|
|
if (acknowledgeMode !== 'immediately') {
|
|
|
|
messageTracker.received(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
let content: IDataObject | string = message!.content!.toString();
|
2021-04-08 14:34:10 -07:00
|
|
|
|
2022-05-30 03:16:44 -07:00
|
|
|
const item: INodeExecutionData = {
|
|
|
|
json: {},
|
2020-12-22 23:05:02 -08:00
|
|
|
};
|
|
|
|
|
2022-05-30 03:16:44 -07:00
|
|
|
if (options.contentIsBinary === true) {
|
|
|
|
item.binary = {
|
|
|
|
data: await this.helpers.prepareBinaryData(message.content),
|
|
|
|
};
|
|
|
|
|
|
|
|
item.json = message as unknown as IDataObject;
|
|
|
|
message.content = undefined as unknown as Buffer;
|
|
|
|
} else {
|
|
|
|
if (options.jsonParseBody === true) {
|
|
|
|
content = JSON.parse(content as string);
|
|
|
|
}
|
|
|
|
if (options.onlyContent === true) {
|
|
|
|
item.json = content as IDataObject;
|
|
|
|
} else {
|
|
|
|
message.content = content as unknown as Buffer;
|
|
|
|
item.json = message as unknown as IDataObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let responsePromise = undefined;
|
|
|
|
if (acknowledgeMode !== 'immediately') {
|
|
|
|
responsePromise = await createDeferredPromise<IRun>();
|
2020-12-22 23:05:02 -08:00
|
|
|
}
|
2022-05-30 03:16:44 -07:00
|
|
|
|
|
|
|
self.emit([
|
|
|
|
[
|
|
|
|
item,
|
|
|
|
],
|
|
|
|
], undefined, responsePromise);
|
|
|
|
|
|
|
|
if (responsePromise) {
|
|
|
|
// Acknowledge message after the execution finished
|
|
|
|
await responsePromise
|
|
|
|
.promise()
|
|
|
|
.then(async (data: IRun) => {
|
|
|
|
if (data.data.resultData.error) {
|
|
|
|
// The execution did fail
|
|
|
|
if (acknowledgeMode === 'executionFinishesSuccessfully') {
|
|
|
|
channel.nack(message);
|
|
|
|
messageTracker.answered(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.ack(message);
|
|
|
|
messageTracker.answered(message);
|
|
|
|
});
|
2020-12-22 23:05:02 -08:00
|
|
|
} else {
|
2022-05-30 03:16:44 -07:00
|
|
|
// Acknowledge message directly
|
|
|
|
channel.ack(message);
|
2020-12-22 23:05:02 -08:00
|
|
|
}
|
|
|
|
|
2022-05-30 03:16:44 -07:00
|
|
|
} catch (error) {
|
|
|
|
const workflow = this.getWorkflow();
|
|
|
|
const node = this.getNode();
|
|
|
|
if (acknowledgeMode !== 'immediately') {
|
|
|
|
messageTracker.answered(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.error(`There was a problem with the RabbitMQ Trigger node "${node.name}" in workflow "${workflow.id}": "${error.message}"`,
|
|
|
|
{
|
|
|
|
node: node.name,
|
|
|
|
workflowId: workflow.id,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-12-22 23:05:02 -08:00
|
|
|
}
|
|
|
|
});
|
2022-05-30 03:16:44 -07:00
|
|
|
consumerTag = consumerInfo.consumerTag;
|
2020-12-22 23:05:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
startConsumer();
|
|
|
|
|
|
|
|
// The "closeFunction" function gets called by n8n whenever
|
|
|
|
// the workflow gets deactivated and can so clean up.
|
|
|
|
async function closeFunction() {
|
|
|
|
|
2022-05-30 03:16:44 -07:00
|
|
|
try {
|
|
|
|
return messageTracker.closeChannel(channel, consumerTag);
|
|
|
|
} catch(error) {
|
|
|
|
const workflow = self.getWorkflow();
|
|
|
|
const node = self.getNode();
|
|
|
|
Logger.error(`There was a problem closing the RabbitMQ Trigger node connection "${node.name}" in workflow "${workflow.id}": "${error.message}"`,
|
|
|
|
{
|
|
|
|
node: node.name,
|
|
|
|
workflowId: workflow.id,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-12-22 23:05:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
closeFunction,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|