File size: 4,171 Bytes
4e83772 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 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<void> {
//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;
|