Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Add manageable acknowledge for services #2646

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 66 additions & 7 deletions packages/nodes-base/nodes/RabbitMQ/RabbitMQTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
INodeTypeDescription,
ITriggerFunctions,
ITriggerResponse,
IDeferredPromise,
IExecuteResponsePromiseData,
createDeferredPromise,
} from 'n8n-workflow';

import {
Expand Down Expand Up @@ -44,7 +47,38 @@ export class RabbitMQTrigger implements INodeType {
placeholder: 'queue-name',
description: 'Name of the queue to publish to.',
},

{
displayName: 'Acknowledge',
name: 'acknowledgeMode',
type: 'options',
options: [
{
name: 'Immediately',
value: 'onReceived',
description: 'As soon as this node executes',
},
{
name: 'Using \'Respond to Trigger\' node',
value: 'acknowledgeNode',
description: 'Acknowledge when acknowledge node executes',
},
],
default: 'onReceived',
description: 'When and how to acknowledge to the trigger.',
},
{
displayName: 'Insert a \'Respond to Trigger\' node to control when and how you acknowledge. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.respondToTrigger" target="_blank">More details</a>',
name: 'triggerNotice',
type: 'notice',
displayOptions: {
show: {
acknowledgeMode: [
'acknowledgeNode',
],
},
},
default: '',
},
{
displayName: 'Options',
name: 'options',
Expand Down Expand Up @@ -101,6 +135,7 @@ export class RabbitMQTrigger implements INodeType {
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const queue = this.getNodeParameter('queue') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
const acknowledgeMode = this.getNodeParameter('acknowledgeMode') as string;

const channel = await rabbitmqConnectQueue.call(this, queue, options);

Expand Down Expand Up @@ -134,12 +169,36 @@ export class RabbitMQTrigger implements INodeType {
}
}

self.emit([
[
item,
],
]);
channel.ack(message);
if (acknowledgeMode == 'onReceived') {
self.emit([
[
item,
],
]);

channel.ack(message);
} else {
let acknowledgePromise = await createDeferredPromise<IExecuteResponsePromiseData>();
acknowledgePromise
.promise()
.then((response: IExecuteResponsePromiseData) => {
let acknowledgeResponse = response as IDataObject;
if (acknowledgeResponse.ack) {
channel.ack(message);
} else {
channel.nack(message);
}
})
.catch(async (error) => {
channel.nack(message);
});

self.emit([
[
item,
],
], acknowledgePromise);
}
}
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"node": "n8n-nodes-base.respondToTrigger",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": [
"Core Nodes",
"Utility"
],
"resources": {
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/nodes/n8n-nodes-base.respondToTrigger/"
}
]
},
"subcategories": {
"Core Nodes":["Flow"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
IExecuteFunctions,
} from 'n8n-core';

import {
IDataObject,
IN8nHttpFullResponse,
IN8nHttpResponse,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';

export class RespondToTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Respond to Trigger',
icon: 'file:trigger.svg',
name: 'respondToTrigger',
group: ['transform'],
version: 1,
description: 'Returns data for Trigger',
defaults: {
name: 'Respond to Trigger',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
],
properties: [
{
displayName: 'Acknowledge',
name: 'acknowledge',
type: 'boolean',
default: true,
description: 'Set \'true\' for acknowledge or \'false\' for not acknowledge.',
},
],
};

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();

const acknowledge = this.getNodeParameter('acknowledge', 0, false) as boolean;
const response = {ack: acknowledge} as IDataObject;

this.sendResponse(response);

return this.prepareOutputData(items);
}
}
1 change: 1 addition & 0 deletions packages/nodes-base/nodes/RespondToTrigger/trigger.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@
"dist/nodes/Reddit/Reddit.node.js",
"dist/nodes/Redis/Redis.node.js",
"dist/nodes/RenameKeys/RenameKeys.node.js",
"dist/nodes/RespondToTrigger/RespondToTrigger.node.js",
"dist/nodes/RespondToWebhook/RespondToWebhook.node.js",
"dist/nodes/Rocketchat/Rocketchat.node.js",
"dist/nodes/RssFeedRead/RssFeedRead.node.js",
Expand Down