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

Remove serverless functions on soft delete #9438

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 @@ -122,10 +122,10 @@ export class ServerlessFunctionResolver {
try {
await this.checkFeatureFlag(workspaceId);

return await this.serverlessFunctionService.deleteOneServerlessFunction(
input.id,
return await this.serverlessFunctionService.deleteOneServerlessFunction({
id: input.id,
workspaceId,
);
});
} catch (error) {
serverlessFunctionGraphQLApiExceptionHandler(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,15 @@ export class ServerlessFunctionService {
});
}

async deleteOneServerlessFunction(id: string, workspaceId: string) {
async deleteOneServerlessFunction({
id,
workspaceId,
isHardDeletion = true,
}: {
id: string;
workspaceId: string;
isHardDeletion?: boolean;
}) {
const existingServerlessFunction =
await this.serverlessFunctionRepository.findOneBy({
id,
Expand All @@ -219,16 +227,17 @@ export class ServerlessFunctionService {
);
}

await this.serverlessFunctionRepository.delete(id);
if (isHardDeletion) {
await this.serverlessFunctionRepository.delete(id);
await this.fileStorageService.delete({
folderPath: getServerlessFolder({
serverlessFunction: existingServerlessFunction,
}),
});
}

await this.serverlessService.delete(existingServerlessFunction);

await this.fileStorageService.delete({
folderPath: getServerlessFolder({
serverlessFunction: existingServerlessFunction,
}),
});

return existingServerlessFunction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export class WorkflowDeleteManyPostQueryHook
) {}

async execute(
_authContext: AuthContext,
authContext: AuthContext,
_objectName: string,
payload: WorkflowWorkspaceEntity[],
): Promise<void> {
this.workflowCommonWorkspaceService.cleanWorkflowsSubEntities(
payload.map((workflow) => workflow.id),
authContext.workspace.id,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export class WorkflowDeleteOnePostQueryHook
) {}

async execute(
_authContext: AuthContext,
authContext: AuthContext,
_objectName: string,
payload: WorkflowWorkspaceEntity[],
): Promise<void> {
this.workflowCommonWorkspaceService.cleanWorkflowsSubEntities(
payload.map((workflow) => workflow.id),
authContext.workspace.id,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import { WorkflowVersionUpdateManyPreQueryHook } from 'src/modules/workflow/comm
import { WorkflowVersionUpdateOnePreQueryHook } from 'src/modules/workflow/common/query-hooks/workflow-version-update-one.pre-query.hook';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
import { WorkflowVersionValidationWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-version-validation.workspace-service';
import { ServerlessFunctionModule } from 'src/engine/metadata-modules/serverless-function/serverless-function.module';

@Module({
imports: [
NestjsQueryTypeOrmModule.forFeature([ObjectMetadataEntity], 'metadata'),
ServerlessFunctionModule,
],
providers: [
WorkflowCreateOnePreQueryHook,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import {
WorkflowTriggerException,
WorkflowTriggerExceptionCode,
} from 'src/modules/workflow/workflow-trigger/exceptions/workflow-trigger.exception';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { WorkflowActionType } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
import { ServerlessFunctionService } from 'src/engine/metadata-modules/serverless-function/serverless-function.service';

@Injectable()
export class WorkflowCommonWorkspaceService {
constructor(private readonly twentyORMManager: TwentyORMManager) {}
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly serverlessFunctionService: ServerlessFunctionService,
) {}

async getWorkflowVersionOrFail(
workflowVersionId: string,
Expand Down Expand Up @@ -58,7 +64,10 @@ export class WorkflowCommonWorkspaceService {
return { ...workflowVersion, trigger: workflowVersion.trigger };
}

async cleanWorkflowsSubEntities(workflowIds: string[]): Promise<void> {
async cleanWorkflowsSubEntities(
workflowIds: string[],
workspaceId: string,
): Promise<void> {
const workflowVersionRepository =
await this.twentyORMManager.getRepository<WorkflowVersionWorkspaceEntity>(
'workflowVersion',
Expand All @@ -74,20 +83,48 @@ export class WorkflowCommonWorkspaceService {
'workflowEventListener',
);

Promise.all(
workflowIds.map((workflowId) => {
workflowEventListenerRepository.softDelete({
workflowId,
});

workflowRunRepository.softDelete({
workflowId,
});

workflowVersionRepository.softDelete({
workflowId,
});
}),
);
workflowIds.forEach((workflowId) => {
workflowEventListenerRepository.softDelete({
workflowId,
});

workflowRunRepository.softDelete({
workflowId,
});

workflowVersionRepository.softDelete({
workflowId,
});

this.deleteServerlessFunctions(
workflowVersionRepository,
workflowId,
workspaceId,
);
});
}

private async deleteServerlessFunctions(
workflowVersionRepository: WorkspaceRepository<WorkflowVersionWorkspaceEntity>,
workflowId: string,
workspaceId: string,
) {
const workflowVersions = await workflowVersionRepository.find({
where: {
workflowId,
},
});

workflowVersions.forEach((workflowVersion) => {
workflowVersion.steps?.forEach(async (step) => {
if (step.type === WorkflowActionType.CODE) {
await this.serverlessFunctionService.deleteOneServerlessFunction({
id: step.settings.input.serverlessFunctionId,
workspaceId,
isHardDeletion: false,
});
}
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,10 @@ export class WorkflowVersionStepWorkspaceService {
}) {
switch (step.type) {
case WorkflowActionType.CODE: {
await this.serverlessFunctionService.deleteOneServerlessFunction(
step.settings.input.serverlessFunctionId,
await this.serverlessFunctionService.deleteOneServerlessFunction({
id: step.settings.input.serverlessFunctionId,
workspaceId,
);
});

break;
}
Expand Down
Loading