mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor(core): Delete some duplicate code between ActiveWebhooks and ActiveWorkflowRunner (no-changelog) (#6951)
This commit is contained in:
parent
9b27878d8f
commit
41c3cc89ca
|
@ -63,25 +63,13 @@ export class ActiveWebhooks {
|
||||||
this.webhookUrls[webhookKey].push(webhookData);
|
this.webhookUrls[webhookKey].push(webhookData);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const webhookExists = await workflow.runWebhookMethod(
|
await workflow.createWebhookIfNotExists(
|
||||||
'checkExists',
|
|
||||||
webhookData,
|
webhookData,
|
||||||
NodeExecuteFunctions,
|
NodeExecuteFunctions,
|
||||||
mode,
|
mode,
|
||||||
activation,
|
activation,
|
||||||
this.testWebhooks,
|
this.testWebhooks,
|
||||||
);
|
);
|
||||||
if (webhookExists !== true) {
|
|
||||||
// If webhook does not exist yet create it
|
|
||||||
await workflow.runWebhookMethod(
|
|
||||||
'create',
|
|
||||||
webhookData,
|
|
||||||
NodeExecuteFunctions,
|
|
||||||
mode,
|
|
||||||
activation,
|
|
||||||
this.testWebhooks,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// If there was a problem unregister the webhook again
|
// If there was a problem unregister the webhook again
|
||||||
if (this.webhookUrls[webhookKey].length <= 1) {
|
if (this.webhookUrls[webhookKey].length <= 1) {
|
||||||
|
@ -183,8 +171,7 @@ export class ActiveWebhooks {
|
||||||
// Go through all the registered webhooks of the workflow and remove them
|
// Go through all the registered webhooks of the workflow and remove them
|
||||||
|
|
||||||
for (const webhookData of webhooks) {
|
for (const webhookData of webhooks) {
|
||||||
await workflow.runWebhookMethod(
|
await workflow.deleteWebhook(
|
||||||
'delete',
|
|
||||||
webhookData,
|
webhookData,
|
||||||
NodeExecuteFunctions,
|
NodeExecuteFunctions,
|
||||||
mode,
|
mode,
|
||||||
|
|
|
@ -392,25 +392,13 @@ export class ActiveWorkflowRunner implements IWebhookManager {
|
||||||
try {
|
try {
|
||||||
// TODO: this should happen in a transaction, that way we don't need to manually remove this in `catch`
|
// TODO: this should happen in a transaction, that way we don't need to manually remove this in `catch`
|
||||||
await this.webhookService.storeWebhook(webhook);
|
await this.webhookService.storeWebhook(webhook);
|
||||||
const webhookExists = await workflow.runWebhookMethod(
|
await workflow.createWebhookIfNotExists(
|
||||||
'checkExists',
|
|
||||||
webhookData,
|
webhookData,
|
||||||
NodeExecuteFunctions,
|
NodeExecuteFunctions,
|
||||||
mode,
|
mode,
|
||||||
activation,
|
activation,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
if (webhookExists !== true) {
|
|
||||||
// If webhook does not exist yet create it
|
|
||||||
await workflow.runWebhookMethod(
|
|
||||||
'create',
|
|
||||||
webhookData,
|
|
||||||
NodeExecuteFunctions,
|
|
||||||
mode,
|
|
||||||
activation,
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (
|
if (
|
||||||
activation === 'init' &&
|
activation === 'init' &&
|
||||||
|
@ -489,14 +477,7 @@ export class ActiveWorkflowRunner implements IWebhookManager {
|
||||||
const webhooks = WebhookHelpers.getWorkflowWebhooks(workflow, additionalData, undefined, true);
|
const webhooks = WebhookHelpers.getWorkflowWebhooks(workflow, additionalData, undefined, true);
|
||||||
|
|
||||||
for (const webhookData of webhooks) {
|
for (const webhookData of webhooks) {
|
||||||
await workflow.runWebhookMethod(
|
await workflow.deleteWebhook(webhookData, NodeExecuteFunctions, mode, 'update', false);
|
||||||
'delete',
|
|
||||||
webhookData,
|
|
||||||
NodeExecuteFunctions,
|
|
||||||
mode,
|
|
||||||
'update',
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await WorkflowHelpers.saveStaticData(workflow);
|
await WorkflowHelpers.saveStaticData(workflow);
|
||||||
|
|
|
@ -972,12 +972,52 @@ export class Workflow {
|
||||||
return this.__getStartNode(Object.keys(this.nodes));
|
return this.__getStartNode(Object.keys(this.nodes));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async createWebhookIfNotExists(
|
||||||
* Executes the Webhooks method of the node
|
webhookData: IWebhookData,
|
||||||
*
|
nodeExecuteFunctions: INodeExecuteFunctions,
|
||||||
* @param {WebhookSetupMethodNames} method The name of the method to execute
|
mode: WorkflowExecuteMode,
|
||||||
*/
|
activation: WorkflowActivateMode,
|
||||||
async runWebhookMethod(
|
isTest?: boolean,
|
||||||
|
): Promise<void> {
|
||||||
|
const webhookExists = await this.runWebhookMethod(
|
||||||
|
'checkExists',
|
||||||
|
webhookData,
|
||||||
|
nodeExecuteFunctions,
|
||||||
|
mode,
|
||||||
|
activation,
|
||||||
|
isTest,
|
||||||
|
);
|
||||||
|
if (!webhookExists) {
|
||||||
|
// If webhook does not exist yet create it
|
||||||
|
await this.runWebhookMethod(
|
||||||
|
'create',
|
||||||
|
webhookData,
|
||||||
|
nodeExecuteFunctions,
|
||||||
|
mode,
|
||||||
|
activation,
|
||||||
|
isTest,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteWebhook(
|
||||||
|
webhookData: IWebhookData,
|
||||||
|
nodeExecuteFunctions: INodeExecuteFunctions,
|
||||||
|
mode: WorkflowExecuteMode,
|
||||||
|
activation: WorkflowActivateMode,
|
||||||
|
isTest?: boolean,
|
||||||
|
) {
|
||||||
|
await this.runWebhookMethod(
|
||||||
|
'delete',
|
||||||
|
webhookData,
|
||||||
|
nodeExecuteFunctions,
|
||||||
|
mode,
|
||||||
|
activation,
|
||||||
|
isTest,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async runWebhookMethod(
|
||||||
method: WebhookSetupMethodNames,
|
method: WebhookSetupMethodNames,
|
||||||
webhookData: IWebhookData,
|
webhookData: IWebhookData,
|
||||||
nodeExecuteFunctions: INodeExecuteFunctions,
|
nodeExecuteFunctions: INodeExecuteFunctions,
|
||||||
|
|
Loading…
Reference in a new issue