-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtimeoutDeployment.server.ts
More file actions
103 lines (94 loc) · 3.09 KB
/
Copy pathtimeoutDeployment.server.ts
File metadata and controls
103 lines (94 loc) · 3.09 KB
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
import { Prisma } from "@trigger.dev/database";
import { logger } from "~/services/logger.server";
import { BaseService } from "./baseService.server";
import { commonWorker } from "../commonWorker.server";
import { PerformDeploymentAlertsService } from "./alerts/performDeploymentAlerts.server";
import { type PrismaClientOrTransaction } from "~/db.server";
import { DeploymentService } from "./deployment.server";
import { recordDeploymentOutcome } from "./recordDeploymentOutcome.server";
export class TimeoutDeploymentService extends BaseService {
public async call(id: string, fromStatus: string, errorMessage: string) {
const deployment = await this._prisma.workerDeployment.findFirst({
where: {
id,
},
include: {
environment: {
include: {
project: true,
},
},
},
});
if (!deployment) {
logger.error(`No worker deployment with this ID: ${id}`);
return;
}
if (deployment.status !== fromStatus) {
// Race: timeout job fired after the deployment moved out of the
// expected state (already deployed/failed). System handles it by
// returning early — not an error.
logger.warn("Deployment is not in the correct state to be timed out", {
currentStatus: deployment.status,
fromStatus,
});
return;
}
const timedOutDeployment = await this._prisma.workerDeployment.update({
where: {
id: deployment.id,
},
data: {
status: "TIMED_OUT",
failedAt: new Date(),
errorData: { message: errorMessage, name: "TimeoutError" },
// Build env vars only live for the active build window
buildEnvVars: Prisma.DbNull,
},
});
recordDeploymentOutcome({
status: "TIMED_OUT",
deploymentFriendlyId: deployment.friendlyId,
organizationId: deployment.environment.project.organizationId,
projectId: deployment.environment.projectId,
environmentId: deployment.environmentId,
environmentType: deployment.environment.type,
reason: errorMessage,
});
const deploymentService = new DeploymentService();
await deploymentService
.appendToEventLog(deployment.environment.project, timedOutDeployment, [
{
type: "finalized",
data: {
result: "timed_out",
message: errorMessage,
},
},
])
.orTee((error) => {
logger.error("Failed to append timed out deployment event to event log", { error });
});
await PerformDeploymentAlertsService.enqueue(deployment.id);
}
static async enqueue(
deploymentId: string,
fromStatus: string,
errorMessage: string,
runAt: Date
) {
await commonWorker.enqueue({
id: `timeoutDeployment:${deploymentId}`,
job: "v3.timeoutDeployment",
payload: {
deploymentId,
fromStatus,
errorMessage,
},
availableAt: runAt,
});
}
static async dequeue(deploymentId: string, tx?: PrismaClientOrTransaction) {
await commonWorker.ack(`timeoutDeployment:${deploymentId}`);
}
}