-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathexecuteTasksWaitingForDeploy.ts
More file actions
117 lines (107 loc) · 3.21 KB
/
executeTasksWaitingForDeploy.ts
File metadata and controls
117 lines (107 loc) · 3.21 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { PrismaClientOrTransaction } from "~/db.server";
import { env } from "~/env.server";
import { logger } from "~/services/logger.server";
import { commonWorker } from "../commonWorker.server";
import { marqs } from "~/v3/marqs/index.server";
import { BaseService } from "./baseService.server";
export class ExecuteTasksWaitingForDeployService extends BaseService {
public async call(backgroundWorkerId: string) {
const backgroundWorker = await this._prisma.backgroundWorker.findFirst({
where: {
id: backgroundWorkerId,
},
include: {
runtimeEnvironment: {
include: {
project: true,
organization: true,
},
},
tasks: {
select: {
slug: true,
},
},
},
});
if (!backgroundWorker) {
logger.error("Background worker not found", { id: backgroundWorkerId });
return;
}
const maxCount = env.LEGACY_RUN_ENGINE_WAITING_FOR_DEPLOY_BATCH_SIZE;
const runsWaitingForDeploy = await this._replica.taskRun.findMany({
where: {
runtimeEnvironmentId: backgroundWorker.runtimeEnvironmentId,
projectId: backgroundWorker.projectId,
status: "WAITING_FOR_DEPLOY",
taskIdentifier: {
in: backgroundWorker.tasks.map((task) => task.slug),
},
},
orderBy: {
createdAt: "asc",
},
select: {
id: true,
status: true,
taskIdentifier: true,
concurrencyKey: true,
queue: true,
updatedAt: true,
createdAt: true,
},
take: maxCount + 1,
});
if (!runsWaitingForDeploy.length) {
return;
}
// Clear any runs awaiting deployment for execution
const pendingRuns = await this._prisma.taskRun.updateMany({
where: {
id: {
in: runsWaitingForDeploy.map((run) => run.id),
},
},
data: {
status: "PENDING",
},
});
if (pendingRuns.count) {
logger.debug("Task runs waiting for deploy are now ready for execution", {
tasks: runsWaitingForDeploy.map((run) => run.id),
total: pendingRuns.count,
});
}
for (const run of runsWaitingForDeploy) {
await marqs?.enqueueMessage(
backgroundWorker.runtimeEnvironment,
run.queue,
run.id,
{
type: "EXECUTE",
taskIdentifier: run.taskIdentifier,
projectId: backgroundWorker.runtimeEnvironment.projectId,
environmentId: backgroundWorker.runtimeEnvironment.id,
environmentType: backgroundWorker.runtimeEnvironment.type,
},
run.concurrencyKey ?? undefined
);
}
if (runsWaitingForDeploy.length > maxCount) {
await ExecuteTasksWaitingForDeployService.enqueue(
backgroundWorkerId,
new Date(Date.now() + env.LEGACY_RUN_ENGINE_WAITING_FOR_DEPLOY_BATCH_STAGGER_MS)
);
}
}
static async enqueue(backgroundWorkerId: string, runAt?: Date) {
return await commonWorker.enqueue({
id: `v3.executeTasksWaitingForDeploy:${backgroundWorkerId}`,
job: "v3.executeTasksWaitingForDeploy",
payload: {
backgroundWorkerId,
},
availableAt: runAt,
});
}
}