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

fix(core): Streamline multiple pinned triggers behavior #4569

Merged
merged 1 commit into from
Nov 10, 2022
Merged
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
19 changes: 3 additions & 16 deletions packages/cli/src/workflows/workflows.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */

import express from 'express';
import { INode, IPinData, LoggerProxy, Workflow } from 'n8n-workflow';
import { INode, LoggerProxy, Workflow } from 'n8n-workflow';

import axios from 'axios';
import * as ActiveWorkflowRunner from '@/ActiveWorkflowRunner';
Expand All @@ -18,7 +18,6 @@ import {
IWorkflowResponse,
IExecutionPushResponse,
IWorkflowExecutionDataProcess,
IWorkflowDb,
} from '@/Interfaces';
import config from '@/config';
import * as TagHelpers from '@/TagHelpers';
Expand Down Expand Up @@ -49,18 +48,6 @@ workflowsController.use((req, res, next) => {

workflowsController.use('/', EEWorkflowController);

const isTrigger = (nodeType: string) =>
['trigger', 'webhook'].some((suffix) => nodeType.toLowerCase().includes(suffix));

function findFirstPinnedTrigger(workflow: IWorkflowDb, pinData?: IPinData) {
if (!pinData) return;

// eslint-disable-next-line consistent-return
return workflow.nodes.find(
(node) => !node.disabled && isTrigger(node.type) && pinData[node.name],
);
}

/**
* POST /workflows
*/
Expand Down Expand Up @@ -351,11 +338,11 @@ workflowsController.post(

const sessionId = GenericHelpers.getSessionId(req);

const pinnedTrigger = findFirstPinnedTrigger(workflowData, pinData);
const pinnedTrigger = WorkflowsService.findPinnedTrigger(workflowData, startNodes, pinData);

// If webhooks nodes exist and are active we have to wait for till we receive a call
if (
pinnedTrigger === undefined &&
pinnedTrigger === null &&
(runData === undefined ||
startNodes === undefined ||
startNodes.length === 0 ||
Expand Down
25 changes: 24 additions & 1 deletion packages/cli/src/workflows/workflows.services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonObject, jsonParse, LoggerProxy } from 'n8n-workflow';
import { IPinData, JsonObject, jsonParse, LoggerProxy } from 'n8n-workflow';
import { FindManyOptions, FindOneOptions, In, ObjectLiteral } from 'typeorm';
import { validate as jsonSchemaValidate } from 'jsonschema';
import * as ActiveWorkflowRunner from '@/ActiveWorkflowRunner';
Expand All @@ -15,6 +15,7 @@ import { validateEntity } from '@/GenericHelpers';
import { externalHooks } from '@/Server';
import * as TagHelpers from '@/TagHelpers';
import { getSharedWorkflowIds } from '@/WorkflowHelpers';
import { IWorkflowDb } from '..';

export interface IGetWorkflowsQueryFilter {
id?: number | string;
Expand Down Expand Up @@ -61,6 +62,28 @@ export class WorkflowsService {
return Db.collections.SharedWorkflow.findOne(options);
}

/**
* Find the pinned trigger to execute the workflow from, if any.
*/
static findPinnedTrigger(workflow: IWorkflowDb, startNodes?: string[], pinData?: IPinData) {
if (!pinData || !startNodes) return null;

const isTrigger = (nodeTypeName: string) =>
['trigger', 'webhook'].some((suffix) => nodeTypeName.toLowerCase().includes(suffix));

const pinnedTriggers = workflow.nodes.filter(
(node) => !node.disabled && pinData[node.name] && isTrigger(node.type),
);

if (pinnedTriggers.length === 0) return null;

if (startNodes?.length === 0) return pinnedTriggers[0]; // full execution

const [startNodeName] = startNodes;

return pinnedTriggers.find((pt) => pt.name === startNodeName) ?? null; // partial execution
}

static async get(workflow: Partial<WorkflowEntity>, options?: { relations: string[] }) {
return Db.collections.Workflow.findOne(workflow, options);
}
Expand Down