-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdelayedRunSystem.ts
More file actions
213 lines (191 loc) · 6.41 KB
/
delayedRunSystem.ts
File metadata and controls
213 lines (191 loc) · 6.41 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { startSpan } from "@internal/tracing";
import { SystemResources } from "./systems.js";
import { PrismaClientOrTransaction, TaskRun } from "@trigger.dev/database";
import { getLatestExecutionSnapshot } from "./executionSnapshotSystem.js";
import { parseNaturalLanguageDuration } from "@trigger.dev/core/v3/isomorphic";
import { EnqueueSystem } from "./enqueueSystem.js";
import { ServiceValidationError } from "../errors.js";
export type DelayedRunSystemOptions = {
resources: SystemResources;
enqueueSystem: EnqueueSystem;
};
export class DelayedRunSystem {
private readonly $: SystemResources;
private readonly enqueueSystem: EnqueueSystem;
constructor(private readonly options: DelayedRunSystemOptions) {
this.$ = options.resources;
this.enqueueSystem = options.enqueueSystem;
}
/**
* Reschedules a delayed run where the run hasn't been queued yet
*/
async rescheduleDelayedRun({
runId,
delayUntil,
tx,
}: {
runId: string;
delayUntil: Date;
tx?: PrismaClientOrTransaction;
}): Promise<TaskRun> {
const prisma = tx ?? this.$.prisma;
return startSpan(
this.$.tracer,
"rescheduleDelayedRun",
async () => {
return await this.$.runLock.lock("rescheduleDelayedRun", [runId], async () => {
const snapshot = await getLatestExecutionSnapshot(prisma, runId);
// Check if the run is still in DELAYED status (or legacy RUN_CREATED for older runs)
if (
snapshot.executionStatus !== "DELAYED" &&
snapshot.executionStatus !== "RUN_CREATED"
) {
throw new ServiceValidationError("Cannot reschedule a run that is not delayed");
}
const updatedRun = await prisma.taskRun.update({
where: {
id: runId,
},
data: {
delayUntil: delayUntil,
executionSnapshots: {
create: {
engine: "V2",
executionStatus: "DELAYED",
description: "Delayed run was rescheduled to a future date",
runStatus: "DELAYED",
environmentId: snapshot.environmentId,
environmentType: snapshot.environmentType,
projectId: snapshot.projectId,
organizationId: snapshot.organizationId,
},
},
},
});
await this.$.worker.reschedule(`enqueueDelayedRun:${updatedRun.id}`, delayUntil);
this.$.eventBus.emit("runDelayRescheduled", {
time: new Date(),
run: {
id: updatedRun.id,
status: updatedRun.status,
delayUntil: delayUntil,
updatedAt: updatedRun.updatedAt,
createdAt: updatedRun.createdAt,
},
organization: {
id: snapshot.organizationId,
},
project: {
id: updatedRun.projectId,
},
environment: {
id: updatedRun.runtimeEnvironmentId,
},
});
return updatedRun;
});
},
{
attributes: { runId },
}
);
}
async enqueueDelayedRun({ runId }: { runId: string }) {
// Use lock to prevent race with debounce rescheduling
return await this.$.runLock.lock("enqueueDelayedRun", [runId], async () => {
// Check if run is still in DELAYED status before enqueuing
// This prevents a race where debounce reschedules the run while we're about to enqueue it
const snapshot = await getLatestExecutionSnapshot(this.$.prisma, runId);
if (snapshot.executionStatus !== "DELAYED" && snapshot.executionStatus !== "RUN_CREATED") {
this.$.logger.debug("enqueueDelayedRun: run is no longer delayed, skipping enqueue", {
runId,
executionStatus: snapshot.executionStatus,
});
return;
}
const run = await this.$.prisma.taskRun.findFirst({
where: { id: runId },
include: {
runtimeEnvironment: {
include: {
project: true,
organization: true,
},
},
},
});
if (!run) {
throw new Error(`#enqueueDelayedRun: run not found: ${runId}`);
}
// Check if delayUntil has been rescheduled to the future (e.g., by debounce)
// If so, don't enqueue - the rescheduled worker job will handle it
if (run.delayUntil && run.delayUntil > new Date()) {
this.$.logger.debug(
"enqueueDelayedRun: delay was rescheduled to the future, skipping enqueue",
{
runId,
delayUntil: run.delayUntil,
}
);
return;
}
// Now we need to enqueue the run into the RunQueue
// Skip the lock in enqueueRun since we already hold it
await this.enqueueSystem.enqueueRun({
run,
env: run.runtimeEnvironment,
batchId: run.batchId ?? undefined,
skipRunLock: true,
});
const queuedAt = new Date();
const updatedRun = await this.$.prisma.taskRun.update({
where: { id: runId },
data: {
status: "PENDING",
queuedAt,
},
});
this.$.eventBus.emit("runEnqueuedAfterDelay", {
time: new Date(),
run: {
id: runId,
status: "PENDING",
queuedAt,
updatedAt: updatedRun.updatedAt,
createdAt: updatedRun.createdAt,
},
organization: {
id: run.runtimeEnvironment.organizationId,
},
project: {
id: run.runtimeEnvironment.projectId,
},
environment: {
id: run.runtimeEnvironmentId,
},
});
if (run.ttl) {
const expireAt = parseNaturalLanguageDuration(run.ttl);
if (expireAt) {
await this.$.worker.enqueue({
id: `expireRun:${runId}`,
job: "expireRun",
payload: { runId },
availableAt: expireAt,
});
}
}
});
}
async scheduleDelayedRunEnqueuing({ runId, delayUntil }: { runId: string; delayUntil: Date }) {
await this.$.worker.enqueue({
id: `enqueueDelayedRun:${runId}`,
job: "enqueueDelayedRun",
payload: { runId },
availableAt: delayUntil,
});
}
async preventDelayedRunFromBeingEnqueued({ runId }: { runId: string }) {
await this.$.worker.ack(`enqueueDelayedRun:${runId}`);
}
}