Allow to stop running workflow (#15270)
https://github.com/user-attachments/assets/599154e4-8743-471b-b05a-721b635bcf4e On stoppage: - if no running steps, mark pending as failed and end the workflow - if running steps, set as stopping and exit. Going to the next step, as the workflow is not running anymore, it will naturally stop
This commit is contained in:
+124
@@ -0,0 +1,124 @@
|
||||
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Command } from 'nest-commander';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
|
||||
import {
|
||||
ActiveOrSuspendedWorkspacesMigrationCommandRunner,
|
||||
type RunOnWorkspaceArgs,
|
||||
} from 'src/database/commands/command-runners/active-or-suspended-workspaces-migration.command-runner';
|
||||
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';
|
||||
import { WORKFLOW_RUN_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
|
||||
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
|
||||
|
||||
@Command({
|
||||
name: 'upgrade:1-10:add-workflow-run-stop-statuses',
|
||||
description: 'Add stopped and stopping statuses to workflow runs',
|
||||
})
|
||||
export class AddWorkflowRunStopStatusesCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
|
||||
constructor(
|
||||
@InjectRepository(WorkspaceEntity)
|
||||
protected readonly workspaceRepository: Repository<WorkspaceEntity>,
|
||||
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
@InjectRepository(FieldMetadataEntity)
|
||||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
|
||||
@InjectDataSource()
|
||||
private readonly coreDataSource: DataSource,
|
||||
) {
|
||||
super(workspaceRepository, twentyORMGlobalManager);
|
||||
}
|
||||
|
||||
override async runOnWorkspace({
|
||||
workspaceId,
|
||||
options,
|
||||
}: RunOnWorkspaceArgs): Promise<void> {
|
||||
this.logger.log(
|
||||
`Adding stopped and stopping statuses to workflow runs for workspace ${workspaceId}`,
|
||||
);
|
||||
|
||||
const workflowRunStatusFieldMetadata =
|
||||
await this.fieldMetadataRepository.findOne({
|
||||
where: {
|
||||
standardId: WORKFLOW_RUN_STANDARD_FIELD_IDS.status,
|
||||
},
|
||||
});
|
||||
|
||||
if (!workflowRunStatusFieldMetadata) {
|
||||
this.logger.error(
|
||||
`Workflow run status field metadata not found for workspace ${workspaceId}`,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const workflowRunStatusFieldMetadataOptions =
|
||||
workflowRunStatusFieldMetadata.options;
|
||||
|
||||
if (
|
||||
workflowRunStatusFieldMetadataOptions?.some(
|
||||
(option) =>
|
||||
option.value === WorkflowRunStatus.STOPPED ||
|
||||
option.value === WorkflowRunStatus.STOPPING,
|
||||
)
|
||||
) {
|
||||
this.logger.log(
|
||||
`Workflow run status field metadata options already contain stopped and stopping statuses for workspace ${workspaceId}`,
|
||||
);
|
||||
|
||||
return;
|
||||
} else if (options.dryRun) {
|
||||
this.logger.log(
|
||||
`Would add stopped and stopping statuses to workflow run status field metadata for workspace ${workspaceId}`,
|
||||
);
|
||||
} else {
|
||||
workflowRunStatusFieldMetadataOptions?.push({
|
||||
value: WorkflowRunStatus.STOPPING,
|
||||
label: 'Stopping',
|
||||
position: 5,
|
||||
color: 'orange',
|
||||
});
|
||||
workflowRunStatusFieldMetadataOptions?.push({
|
||||
value: WorkflowRunStatus.STOPPED,
|
||||
label: 'Stopped',
|
||||
position: 6,
|
||||
color: 'gray',
|
||||
});
|
||||
|
||||
await this.fieldMetadataRepository.save(workflowRunStatusFieldMetadata);
|
||||
|
||||
this.logger.log(
|
||||
`Stopped and stopping statuses added to workflow run status field metadata for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const schemaName = getWorkspaceSchemaName(workspaceId);
|
||||
|
||||
if (options.dryRun) {
|
||||
this.logger.log(
|
||||
`Would try to add stopped and stopping statuses to workflow run status enum for workspace ${workspaceId}`,
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
await this.coreDataSource.query(
|
||||
`ALTER TYPE ${schemaName}."workflowRun_status_enum" ADD VALUE 'STOPPING'`,
|
||||
);
|
||||
this.logger.log(
|
||||
`Stopping status added to workflow run status enum for workspace ${workspaceId}`,
|
||||
);
|
||||
await this.coreDataSource.query(
|
||||
`ALTER TYPE ${schemaName}."workflowRun_status_enum" ADD VALUE 'STOPPED'`,
|
||||
);
|
||||
this.logger.log(
|
||||
`Stopped status added to workflow run status enum for workspace ${workspaceId}`,
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Error adding stopped and stopping statuses to workflow run status enum for workspace ${workspaceId}: ${error}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -1,23 +1,27 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { AddWorkflowRunStopStatusesCommand } from 'src/database/commands/upgrade-version-command/1-10/1-10-add-workflow-run-stop-statuses.command';
|
||||
import { MigrateAttachmentAuthorToCreatedByCommand } from 'src/database/commands/upgrade-version-command/1-10/1-10-migrate-attachment-author-to-created-by.command';
|
||||
import { MigrateAttachmentTypeToFileCategoryCommand } from 'src/database/commands/upgrade-version-command/1-10/1-10-migrate-attachment-type-to-file-category.command';
|
||||
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { WorkspaceSchemaManagerModule } from 'src/engine/twenty-orm/workspace-schema-manager/workspace-schema-manager.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([WorkspaceEntity]),
|
||||
TypeOrmModule.forFeature([WorkspaceEntity, FieldMetadataEntity]),
|
||||
WorkspaceSchemaManagerModule,
|
||||
],
|
||||
providers: [
|
||||
MigrateAttachmentAuthorToCreatedByCommand,
|
||||
MigrateAttachmentTypeToFileCategoryCommand,
|
||||
AddWorkflowRunStopStatusesCommand,
|
||||
],
|
||||
exports: [
|
||||
MigrateAttachmentAuthorToCreatedByCommand,
|
||||
MigrateAttachmentTypeToFileCategoryCommand,
|
||||
AddWorkflowRunStopStatusesCommand,
|
||||
],
|
||||
})
|
||||
export class V1_10_UpgradeVersionCommandModule {}
|
||||
|
||||
+3
-1
@@ -19,6 +19,7 @@ import { AddEnqueuedStatusToWorkflowRunCommand } from 'src/database/commands/upg
|
||||
import { FixSchemaArrayTypeCommand } from 'src/database/commands/upgrade-version-command/1-1/1-1-fix-schema-array-type.command';
|
||||
import { FixUpdateStandardFieldsIsLabelSyncedWithName } from 'src/database/commands/upgrade-version-command/1-1/1-1-fix-update-standard-field-is-label-synced-with-name.command';
|
||||
import { MigrateWorkflowRunStatesCommand } from 'src/database/commands/upgrade-version-command/1-1/1-1-migrate-workflow-run-state.command';
|
||||
import { AddWorkflowRunStopStatusesCommand } from 'src/database/commands/upgrade-version-command/1-10/1-10-add-workflow-run-stop-statuses.command';
|
||||
import { MigrateAttachmentAuthorToCreatedByCommand } from 'src/database/commands/upgrade-version-command/1-10/1-10-migrate-attachment-author-to-created-by.command';
|
||||
import { MigrateAttachmentTypeToFileCategoryCommand } from 'src/database/commands/upgrade-version-command/1-10/1-10-migrate-attachment-type-to-file-category.command';
|
||||
import { AddEnqueuedStatusToWorkflowRunV2Command } from 'src/database/commands/upgrade-version-command/1-2/1-2-add-enqueued-status-to-workflow-run-v2.command';
|
||||
@@ -101,6 +102,7 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
// 1.10 Commands
|
||||
protected readonly migrateAttachmentAuthorToCreatedByCommand: MigrateAttachmentAuthorToCreatedByCommand,
|
||||
protected readonly migrateAttachmentTypeToFileCategoryCommand: MigrateAttachmentTypeToFileCategoryCommand,
|
||||
protected readonly addWorkflowRunStopStatusesCommand: AddWorkflowRunStopStatusesCommand,
|
||||
) {
|
||||
super(
|
||||
workspaceRepository,
|
||||
@@ -206,7 +208,7 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
};
|
||||
|
||||
const commands_1100: VersionCommands = {
|
||||
beforeSyncMetadata: [],
|
||||
beforeSyncMetadata: [this.addWorkflowRunStopStatusesCommand],
|
||||
afterSyncMetadata: [
|
||||
this.migrateAttachmentAuthorToCreatedByCommand,
|
||||
this.migrateAttachmentTypeToFileCategoryCommand,
|
||||
|
||||
@@ -19,6 +19,7 @@ export enum MetricsKeys {
|
||||
WorkflowRunStartedManualTrigger = 'workflow-run/started/manual-trigger',
|
||||
WorkflowRunCompleted = 'workflow-run/completed',
|
||||
WorkflowRunFailed = 'workflow-run/failed',
|
||||
WorkflowRunStopped = 'workflow-run/stopped',
|
||||
WorkflowRunFailedThrottled = 'workflow-run/failed/throttled',
|
||||
WorkflowRunFailedToEnqueue = 'workflow-run/failed/to-enqueue',
|
||||
AIToolExecutionFailed = 'ai-tool-execution/failed',
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import { Field, ObjectType } from '@nestjs/graphql';
|
||||
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
|
||||
@ObjectType('RunWorkflowVersionOutput')
|
||||
export class RunWorkflowVersionOutput {
|
||||
@Field(() => UUIDScalarType)
|
||||
workflowRunId: string;
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
import { Field, ObjectType } from '@nestjs/graphql';
|
||||
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
|
||||
|
||||
@ObjectType('WorkflowRun')
|
||||
export class WorkflowRunDTO {
|
||||
@Field(() => UUIDScalarType)
|
||||
workflowRunId: string;
|
||||
id: string;
|
||||
|
||||
@Field(() => WorkflowRunStatus)
|
||||
status: WorkflowRunStatus;
|
||||
}
|
||||
|
||||
+10
-1
@@ -7,6 +7,7 @@ import { PreventNestToAutoLogGraphqlErrorsFilter } from 'src/engine/core-modules
|
||||
import { ResolverValidationPipe } from 'src/engine/core-modules/graphql/pipes/resolver-validation.pipe';
|
||||
import { UserEntity } from 'src/engine/core-modules/user/user.entity';
|
||||
import { RunWorkflowVersionInput } from 'src/engine/core-modules/workflow/dtos/run-workflow-version-input.dto';
|
||||
import { RunWorkflowVersionOutput } from 'src/engine/core-modules/workflow/dtos/run-workflow-version-output.dto';
|
||||
import { WorkflowRunDTO } from 'src/engine/core-modules/workflow/dtos/workflow-run.dto';
|
||||
import { WorkflowTriggerGraphqlApiExceptionFilter } from 'src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter';
|
||||
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
@@ -59,7 +60,7 @@ export class WorkflowTriggerResolver {
|
||||
);
|
||||
}
|
||||
|
||||
@Mutation(() => WorkflowRunDTO)
|
||||
@Mutation(() => RunWorkflowVersionOutput)
|
||||
async runWorkflowVersion(
|
||||
@AuthUser() user: UserEntity,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
@@ -91,4 +92,12 @@ export class WorkflowTriggerResolver {
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => WorkflowRunDTO)
|
||||
async stopWorkflowRun(
|
||||
@Args('workflowRunId', { type: () => UUIDScalarType })
|
||||
workflowRunId: string,
|
||||
) {
|
||||
return this.workflowTriggerWorkspaceService.stopWorkflowRun(workflowRunId);
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -18,7 +18,10 @@ export class WorkflowRunUpdateOnePreQueryHook
|
||||
_objectName: string,
|
||||
payload: UpdateOneResolverArgs<WorkflowRunWorkspaceEntity>,
|
||||
): Promise<UpdateOneResolverArgs<WorkflowRunWorkspaceEntity>> {
|
||||
if (Object.keys(payload.data).length === 1 && payload.data.name) {
|
||||
const allowedFields = ['name'];
|
||||
const payloadKeys = Object.keys(payload.data);
|
||||
|
||||
if (payloadKeys.every((key) => allowedFields.includes(key))) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
|
||||
+21
@@ -1,3 +1,5 @@
|
||||
import { registerEnumType } from '@nestjs/graphql';
|
||||
|
||||
import { msg } from '@lingui/core/macro';
|
||||
import { FieldMetadataType } from 'twenty-shared/types';
|
||||
import { type WorkflowRunStepInfos } from 'twenty-shared/workflow';
|
||||
@@ -40,8 +42,15 @@ export enum WorkflowRunStatus {
|
||||
COMPLETED = 'COMPLETED',
|
||||
FAILED = 'FAILED',
|
||||
ENQUEUED = 'ENQUEUED',
|
||||
STOPPING = 'STOPPING',
|
||||
STOPPED = 'STOPPED',
|
||||
}
|
||||
|
||||
registerEnumType(WorkflowRunStatus, {
|
||||
name: 'WorkflowRunStatusEnum',
|
||||
description: 'Status of the workflow run',
|
||||
});
|
||||
|
||||
export type StepOutput = {
|
||||
id: string;
|
||||
output: WorkflowActionOutput;
|
||||
@@ -159,6 +168,18 @@ export class WorkflowRunWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
position: 4,
|
||||
color: 'blue',
|
||||
},
|
||||
{
|
||||
value: WorkflowRunStatus.STOPPING,
|
||||
label: 'Stopping',
|
||||
position: 5,
|
||||
color: 'orange',
|
||||
},
|
||||
{
|
||||
value: WorkflowRunStatus.STOPPED,
|
||||
label: 'Stopped',
|
||||
position: 6,
|
||||
color: 'gray',
|
||||
},
|
||||
],
|
||||
defaultValue: "'NOT_STARTED'",
|
||||
})
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
|
||||
|
||||
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
|
||||
|
||||
export const workflowHasRunningSteps = ({
|
||||
stepInfos,
|
||||
steps,
|
||||
}: {
|
||||
stepInfos: WorkflowRunStepInfos;
|
||||
steps: WorkflowAction[];
|
||||
}) => {
|
||||
return steps.some(
|
||||
(step) => stepInfos[step.id]?.status === StepStatus.RUNNING,
|
||||
);
|
||||
};
|
||||
+13
@@ -20,6 +20,7 @@ import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queu
|
||||
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
|
||||
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
|
||||
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
|
||||
import { workflowHasRunningSteps } from 'src/modules/workflow/common/utils/workflow-has-running-steps.util';
|
||||
import { WorkflowActionFactory } from 'src/modules/workflow/workflow-executor/factories/workflow-action.factory';
|
||||
import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';
|
||||
import {
|
||||
@@ -224,6 +225,18 @@ export class WorkflowExecutorWorkspaceService {
|
||||
|
||||
const steps = workflowRun.state.flow.steps;
|
||||
|
||||
if (workflowRun.status === WorkflowRunStatus.STOPPING) {
|
||||
if (!workflowHasRunningSteps({ stepInfos, steps })) {
|
||||
await this.workflowRunWorkspaceService.endWorkflowRun({
|
||||
workflowRunId,
|
||||
workspaceId,
|
||||
status: WorkflowRunStatus.STOPPED,
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (workflowShouldFail({ stepInfos, steps })) {
|
||||
await this.workflowRunWorkspaceService.endWorkflowRun({
|
||||
workflowRunId,
|
||||
|
||||
+37
-38
@@ -190,7 +190,7 @@ export class WorkflowRunWorkspaceService {
|
||||
}: {
|
||||
workflowRunId: string;
|
||||
workspaceId: string;
|
||||
status: WorkflowRunStatus;
|
||||
status: Extract<WorkflowRunStatus, 'COMPLETED' | 'FAILED' | 'STOPPED'>;
|
||||
error?: string;
|
||||
}) {
|
||||
const workflowRunToUpdate = await this.getWorkflowRunOrFail({
|
||||
@@ -199,13 +199,10 @@ export class WorkflowRunWorkspaceService {
|
||||
});
|
||||
|
||||
let updatedStepInfos = {};
|
||||
const shouldUpdateStepInfos = status === WorkflowRunStatus.FAILED;
|
||||
|
||||
if (shouldUpdateStepInfos) {
|
||||
updatedStepInfos = this.markRunningStepsAsFailed({
|
||||
stepInfosToUpdate: workflowRunToUpdate.state?.stepInfos ?? {},
|
||||
});
|
||||
}
|
||||
updatedStepInfos = this.markRunningStepsAsFailed({
|
||||
stepInfosToUpdate: workflowRunToUpdate.state?.stepInfos ?? {},
|
||||
});
|
||||
|
||||
const partialUpdate = {
|
||||
status,
|
||||
@@ -213,7 +210,7 @@ export class WorkflowRunWorkspaceService {
|
||||
state: {
|
||||
...workflowRunToUpdate.state,
|
||||
workflowRunError: error,
|
||||
...(shouldUpdateStepInfos && { stepInfos: updatedStepInfos }),
|
||||
stepInfos: updatedStepInfos,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -223,7 +220,9 @@ export class WorkflowRunWorkspaceService {
|
||||
key:
|
||||
status === WorkflowRunStatus.COMPLETED
|
||||
? MetricsKeys.WorkflowRunCompleted
|
||||
: MetricsKeys.WorkflowRunFailed,
|
||||
: status === WorkflowRunStatus.STOPPED
|
||||
? MetricsKeys.WorkflowRunStopped
|
||||
: MetricsKeys.WorkflowRunFailed,
|
||||
eventId: workflowRunId,
|
||||
});
|
||||
}
|
||||
@@ -378,35 +377,7 @@ export class WorkflowRunWorkspaceService {
|
||||
return workflowRun;
|
||||
}
|
||||
|
||||
private getInitState(
|
||||
workflowVersion: WorkflowVersionWorkspaceEntity,
|
||||
triggerPayload: object,
|
||||
): WorkflowRunState | undefined {
|
||||
if (
|
||||
!isDefined(workflowVersion.trigger) ||
|
||||
!isDefined(workflowVersion.steps)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
flow: {
|
||||
trigger: workflowVersion.trigger,
|
||||
steps: workflowVersion.steps,
|
||||
},
|
||||
stepInfos: {
|
||||
trigger: { status: StepStatus.NOT_STARTED, result: triggerPayload },
|
||||
...Object.fromEntries(
|
||||
workflowVersion.steps.map((step) => [
|
||||
step.id,
|
||||
{ status: StepStatus.NOT_STARTED },
|
||||
]),
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private async updateWorkflowRun({
|
||||
async updateWorkflowRun({
|
||||
workflowRunId,
|
||||
workspaceId,
|
||||
partialUpdate,
|
||||
@@ -441,6 +412,34 @@ export class WorkflowRunWorkspaceService {
|
||||
);
|
||||
}
|
||||
|
||||
private getInitState(
|
||||
workflowVersion: WorkflowVersionWorkspaceEntity,
|
||||
triggerPayload: object,
|
||||
): WorkflowRunState | undefined {
|
||||
if (
|
||||
!isDefined(workflowVersion.trigger) ||
|
||||
!isDefined(workflowVersion.steps)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
flow: {
|
||||
trigger: workflowVersion.trigger,
|
||||
steps: workflowVersion.steps,
|
||||
},
|
||||
stepInfos: {
|
||||
trigger: { status: StepStatus.NOT_STARTED, result: triggerPayload },
|
||||
...Object.fromEntries(
|
||||
workflowVersion.steps.map((step) => [
|
||||
step.id,
|
||||
{ status: StepStatus.NOT_STARTED },
|
||||
]),
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private markRunningStepsAsFailed({
|
||||
stepInfosToUpdate,
|
||||
}: {
|
||||
|
||||
+53
@@ -14,9 +14,14 @@ import {
|
||||
WorkflowVersionStepExceptionCode,
|
||||
} from 'src/modules/workflow/common/exceptions/workflow-version-step.exception';
|
||||
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
|
||||
import { workflowHasRunningSteps } from 'src/modules/workflow/common/utils/workflow-has-running-steps.util';
|
||||
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
|
||||
import { WorkflowVersionStepOperationsWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-version-step/workflow-version-step-operations.workspace-service';
|
||||
import { isWorkflowFormAction } from 'src/modules/workflow/workflow-executor/workflow-actions/form/guards/is-workflow-form-action.guard';
|
||||
import {
|
||||
WorkflowRunException,
|
||||
WorkflowRunExceptionCode,
|
||||
} from 'src/modules/workflow/workflow-runner/exceptions/workflow-run.exception';
|
||||
import { RunWorkflowJob } from 'src/modules/workflow/workflow-runner/jobs/run-workflow.job';
|
||||
import { type RunWorkflowJobData } from 'src/modules/workflow/workflow-runner/types/run-workflow-job-data.type';
|
||||
import { WorkflowRunQueueWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run-queue/workspace-services/workflow-run-queue.workspace-service';
|
||||
@@ -191,4 +196,52 @@ export class WorkflowRunnerWorkspaceService {
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
|
||||
async stopWorkflowRun(workspaceId: string, workflowRunId: string) {
|
||||
const workflowRun =
|
||||
await this.workflowRunWorkspaceService.getWorkflowRunOrFail({
|
||||
workflowRunId,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
if (workflowRun.status !== WorkflowRunStatus.RUNNING) {
|
||||
throw new WorkflowRunException(
|
||||
'Workflow run is not running',
|
||||
WorkflowRunExceptionCode.INVALID_OPERATION,
|
||||
{
|
||||
userFriendlyMessage: msg`Workflow run is not running`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
let newStatus: WorkflowRunStatus;
|
||||
|
||||
if (
|
||||
workflowHasRunningSteps({
|
||||
stepInfos: workflowRun.state.stepInfos,
|
||||
steps: workflowRun.state.flow.steps,
|
||||
})
|
||||
) {
|
||||
await this.workflowRunWorkspaceService.updateWorkflowRun({
|
||||
workflowRunId,
|
||||
workspaceId,
|
||||
partialUpdate: {
|
||||
status: WorkflowRunStatus.STOPPING,
|
||||
},
|
||||
});
|
||||
newStatus = WorkflowRunStatus.STOPPING;
|
||||
} else {
|
||||
await this.workflowRunWorkspaceService.endWorkflowRun({
|
||||
workflowRunId,
|
||||
workspaceId,
|
||||
status: WorkflowRunStatus.STOPPED,
|
||||
});
|
||||
newStatus = WorkflowRunStatus.STOPPED;
|
||||
}
|
||||
|
||||
return {
|
||||
id: workflowRun.id,
|
||||
status: newStatus,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -142,6 +142,13 @@ export class WorkflowTriggerWorkspaceService {
|
||||
return true;
|
||||
}
|
||||
|
||||
async stopWorkflowRun(workflowRunId: string) {
|
||||
return this.workflowRunnerWorkspaceService.stopWorkflowRun(
|
||||
this.getWorkspaceId(),
|
||||
workflowRunId,
|
||||
);
|
||||
}
|
||||
|
||||
private async performActivationSteps(
|
||||
workflow: WorkflowWorkspaceEntity,
|
||||
workflowVersion: WorkflowVersionWorkspaceEntity,
|
||||
|
||||
Reference in New Issue
Block a user