-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTestTaskPresenter.server.ts
More file actions
217 lines (199 loc) · 5.35 KB
/
TestTaskPresenter.server.ts
File metadata and controls
217 lines (199 loc) · 5.35 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
214
215
216
217
import { ScheduledTaskPayload, parsePacket, prettyPrintPacket } from "@trigger.dev/core/v3";
import { type RuntimeEnvironmentType, type TaskRunStatus } from "@trigger.dev/database";
import { type PrismaClient, prisma, sqlDatabaseSchema } from "~/db.server";
import { getTimezones } from "~/utils/timezones.server";
import {
type BackgroundWorkerTaskSlim,
findCurrentWorkerDeployment,
} from "~/v3/models/workerDeployment.server";
type TestTaskOptions = {
userId: string;
projectId: string;
environment: {
id: string;
type: RuntimeEnvironmentType;
};
taskIdentifier: string;
};
type Task = {
id: string;
taskIdentifier: string;
filePath: string;
friendlyId: string;
};
export type TestTask =
| {
triggerSource: "STANDARD";
task: Task;
runs: StandardRun[];
}
| {
triggerSource: "SCHEDULED";
task: Task;
possibleTimezones: string[];
runs: ScheduledRun[];
};
export type TestTaskResult =
| {
foundTask: true;
task: TestTask;
}
| {
foundTask: false;
};
type RawRun = {
id: string;
number: BigInt;
friendlyId: string;
createdAt: Date;
status: TaskRunStatus;
payload: string;
payloadType: string;
runtimeEnvironmentId: string;
seedMetadata?: string;
seedMetadataType?: string;
};
export type StandardRun = Omit<RawRun, "number"> & {
number: number;
};
export type ScheduledRun = Omit<RawRun, "number" | "payload"> & {
number: number;
payload: {
timestamp: Date;
lastTimestamp?: Date;
externalId?: string;
timezone: string;
};
};
export class TestTaskPresenter {
#prismaClient: PrismaClient;
constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}
public async call({
userId,
projectId,
environment,
taskIdentifier,
}: TestTaskOptions): Promise<TestTaskResult> {
let task: BackgroundWorkerTaskSlim | null = null;
if (environment.type !== "DEVELOPMENT") {
const deployment = await findCurrentWorkerDeployment({ environmentId: environment.id });
if (deployment) {
task = deployment.worker?.tasks.find((t) => t.slug === taskIdentifier) ?? null;
}
} else {
task = await this.#prismaClient.backgroundWorkerTask.findFirst({
where: {
slug: taskIdentifier,
runtimeEnvironmentId: environment.id,
},
orderBy: {
createdAt: "desc",
},
});
}
if (!task) {
return {
foundTask: false,
};
}
const latestRuns = await this.#prismaClient.$queryRaw<RawRun[]>`
WITH taskruns AS (
SELECT
tr.*
FROM
${sqlDatabaseSchema}."TaskRun" as tr
JOIN
${sqlDatabaseSchema}."BackgroundWorkerTask" as bwt
ON
tr."taskIdentifier" = bwt.slug
WHERE
bwt."friendlyId" = ${task.friendlyId} AND
tr."runtimeEnvironmentId" = ${environment.id}
ORDER BY
tr."createdAt" DESC
LIMIT 5
)
SELECT
taskr.id,
taskr.number,
taskr."friendlyId",
taskr."taskIdentifier",
taskr."createdAt",
taskr.status,
taskr.payload,
taskr."payloadType",
taskr."seedMetadata",
taskr."seedMetadataType",
taskr."runtimeEnvironmentId"
FROM
taskruns AS taskr
WHERE
taskr."payloadType" = 'application/json' OR taskr."payloadType" = 'application/super+json'
ORDER BY
taskr."createdAt" DESC;`;
const taskWithEnvironment = {
id: task.id,
taskIdentifier: task.slug,
filePath: task.filePath,
friendlyId: task.friendlyId,
};
switch (task.triggerSource) {
case "STANDARD":
return {
foundTask: true,
task: {
triggerSource: "STANDARD",
task: taskWithEnvironment,
runs: await Promise.all(
latestRuns.map(async (r) => {
const number = Number(r.number);
return {
...r,
number,
payload: await prettyPrintPacket(r.payload, r.payloadType),
metadata: r.seedMetadata
? await prettyPrintPacket(r.seedMetadata, r.seedMetadataType)
: undefined,
};
})
),
},
};
case "SCHEDULED":
const possibleTimezones = getTimezones();
return {
foundTask: true,
task: {
triggerSource: "SCHEDULED",
task: taskWithEnvironment,
possibleTimezones,
runs: (
await Promise.all(
latestRuns.map(async (r) => {
const number = Number(r.number);
const payload = await getScheduleTaskRunPayload(r);
if (payload.success) {
return {
...r,
number,
payload: payload.data,
};
}
})
)
).filter(Boolean),
},
};
}
}
}
async function getScheduleTaskRunPayload(run: RawRun) {
const payload = await parsePacket({ data: run.payload, dataType: run.payloadType });
if (!payload.timezone) {
payload.timezone = "UTC";
}
const parsed = ScheduledTaskPayload.safeParse(payload);
return parsed;
}