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

Migrate to workspace services #6628

Merged
merged 1 commit into from
Aug 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Module } from '@nestjs/common';
import { WorkflowTriggerResolver } from 'src/engine/core-modules/workflow/workflow-trigger.resolver';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module';
import { WorkflowTriggerService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.service';
import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service';

@Module({
imports: [WorkflowCommonModule, WorkflowRunnerModule],
providers: [WorkflowTriggerService, WorkflowTriggerResolver],
providers: [WorkflowTriggerWorkspaceService, WorkflowTriggerResolver],
})
export class WorkflowTriggerCoreModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { RunWorkflowVersionInput } from 'src/engine/core-modules/workflow/dtos/run-workflow-version-input.dto';
import { WorkflowTriggerResultDTO } from 'src/engine/core-modules/workflow/dtos/workflow-trigger-result.dto';
import { workflowTriggerGraphqlApiExceptionHandler } from 'src/engine/core-modules/workflow/utils/workflow-trigger-graphql-api-exception-handler.util';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
import { WorkflowTriggerService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.service';
import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service';

@UseGuards(JwtAuthGuard)
@Resolver()
export class WorkflowTriggerResolver {
constructor(
private readonly workflowTriggerService: WorkflowTriggerService,
private readonly workflowTriggerWorkspaceService: WorkflowTriggerWorkspaceService,
) {}

@Mutation(() => Boolean)
async enableWorkflowTrigger(
@AuthWorkspace() { id: workspaceId }: Workspace,
@Args('workflowVersionId') workflowVersionId: string,
) {
Comment on lines 18 to 20
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: workspaceId parameter removed. Ensure workspace context is correctly handled by WorkflowTriggerWorkspaceService

try {
return await this.workflowTriggerService.enableWorkflowTrigger(
workspaceId,
return await this.workflowTriggerWorkspaceService.enableWorkflowTrigger(
workflowVersionId,
);
} catch (error) {
Expand All @@ -33,13 +29,11 @@ export class WorkflowTriggerResolver {

@Mutation(() => WorkflowTriggerResultDTO)
async runWorkflowVersion(
@AuthWorkspace() { id: workspaceId }: Workspace,
@Args('input') { workflowVersionId, payload }: RunWorkflowVersionInput,
) {
Comment on lines 31 to 33
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: workspaceId parameter removed here as well. Verify that WorkflowTriggerWorkspaceService correctly identifies the workspace context

try {
return {
result: await this.workflowTriggerService.runWorkflowVersion(
workspaceId,
result: await this.workflowTriggerWorkspaceService.runWorkflowVersion(
workflowVersionId,
payload ?? {},
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Module } from '@nestjs/common';

import { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';

@Module({
providers: [WorkflowCommonService],
exports: [WorkflowCommonService],
providers: [WorkflowCommonWorkspaceService],
exports: [WorkflowCommonWorkspaceService],
})
export class WorkflowCommonModule {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';

import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { WorkflowTrigger } from 'src/modules/workflow/common/types/workflow-trigger.type';
import {
Expand All @@ -9,22 +9,16 @@ import {
} from 'src/modules/workflow/workflow-trigger/workflow-trigger.exception';

@Injectable()
export class WorkflowCommonService {
constructor(
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {}
export class WorkflowCommonWorkspaceService {
constructor(private readonly twentyORMManager: TwentyORMManager) {}

async getWorkflowVersion(
workspaceId: string,
workflowVersionId: string,
): Promise<
async getWorkflowVersion(workflowVersionId: string): Promise<
Omit<WorkflowVersionWorkspaceEntity, 'trigger'> & {
trigger: WorkflowTrigger;
}
> {
const workflowVersionRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowVersionWorkspaceEntity>(
workspaceId,
await this.twentyORMManager.getRepository<WorkflowVersionWorkspaceEntity>(
'workflowVersion',
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CustomException } from 'src/utils/custom-exception';
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider using a relative import path for better maintainability


export class WorkflowActionRunnerException extends CustomException {
code: WorkflowActionRunnerExceptionCode;
Copy link
Contributor

Choose a reason for hiding this comment

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

style: The code property is redundant as it's already defined in the constructor parameter

constructor(message: string, code: WorkflowActionRunnerExceptionCode) {
super(message, code);
}
}

export enum WorkflowActionRunnerExceptionCode {
SCOPED_WORKSPACE_NOT_FOUND = 'SCOPED_WORKSPACE_NOT_FOUND',
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type';
import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type';

export interface WorkflowActionRunner {
execute({
action,
workspaceId,
payload,
}: {
action: WorkflowAction;
workspaceId: string;
payload?: object;
}): Promise<WorkflowResult>;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Module } from '@nestjs/common';

import { ServerlessFunctionModule } from 'src/engine/metadata-modules/serverless-function/serverless-function.module';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowActionRunnerFactory } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.factory';
import { CodeWorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runners/code-workflow-action-runner';
import { ServerlessFunctionModule } from 'src/engine/metadata-modules/serverless-function/serverless-function.module';

@Module({
imports: [ServerlessFunctionModule],
providers: [WorkflowActionRunnerFactory, CodeWorkflowActionRunner],
providers: [
WorkflowActionRunnerFactory,
CodeWorkflowActionRunner,
ScopedWorkspaceContextFactory,
],
exports: [WorkflowActionRunnerFactory],
})
export class WorkflowActionRunnerModule {}
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { Injectable } from '@nestjs/common';

import { WorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.interface';
import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { ServerlessFunctionService } from 'src/engine/metadata-modules/serverless-function/serverless-function.service';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type';
import {
WorkflowActionRunnerException,
WorkflowActionRunnerExceptionCode,
} from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.exception';
import { WorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.interface';

@Injectable()
export class CodeWorkflowActionRunner implements WorkflowActionRunner {
constructor(
private readonly serverlessFunctionService: ServerlessFunctionService,
private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory,
) {}

async execute({
action,
workspaceId,
payload,
}: {
action: WorkflowAction;
workspaceId: string;
payload?: object;
}): Promise<WorkflowResult> {
const { workspaceId } = this.scopedWorkspaceContextFactory.create();

if (!workspaceId) {
throw new WorkflowActionRunnerException(
'Scoped workspace not found',
WorkflowActionRunnerExceptionCode.SCOPED_WORKSPACE_NOT_FOUND,
);
}

const result = await this.serverlessFunctionService.executeOne(
action.settings.serverlessFunctionId,
workspaceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Process } from 'src/engine/integrations/message-queue/decorators/proces
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services';
import { WorkflowRunnerService } from 'src/modules/workflow/workflow-runner/workflow-runner.service';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';
import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service';
import { WorkflowStatusWorkspaceService } from 'src/modules/workflow/workflow-status/workflow-status.workspace-service';

export type RunWorkflowJobData = {
Expand All @@ -18,29 +18,27 @@ export type RunWorkflowJobData = {
@Processor({ queueName: MessageQueue.workflowQueue, scope: Scope.REQUEST })
export class WorkflowRunnerJob {
constructor(
private readonly workflowCommonService: WorkflowCommonService,
private readonly workflowRunnerService: WorkflowRunnerService,
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService,
private readonly workflowStatusWorkspaceService: WorkflowStatusWorkspaceService,
) {}

@Process(WorkflowRunnerJob.name)
async handle({
workspaceId,
workflowVersionId,
workflowRunId,
payload,
}: RunWorkflowJobData): Promise<void> {
await this.workflowStatusWorkspaceService.startWorkflowRun(workflowRunId);

const workflowVersion = await this.workflowCommonService.getWorkflowVersion(
workspaceId,
workflowVersionId,
);
const workflowVersion =
await this.workflowCommonWorkspaceService.getWorkflowVersion(
workflowVersionId,
);

try {
await this.workflowRunnerService.run({
await this.workflowRunnerWorkspaceService.run({
action: workflowVersion.trigger.nextAction,
workspaceId,
payload,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Module } from '@nestjs/common';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { WorkflowActionRunnerModule } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.module';
import { WorkflowRunnerJob } from 'src/modules/workflow/workflow-runner/workflow-runner.job';
import { WorkflowRunnerService } from 'src/modules/workflow/workflow-runner/workflow-runner.service';
import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service';
import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workflow-status.module';

@Module({
Expand All @@ -12,7 +12,7 @@ import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workf
WorkflowActionRunnerModule,
WorkflowStatusModule,
],
providers: [WorkflowRunnerService, WorkflowRunnerJob],
exports: [WorkflowRunnerService],
providers: [WorkflowRunnerWorkspaceService, WorkflowRunnerJob],
exports: [WorkflowRunnerWorkspaceService],
})
export class WorkflowRunnerModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ export type WorkflowRunOutput = {
};

@Injectable()
export class WorkflowRunnerService {
export class WorkflowRunnerWorkspaceService {
constructor(
private readonly workflowActionRunnerFactory: WorkflowActionRunnerFactory,
) {}

async run({
action,
workspaceId,
payload,
attemptCount = 1,
}: {
action?: WorkflowAction;
workspaceId: string;
payload?: object;
attemptCount?: number;
}): Promise<WorkflowRunOutput> {
Expand All @@ -43,14 +41,12 @@ export class WorkflowRunnerService {

const result = await workflowActionRunner.execute({
action,
workspaceId,
payload,
});

if (result.data) {
return await this.run({
action: action.nextAction,
workspaceId,
payload: result.data,
});
}
Expand All @@ -65,7 +61,6 @@ export class WorkflowRunnerService {
if (action.settings.errorHandlingOptions.continueOnFailure.value) {
return await this.run({
action: action.nextAction,
workspaceId,
payload,
});
}
Expand All @@ -76,7 +71,6 @@ export class WorkflowRunnerService {
) {
return await this.run({
action,
workspaceId,
payload,
attemptCount: attemptCount + 1,
});
Expand Down
Loading
Loading