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 output to workflow run #7276

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ export const WORKFLOW_RUN_STANDARD_FIELD_IDS = {
endedAt: '20202020-e1c1-4b6b-bbbd-b2beaf2e159e',
status: '20202020-6b3e-4f9c-8c2b-2e5b8e6d6f3b',
createdBy: '20202020-6007-401a-8aa5-e6f38581a6f3',
output: '20202020-7be4-4db2-8ac6-3ff0d740843d',
};

export const WORKFLOW_VERSION_STANDARD_FIELD_IDS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export enum WorkflowRunStatus {
FAILED = 'FAILED',
}

export type WorkflowRunOutput = {
steps: {
type: string;
result: object | undefined;
error: object | undefined;
}[];
};

@WorkspaceEntity({
standardId: STANDARD_OBJECT_IDS.workflowRun,
namePlural: 'workflowRuns',
Expand Down Expand Up @@ -108,6 +116,15 @@ export class WorkflowRunWorkspaceEntity extends BaseWorkspaceEntity {
})
createdBy: ActorMetadata;

@WorkspaceField({
standardId: WORKFLOW_RUN_STANDARD_FIELD_IDS.output,
type: FieldMetadataType.RAW_JSON,
label: 'Output',
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
description: 'Json object to provide output of the workflow run',
})
@WorkspaceIsNullable()
output: WorkflowRunOutput | null;

// Relations
@WorkspaceRelation({
standardId: WORKFLOW_RUN_STANDARD_FIELD_IDS.workflowVersion,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Injectable } from '@nestjs/common';

import { WorkflowStep } from 'src/modules/workflow/workflow-executor/types/workflow-action.type';
import {
WorkflowExecutorException,
WorkflowExecutorExceptionCode,
} from 'src/modules/workflow/workflow-executor/exceptions/workflow-executor.exception';
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import { WorkflowActionFactory } from 'src/modules/workflow/workflow-executor/factories/workflow-action.factory';
import {
WorkflowActionType,
WorkflowStep,
} from 'src/modules/workflow/workflow-executor/types/workflow-action.type';

const MAX_RETRIES_ON_FAILURE = 3;

export type WorkflowExecutionOutput = {
result?: object;
error?: object;
export type WorkflowExecutorOutput = {
steps: {
type: WorkflowActionType;
result: object | undefined;
error: object | undefined;
}[];
status?: WorkflowRunStatus;
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
};

@Injectable()
Expand All @@ -22,17 +26,17 @@ export class WorkflowExecutorWorkspaceService {
currentStepIndex,
steps,
payload,
output,
attemptCount = 1,
}: {
currentStepIndex: number;
steps: WorkflowStep[];
output: WorkflowExecutorOutput;
payload?: object;
attemptCount?: number;
}): Promise<WorkflowExecutionOutput> {
}): Promise<WorkflowExecutorOutput> {
if (currentStepIndex >= steps.length) {
return {
result: payload,
};
return { ...output, status: WorkflowRunStatus.COMPLETED };
}

const step = steps[currentStepIndex];
Expand All @@ -44,26 +48,49 @@ export class WorkflowExecutorWorkspaceService {
payload,
});

const stepOutput = {
type: step.type,
result: result.result,
error: result.error,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be careful when storing the raw error. Indeed, here, we store the error with the stack trace, showing the internal file system of the worker running the jobs. These errors will be displayed to the user, and it's usually considered unsafe to expose stack traces publicly.

};

const updatedOutput = {
...output,
steps: [...output.steps, stepOutput],
};

if (result.result) {
return await this.execute({
currentStepIndex: currentStepIndex + 1,
steps,
payload: result.result,
output: updatedOutput,
});
}

if (!result.error) {
throw new WorkflowExecutorException(
'Execution result error, no data or error',
WorkflowExecutorExceptionCode.WORKFLOW_FAILED,
);
return {
...output,
steps: [
...output.steps,
{
type: step.type,
result: undefined,
error: {
errorMessage: 'Execution result error, no data or error',
},
},
],
status: WorkflowRunStatus.FAILED,
};
}

if (step.settings.errorHandlingOptions.continueOnFailure.value) {
return await this.execute({
currentStepIndex: currentStepIndex + 1,
steps,
payload,
output: updatedOutput,
});
}

Expand All @@ -75,13 +102,11 @@ export class WorkflowExecutorWorkspaceService {
currentStepIndex,
steps,
payload,
output,
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be interesting to also store the output of the failed attempts. See my comment where I suggest adding two more fields to WorkflowExecutorOutput.

attemptCount: attemptCount + 1,
});
}

throw new WorkflowExecutorException(
`Workflow failed: ${result.error}`,
WorkflowExecutorExceptionCode.WORKFLOW_FAILED,
);
return { ...updatedOutput, status: WorkflowRunStatus.FAILED };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Scope } from '@nestjs/common';
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
import { WorkflowExecutorWorkspaceService } from 'src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service';
import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workspace-services/workflow-run.workspace-service';

Expand Down Expand Up @@ -36,24 +36,22 @@ export class RunWorkflowJob {
workflowVersionId,
);

try {
const { steps, status } =
await this.workflowExecutorWorkspaceService.execute({
currentStepIndex: 0,
steps: workflowVersion.steps || [],
payload,
output: {
steps: [],
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
},
});

await this.workflowRunWorkspaceService.endWorkflowRun(
workflowRunId,
WorkflowRunStatus.COMPLETED,
);
} catch (error) {
await this.workflowRunWorkspaceService.endWorkflowRun(
workflowRunId,
WorkflowRunStatus.FAILED,
);

throw error;
}
await this.workflowRunWorkspaceService.endWorkflowRun(
workflowRunId,
status || WorkflowRunStatus.FAILED,
{
steps,
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Injectable } from '@nestjs/common';

import { ActorMetadata } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
import {
WorkflowRunOutput,
WorkflowRunStatus,
WorkflowRunWorkspaceEntity,
} from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
import {
WorkflowRunException,
WorkflowRunExceptionCode,
Expand Down Expand Up @@ -70,7 +71,11 @@ export class WorkflowRunWorkspaceService {
});
}

async endWorkflowRun(workflowRunId: string, status: WorkflowRunStatus) {
async endWorkflowRun(
workflowRunId: string,
status: WorkflowRunStatus,
output: WorkflowRunOutput,
) {
const workflowRunRepository =
await this.twentyORMManager.getRepository<WorkflowRunWorkspaceEntity>(
'workflowRun',
Expand All @@ -96,6 +101,7 @@ export class WorkflowRunWorkspaceService {

return workflowRunRepository.update(workflowRunToUpdate.id, {
status,
output,
endedAt: new Date().toISOString(),
});
}
Expand Down
Loading