-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconcurrencySystem.server.ts
More file actions
205 lines (187 loc) · 5.7 KB
/
concurrencySystem.server.ts
File metadata and controls
205 lines (187 loc) · 5.7 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
import { TaskQueue, User } from "@trigger.dev/database";
import { errAsync, fromPromise, okAsync } from "neverthrow";
import { PrismaClientOrTransaction } from "~/db.server";
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
import { removeQueueConcurrencyLimits, updateQueueConcurrencyLimits } from "../runQueue.server";
import { engine } from "../runEngine.server";
export type ConcurrencySystemOptions = {
db: PrismaClientOrTransaction;
reader: PrismaClientOrTransaction;
};
export type QueueInput = string | { type: "task" | "custom"; name: string };
export class ConcurrencySystem {
constructor(private readonly options: ConcurrencySystemOptions) {}
private get db() {
return this.options.db;
}
get queues() {
return {
overrideQueueConcurrencyLimit: (
environment: AuthenticatedEnvironment,
queue: QueueInput,
concurrencyLimit: number,
overriddenBy?: User
) => {
return findQueueFromInput(this.db, environment, queue)
.andThen((queue) =>
overrideQueueConcurrencyLimit(
this.db,
environment,
queue,
concurrencyLimit,
overriddenBy
)
)
.andThen((queue) => syncQueueConcurrencyToEngine(environment, queue))
.andThen((queue) => getQueueStats(environment, queue));
},
resetConcurrencyLimit: (environment: AuthenticatedEnvironment, queue: QueueInput) => {
return findQueueFromInput(this.db, environment, queue)
.andThen((queue) => resetQueueConcurrencyLimit(this.db, queue))
.andThen((queue) => syncQueueConcurrencyToEngine(environment, queue))
.andThen((queue) => getQueueStats(environment, queue));
},
};
}
}
function findQueueFromInput(
db: PrismaClientOrTransaction,
environment: AuthenticatedEnvironment,
queue: QueueInput
) {
if (typeof queue === "string") {
return findQueueByFriendlyId(db, environment, queue);
}
const queueName =
queue.type === "task" ? `task/${queue.name.replace(/^task\//, "")}` : queue.name;
return findQueueByName(db, environment, queueName);
}
function findQueueByFriendlyId(
db: PrismaClientOrTransaction,
environment: AuthenticatedEnvironment,
friendlyId: string
) {
return fromPromise(
db.taskQueue.findFirst({
where: {
runtimeEnvironmentId: environment.id,
friendlyId,
},
}),
(error) => ({
type: "other" as const,
cause: error,
})
).andThen((queue) => {
if (!queue) {
return errAsync({ type: "queue_not_found" as const });
}
return okAsync(queue);
});
}
function findQueueByName(
db: PrismaClientOrTransaction,
environment: AuthenticatedEnvironment,
queue: string
) {
return fromPromise(
db.taskQueue.findFirst({
where: {
runtimeEnvironmentId: environment.id,
name: queue,
},
}),
(error) => ({
type: "other" as const,
cause: error,
})
).andThen((queue) => {
if (!queue) {
return errAsync({ type: "queue_not_found" as const });
}
return okAsync(queue);
});
}
function overrideQueueConcurrencyLimit(
db: PrismaClientOrTransaction,
environment: AuthenticatedEnvironment,
queue: TaskQueue,
concurrencyLimit: number,
overriddenBy?: User
) {
const newConcurrencyLimit = Math.max(
Math.min(concurrencyLimit, environment.maximumConcurrencyLimit),
0
);
const concurrencyLimitBase = queue.concurrencyLimitOverriddenAt
? queue.concurrencyLimitBase
: queue.concurrencyLimit;
return fromPromise(
db.taskQueue.update({
where: {
id: queue.id,
},
data: {
concurrencyLimit: newConcurrencyLimit,
concurrencyLimitBase: concurrencyLimitBase ?? null,
concurrencyLimitOverriddenAt: new Date(),
concurrencyLimitOverriddenBy: overriddenBy?.id ?? null,
},
}),
(error) => ({
type: "queue_update_failed" as const,
cause: error,
})
);
}
function resetQueueConcurrencyLimit(db: PrismaClientOrTransaction, queue: TaskQueue) {
if (queue.concurrencyLimitOverriddenAt === null) {
return errAsync({ type: "queue_not_overridden" as const });
}
const newConcurrencyLimit = queue.concurrencyLimitBase;
return fromPromise(
db.taskQueue.update({
where: { id: queue.id },
data: {
concurrencyLimitOverriddenAt: null,
concurrencyLimit: newConcurrencyLimit,
concurrencyLimitBase: null,
concurrencyLimitOverriddenBy: null,
},
}),
(error) => ({
type: "queue_update_failed" as const,
cause: error,
})
);
}
function syncQueueConcurrencyToEngine(environment: AuthenticatedEnvironment, queue: TaskQueue) {
if (typeof queue.concurrencyLimit === "number") {
return fromPromise(
updateQueueConcurrencyLimits(environment, queue.name, queue.concurrencyLimit),
(error) => ({
type: "sync_queue_concurrency_to_engine_failed" as const,
cause: error,
})
).andThen(() => okAsync(queue));
} else {
return fromPromise(removeQueueConcurrencyLimits(environment, queue.name), (error) => ({
type: "sync_queue_concurrency_to_engine_failed" as const,
cause: error,
})).andThen(() => okAsync(queue));
}
}
function getQueueStats(environment: AuthenticatedEnvironment, queue: TaskQueue) {
return fromPromise(
Promise.all([
engine.lengthOfQueues(environment, [queue.name]),
engine.currentConcurrencyOfQueues(environment, [queue.name]),
]),
(error) => ({
type: "get_queue_stats_failed" as const,
cause: error,
})
).andThen(([queued, running]) =>
okAsync({ queued: queued[queue.name] ?? 0, running: running[queue.name] ?? 0, ...queue })
);
}