Trigger workflow run manually (#6696)

Fix https://github.com/twentyhq/twenty/issues/6669

- create a commun function `startWorkflowRun` that both create the run
object and the job for executing the workflow
- use it in both the `workflowEventJob` and the `runWorkflowVersion`
endpoint

Bonus:
- use filtering for exceptions instead of a util. It avoids doing a try
catch in all endpoint
This commit is contained in:
Thomas Trompette
2024-08-21 17:41:26 +02:00
committed by GitHub
parent da5dfb7a5b
commit 663acd56e4
43 changed files with 452 additions and 316 deletions

View File

@ -1,18 +1,16 @@
import { Scope } from '@nestjs/common';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service';
import {
RunWorkflowJobData,
WorkflowRunnerJob,
} from 'src/modules/workflow/workflow-runner/workflow-runner.job';
import { WorkflowStatusWorkspaceService } from 'src/modules/workflow/workflow-status/workflow-status.workspace-service';
WorkflowTriggerException,
WorkflowTriggerExceptionCode,
} from 'src/modules/workflow/workflow-trigger/workflow-trigger.exception';
export type WorkflowEventTriggerJobData = {
workspaceId: string;
@ -23,10 +21,8 @@ export type WorkflowEventTriggerJobData = {
@Processor({ queueName: MessageQueue.workflowQueue, scope: Scope.REQUEST })
export class WorkflowEventTriggerJob {
constructor(
@InjectMessageQueue(MessageQueue.workflowQueue)
private readonly messageQueueService: MessageQueueService,
private readonly twentyORMManager: TwentyORMManager,
private readonly workflowStatusWorkspaceService: WorkflowStatusWorkspaceService,
private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService,
) {}
@Process(WorkflowEventTriggerJob.name)
@ -41,23 +37,20 @@ export class WorkflowEventTriggerJob {
});
if (!workflow.publishedVersionId) {
throw new Error('Workflow has no published version');
throw new WorkflowTriggerException(
'Workflow has no published version',
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
);
}
const workflowRunId =
await this.workflowStatusWorkspaceService.createWorkflowRun(
workflow.publishedVersionId,
{
source: FieldActorSource.WORKFLOW,
name: workflow.name,
},
);
this.messageQueueService.add<RunWorkflowJobData>(WorkflowRunnerJob.name, {
workspaceId: data.workspaceId,
workflowVersionId: workflow.publishedVersionId,
payload: data.payload,
workflowRunId,
});
await this.workflowRunnerWorkspaceService.run(
data.workspaceId,
workflow.publishedVersionId,
data.payload,
{
source: FieldActorSource.WORKFLOW,
name: workflow.name,
},
);
}
}

View File

@ -1,11 +0,0 @@
import { Module } from '@nestjs/common';
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module';
import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workflow-status.module';
import { WorkflowEventTriggerJob } from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job';
@Module({
imports: [WorkflowRunnerModule, WorkflowStatusModule],
providers: [WorkflowEventTriggerJob],
})
export class WorkflowTriggerJobModule {}

View File

@ -1,10 +0,0 @@
import { Module } from '@nestjs/common';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trigger/listeners/database-event-trigger.listener';
@Module({
imports: [FeatureFlagModule],
providers: [DatabaseEventTriggerListener],
})
export class WorkflowTriggerListenerModule {}

View File

@ -1,9 +1,21 @@
import { Module } from '@nestjs/common';
import { WorkflowTriggerJobModule } from 'src/modules/workflow/workflow-trigger/jobs/workflow-trigger-job.module';
import { WorkflowTriggerListenerModule } from 'src/modules/workflow/workflow-trigger/listeners/workflow-trigger-listener.module';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module';
import { WorkflowEventTriggerJob } from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job';
import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trigger/listeners/database-event-trigger.listener';
import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service';
@Module({
imports: [WorkflowTriggerJobModule, WorkflowTriggerListenerModule],
imports: [WorkflowCommonModule, WorkflowRunnerModule, FeatureFlagModule],
providers: [
WorkflowTriggerWorkspaceService,
ScopedWorkspaceContextFactory,
DatabaseEventTriggerListener,
WorkflowEventTriggerJob,
],
exports: [WorkflowTriggerWorkspaceService],
})
export class WorkflowTriggerModule {}

View File

@ -1,5 +1,8 @@
import { Injectable } from '@nestjs/common';
import { buildCreatedByFromWorkspaceMember } from 'src/engine/core-modules/actor/utils/build-created-by-from-workspace-member.util';
import { User } from 'src/engine/core-modules/user/user.entity';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity';
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
@ -19,26 +22,43 @@ export class WorkflowTriggerWorkspaceService {
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory,
private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService,
) {}
async runWorkflowVersion(workflowVersionId: string, payload: object) {
async runWorkflowVersion(
workflowVersionId: string,
payload: object,
workspaceMemberId: string,
user: User,
) {
const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId;
if (!workspaceId) {
throw new WorkflowTriggerException(
'No workspace id found',
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
);
}
const workflowVersion =
await this.workflowCommonWorkspaceService.getWorkflowVersion(
workflowVersionId,
);
try {
return await this.workflowRunnerWorkspaceService.run({
action: workflowVersion.trigger.nextAction,
payload,
});
} catch (error) {
if (!workflowVersion) {
throw new WorkflowTriggerException(
`Error running workflow version ${error}`,
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
'No workflow version found',
WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION,
);
}
return await this.workflowRunnerWorkspaceService.run(
workspaceId,
workflowVersionId,
payload,
buildCreatedByFromWorkspaceMember(workspaceMemberId, user),
);
}
async enableWorkflowTrigger(workflowVersionId: string) {