import { deploymentService, logsService, projectService } from "@/instances.js"; import { DeploymentLogEvent, DeploymentUpdatesEvent } from "@/events/schemas/deployment.schema.js"; import { deploymentEmitter } from "@/events/deploymentEmitter.js"; import { UpdateTypes } from "@/events/types/event.js"; import { ProjectStatus } from "@/models/Projects.js"; import { ILogs } from "@/models/Logs.js"; class DeploymentEventHandler { static handleLogsBatch(events: ILogs[]): void { process.stdout.write(` L-${events.length}`); logsService.__insertToBatch(events); } static async handlerMessageEmitting(event: DeploymentLogEvent) { const { data } = event; const { log, deploymentId, projectId } = data; deploymentEmitter.emitEvents( deploymentId, { event_id: event.eventId, deployment_id: deploymentId, project_id: projectId, ...log, }, "LOG", ); } static async handleUpdates(event: DeploymentUpdatesEvent, isRetry: boolean): Promise { //service call const { data } = event; const { updates, deploymentId, projectId } = data; process.stdout.write(" U "); if (!isRetry) { deploymentEmitter.emitEvents( deploymentId, { ...updates, deploymentId, projectId, }, "UPDATE", ); } switch (data.updateType) { case UpdateTypes.START: { await Promise.all([ deploymentService.__updateDeployment(projectId, deploymentId, { status: updates.status, commit_hash: updates.commit_hash, }), projectService.__updateProjectById(projectId, { status: updates.status as unknown as ProjectStatus, }), ]); break; } case UpdateTypes.END: { await Promise.all([ deploymentService.decrementRunningDeplymnts(projectId, updates.user || ""), deploymentService.__updateDeployment(projectId, deploymentId, { status: updates.status, complete_at: updates.complete_at ? new Date(updates.complete_at) : new Date(), ...(updates.commit_hash && { commit_hash: updates.commit_hash }), timings: { install_ms: updates.install_ms || 0, build_ms: updates.build_ms || 0, upload_ms: updates.upload_ms || 0, duration_ms: updates.duration_ms || 0, }, file_structure: { files: updates.file_structure?.files || [], totalSize: updates.file_structure?.totalSize || 0, }, }), projectService.__updateProjectById(projectId, { status: updates.status as unknown as ProjectStatus, techStack: updates.techStack, tempDeployment: null, ...(updates.status === "READY" && !updates.preventAutoPromoteDeployment && { currentDeployment: deploymentId }), }), ]); break; } case UpdateTypes.ERROR: { await Promise.all([ deploymentService.decrementRunningDeplymnts(projectId, updates.user || ""), deploymentService.__updateDeployment(projectId, deploymentId, { status: updates.status, error_message: updates.error_message, complete_at: updates.complete_at ? new Date(updates.complete_at) : new Date(), }), projectService.__updateProjectById( projectId, { status: updates.status as unknown as ProjectStatus, tempDeployment: null, }, { updateStatusOnlyIfNoCurrentDeployment: true }, ), ]); break; } case UpdateTypes.CUSTOM: { await Promise.all([ deploymentService.__updateDeployment(projectId, deploymentId, { status: updates.status, commit_hash: updates.commit_hash, error_message: updates.error_message, timings: { install_ms: updates.install_ms || 0, build_ms: updates.build_ms || 0, upload_ms: updates.upload_ms || 0, duration_ms: updates.duration_ms || 0, }, file_structure: updates.file_structure, complete_at: new Date(updates.complete_at || ""), }), projectService.__updateProjectById(projectId, { status: updates.status as unknown as ProjectStatus, techStack: updates.techStack, }), ]); break; } default: { console.error("Undefined Update type"); } } } } export default DeploymentEventHandler;