-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpendingVersionSystem.ts
More file actions
142 lines (128 loc) · 4.26 KB
/
Copy pathpendingVersionSystem.ts
File metadata and controls
142 lines (128 loc) · 4.26 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { EnqueueSystem } from "./enqueueSystem.js";
import { SystemResources } from "./systems.js";
export type PendingVersionSystemOptions = {
resources: SystemResources;
enqueueSystem: EnqueueSystem;
queueRunsPendingVersionBatchSize?: number;
};
export class PendingVersionSystem {
private readonly $: SystemResources;
private readonly enqueueSystem: EnqueueSystem;
constructor(private readonly options: PendingVersionSystemOptions) {
this.$ = options.resources;
this.enqueueSystem = options.enqueueSystem;
}
async enqueueRunsForBackgroundWorker(backgroundWorkerId: string) {
//It could be a lot of runs, so we will process them in a batch
//if there are still more to process we will enqueue this function again
const maxCount = this.options.queueRunsPendingVersionBatchSize ?? 200;
const backgroundWorker = await this.$.prisma.backgroundWorker.findFirst({
where: {
id: backgroundWorkerId,
},
include: {
runtimeEnvironment: {
include: {
project: true,
organization: true,
},
},
tasks: true,
queues: true,
},
});
if (!backgroundWorker) {
this.$.logger.error("#enqueueRunsForBackgroundWorker: background worker not found", {
id: backgroundWorkerId,
});
return;
}
this.$.logger.debug("Finding PENDING_VERSION runs for background worker", {
workerId: backgroundWorker.id,
taskIdentifiers: backgroundWorker.tasks.map((task) => task.slug),
queues: backgroundWorker.queues.map((queue) => queue.name),
});
const pendingRuns = await this.$.readOnlyPrisma.taskRun.findMany({
where: {
runtimeEnvironmentId: backgroundWorker.runtimeEnvironmentId,
projectId: backgroundWorker.projectId,
status: "PENDING_VERSION",
taskIdentifier: {
in: backgroundWorker.tasks.map((task) => task.slug),
},
queue: {
in: backgroundWorker.queues.map((queue) => queue.name),
},
},
orderBy: {
createdAt: "asc",
},
take: maxCount + 1,
});
//none to process
if (!pendingRuns.length) return;
this.$.logger.debug("Enqueueing PENDING_VERSION runs for background worker", {
workerId: backgroundWorker.id,
taskIdentifiers: pendingRuns.map((run) => run.taskIdentifier),
queues: pendingRuns.map((run) => run.queue),
runs: pendingRuns.map((run) => ({
id: run.id,
taskIdentifier: run.taskIdentifier,
queue: run.queue,
createdAt: run.createdAt,
priorityMs: run.priorityMs,
})),
});
for (const run of pendingRuns) {
await this.$.prisma.$transaction(async (tx) => {
const updatedRun = await tx.taskRun.update({
where: {
id: run.id,
},
data: {
status: "PENDING",
},
});
await this.enqueueSystem.enqueueRun({
run: updatedRun,
env: backgroundWorker.runtimeEnvironment,
tx,
// PENDING_VERSION re-enqueue is the first time this run is actually
// entering the run queue (the original enqueue was held back waiting
// for a worker version). Arm TTL here so the TTL system can expire it
// if it sits queued waiting on a concurrency slot.
includeTtl: true,
});
});
this.$.eventBus.emit("runStatusChanged", {
time: new Date(),
run: {
id: run.id,
status: "PENDING",
updatedAt: run.updatedAt,
createdAt: run.createdAt,
},
organization: {
id: backgroundWorker.runtimeEnvironment.organizationId,
},
project: {
id: backgroundWorker.runtimeEnvironment.projectId,
},
environment: {
id: backgroundWorker.runtimeEnvironmentId,
},
});
}
//enqueue more if needed
if (pendingRuns.length > maxCount) {
await this.scheduleResolvePendingVersionRuns(backgroundWorkerId);
}
}
async scheduleResolvePendingVersionRuns(backgroundWorkerId: string): Promise<void> {
//we want this to happen in the background
await this.$.worker.enqueue({
job: "queueRunsPendingVersion",
payload: { backgroundWorkerId },
});
}
}