The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Share one check for whether a workflow step still waits (#27531)
Félix Malfait27a18ba5c40d
diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/utils/get-workflow-step-waiting-state.util.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/utils/get-workflow-step-waiting-state.util.tsnew file mode 100644index 00000000000..b3dcb5a9f24--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/utils/get-workflow-step-waiting-state.util.ts@@ -0,0 +1,31 @@+import { isDefined } from 'twenty-shared/utils';+import { StepStatus } from 'twenty-shared/workflow';++import {+ WorkflowRunStatus,+ type WorkflowRunWorkspaceEntity,+} from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';++// A step still runs until the executor marks it pending, so an outcome can arrive before it waits.+// A pending step with an error waits on a retry, not on what it handed its work to+export const getWorkflowStepWaitingState = ({+ workflowRun,+ stepId,+}: {+ workflowRun: Pick<WorkflowRunWorkspaceEntity, 'status' | 'state'> | null;+ stepId: string;+}): 'WAITING' | 'NOT_READY' | 'GONE' => {+ const stepInfo = workflowRun?.state?.stepInfos?.[stepId];++ if (workflowRun?.status !== WorkflowRunStatus.RUNNING) {+ return 'GONE';+ }++ if (stepInfo?.status === StepStatus.RUNNING) {+ return 'NOT_READY';+ }++ return stepInfo?.status === StepStatus.PENDING && !isDefined(stepInfo.error)+ ? 'WAITING'+ : 'GONE';+};diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.tsindex 987071113d3..d2428a30e8b 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts@@ -2,7 +2,6 @@ import { Injectable, type OnModuleInit } from '@nestjs/common';import { type ActorMetadata } from 'twenty-shared/types';import { isDefined } from 'twenty-shared/utils';-import { StepStatus } from 'twenty-shared/workflow';import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';@@ -20,6 +19,7 @@ import { buildWorkflowAgentRunExecutionContext } from 'src/modules/workflow/workimport { RUN_WORKFLOW_JOB_NAME } from 'src/modules/workflow/workflow-runner/constants/run-workflow-job-name';import { type RunWorkflowJobData } from 'src/modules/workflow/workflow-runner/types/run-workflow-job-data.type';import { buildRunWorkflowJobOptions } from 'src/modules/workflow/workflow-runner/utils/build-run-workflow-job-options.util';+import { getWorkflowStepWaitingState } from 'src/modules/workflow/workflow-runner/utils/get-workflow-step-waiting-state.util';import { WorkflowRunStepLogWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service';import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';@@ -67,28 +67,17 @@ export class WorkflowAgentRunCallerHandlerWorkspaceService});}- // a step handing its work off still runs until the executor marks it pending, and an outcome may come firstasync getWaitingState({workspaceId,caller,}: AgentRunCallerInput<WorkflowStepCaller>): Promise<AgentRunCallerWaitingState> {- const workflowRun = await this.workflowRunWorkspaceService.getWorkflowRun({- workflowRunId: caller.ref.workflowRunId,- workspaceId,+ return getWorkflowStepWaitingState({+ workflowRun: await this.workflowRunWorkspaceService.getWorkflowRun({+ workflowRunId: caller.ref.workflowRunId,+ workspaceId,+ }),+ stepId: caller.ref.stepId,});- const stepInfo = workflowRun?.state?.stepInfos?.[caller.ref.stepId];-- if (workflowRun?.status !== WorkflowRunStatus.RUNNING) {- return 'GONE';- }-- if (stepInfo?.status === StepStatus.RUNNING) {- return 'NOT_READY';- }-- return stepInfo?.status === StepStatus.PENDING && !isDefined(stepInfo.error)- ? 'WAITING'- : 'GONE';}// The run job ends the step with the outcome through the executor's usual path, so a failure isdiff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.tsindex 4030c2894b5..d21fd46114c 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts@@ -32,12 +32,14 @@ const buildService = ({storedWait = STORED_WAIT,runStatus = WorkflowRunStatus.RUNNING,stepStatus = StepStatus.PENDING,+ stepError,readableRecords = [READABLE_RECORD],isRecordReadFailing = false,}: {storedWait?: object | null;runStatus?: WorkflowRunStatus;stepStatus?: StepStatus;+ stepError?: string;readableRecords?: object[];isRecordReadFailing?: boolean;} = {}) => {@@ -51,7 +53,7 @@ const buildService = ({status: runStatus,state: {flow: { steps: [{ id: STEP_ID, type: 'WAIT_FOR_EVENT' }] },- stepInfos: { [STEP_ID]: { status: stepStatus } },+ stepInfos: { [STEP_ID]: { status: stepStatus, error: stepError } },},}),updateStepInfoIfPending: jest@@ -296,6 +298,23 @@ describe('WorkflowStepPendingWakeUpHandlerWorkspaceService', () => {expect(messageQueueService.add).not.toHaveBeenCalled();});+ it('removes the wait of a step that now waits on a retry without resuming it', async () => {+ const {+ service,+ pendingWakeUpService,+ workflowRunWorkspaceService,+ messageQueueService,+ } = buildService({ stepError: 'Step failed' });++ await service.resolve({ workspaceId: WORKSPACE_ID, wakeUpId: WAIT_ID });++ expect(pendingWakeUpService.claim).toHaveBeenCalled();+ expect(+ workflowRunWorkspaceService.updateStepInfoIfPending,+ ).not.toHaveBeenCalled();+ expect(messageQueueService.add).not.toHaveBeenCalled();+ });+it('fails the run when the step cannot resume, since its wait is gone', async () => {const { service, workflowRunWorkspaceService, messageQueueService } =buildService();diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.tsindex dd5f94ad3ac..5825c882f08 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts@@ -20,6 +20,7 @@ import { WorkflowExecutionContextService } from 'src/modules/workflow/workflow-eimport { RUN_WORKFLOW_JOB_NAME } from 'src/modules/workflow/workflow-runner/constants/run-workflow-job-name';import { type RunWorkflowJobData } from 'src/modules/workflow/workflow-runner/types/run-workflow-job-data.type';import { buildRunWorkflowJobOptions } from 'src/modules/workflow/workflow-runner/utils/build-run-workflow-job-options.util';+import { getWorkflowStepWaitingState } from 'src/modules/workflow/workflow-runner/utils/get-workflow-step-waiting-state.util';import { isWorkflowRunNotFoundError } from 'src/modules/workflow/workflow-runner/utils/is-workflow-run-not-found-error.util';import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';import { buildDefaultWaitResult } from 'src/modules/workflow/workflow-wait/utils/build-default-wait-result.util';@@ -27,9 +28,7 @@ import { buildDefaultWaitResult } from 'src/modules/workflow/workflow-wait/utils// A WORKFLOW_STEP wake-up is owned by a workflow run and keyed by the step that waits@Injectable()export class WorkflowStepPendingWakeUpHandlerWorkspaceService- implements- PendingWakeUpOwnerHandler<WorkflowRunWorkspaceEntity>,- OnModuleInit+ implements PendingWakeUpOwnerHandler<WorkflowRunWorkspaceEntity>, OnModuleInit{constructor(private readonly pendingWakeUpOwnerHandlerRegistryService: PendingWakeUpOwnerHandlerRegistryService,@@ -61,21 +60,15 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceServiceworkflowRunId,workspaceId,});- const stepStatus = workflowRun?.state?.stepInfos?.[stepId]?.status;+ const status = getWorkflowStepWaitingState({ workflowRun, stepId });- if (workflowRun?.status !== WorkflowRunStatus.RUNNING) {- return { status: 'GONE', owner: workflowRun };+ if (status === 'NOT_READY') {+ return { status };}- // the wait is armed before its step reads as pending- if (stepStatus === StepStatus.RUNNING) {- return { status: 'NOT_READY' };- }-- return {- status: stepStatus === StepStatus.PENDING ? 'WAITING' : 'GONE',- owner: workflowRun,- };+ return status === 'WAITING' && isDefined(workflowRun)+ ? { status, owner: workflowRun }+ : { status: 'GONE', owner: workflowRun };}async getReadPermissions({ wakeUp }: { wakeUp: PendingWakeUpEntity }) {@@ -89,13 +82,14 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceServiceclaimedWakeUp,outcome,owner: workflowRun,+ isOwnerGone,}: {claimedWakeUp: PendingWakeUpEntity;outcome: PendingWakeUpOutcome;owner: WorkflowRunWorkspaceEntity | null;isOwnerGone: boolean;}): Promise<void> {- if (!isDefined(workflowRun)) {+ if (!isDefined(workflowRun) || isOwnerGone) {return;}