8725 workflow avoid serverless function autosave errors (#8916)
See issue #8725 - Build function asynchronously using a job - prevent useless builds - run promises simultaneously Todo: - fix outputSchema computing
This commit is contained in:
@ -0,0 +1,52 @@
|
||||
import { Scope } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
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 { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
|
||||
import { ServerlessService } from 'src/engine/core-modules/serverless/serverless.service';
|
||||
import { ServerlessFunctionEntity } from 'src/engine/metadata-modules/serverless-function/serverless-function.entity';
|
||||
import { isDefined } from 'src/utils/is-defined';
|
||||
|
||||
export type BuildServerlessFunctionBatchEvent = {
|
||||
serverlessFunctions: {
|
||||
serverlessFunctionId: string;
|
||||
serverlessFunctionVersion: string;
|
||||
}[];
|
||||
workspaceId: string;
|
||||
};
|
||||
|
||||
@Processor({
|
||||
queueName: MessageQueue.serverlessFunctionQueue,
|
||||
scope: Scope.REQUEST,
|
||||
})
|
||||
export class BuildServerlessFunctionJob {
|
||||
constructor(
|
||||
@InjectRepository(ServerlessFunctionEntity, 'metadata')
|
||||
private readonly serverlessFunctionRepository: Repository<ServerlessFunctionEntity>,
|
||||
private readonly serverlessService: ServerlessService,
|
||||
) {}
|
||||
|
||||
@Process(BuildServerlessFunctionJob.name)
|
||||
async handle(batchEvent: BuildServerlessFunctionBatchEvent): Promise<void> {
|
||||
for (const {
|
||||
serverlessFunctionId,
|
||||
serverlessFunctionVersion,
|
||||
} of batchEvent.serverlessFunctions) {
|
||||
const serverlessFunction =
|
||||
await this.serverlessFunctionRepository.findOneBy({
|
||||
id: serverlessFunctionId,
|
||||
workspaceId: batchEvent.workspaceId,
|
||||
});
|
||||
|
||||
if (isDefined(serverlessFunction)) {
|
||||
await this.serverlessService.build(
|
||||
serverlessFunction,
|
||||
serverlessFunctionVersion,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
import { Scope } from '@nestjs/common';
|
||||
|
||||
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 { ServerlessFunctionService } from 'src/engine/metadata-modules/serverless-function/serverless-function.service';
|
||||
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
|
||||
|
||||
export type DeleteServerlessFunctionBatchEvent = {
|
||||
ids: string[];
|
||||
workspaceId: string;
|
||||
};
|
||||
|
||||
@Processor({
|
||||
queueName: MessageQueue.serverlessFunctionQueue,
|
||||
scope: Scope.REQUEST,
|
||||
})
|
||||
export class DeleteServerlessFunctionJob {
|
||||
constructor(
|
||||
private readonly serverlessFunctionService: ServerlessFunctionService,
|
||||
) {}
|
||||
|
||||
@Process(DeleteServerlessFunctionJob.name)
|
||||
async handle(batchEvent: DeleteServerlessFunctionBatchEvent): Promise<void> {
|
||||
await Promise.all(
|
||||
batchEvent.ids.map((id) =>
|
||||
this.serverlessFunctionService.deleteOneServerlessFunction(
|
||||
id,
|
||||
batchEvent.workspaceId,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user