-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdelayedRunSystem.ts
More file actions
134 lines (119 loc) · 3.78 KB
/
delayedRunSystem.ts
File metadata and controls
134 lines (119 loc) · 3.78 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
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], 5_000, async () => {
const snapshot = await getLatestExecutionSnapshot(prisma, runId);
//if the run isn't just created then we can't reschedule it
if (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: "RUN_CREATED",
description: "Delayed run was rescheduled to a future date",
runStatus: "EXPIRED",
environmentId: snapshot.environmentId,
environmentType: snapshot.environmentType,
projectId: snapshot.projectId,
organizationId: snapshot.organizationId,
},
},
},
});
await this.$.worker.reschedule(`enqueueDelayedRun:${updatedRun.id}`, delayUntil);
return updatedRun;
});
},
{
attributes: { runId },
}
);
}
async enqueueDelayedRun({ runId }: { runId: string }) {
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}`);
}
// Now we need to enqueue the run into the RunQueue
await this.enqueueSystem.enqueueRun({
run,
env: run.runtimeEnvironment,
batchId: run.batchId ?? undefined,
});
await this.$.prisma.taskRun.update({
where: { id: runId },
data: {
status: "PENDING",
queuedAt: new Date(),
},
});
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,
});
}
}