Skip to content

Commit 6dac1a2

Browse files
committed
Cleanup the task hierarchy, share more code
1 parent 0b984a5 commit 6dac1a2

File tree

3 files changed

+117
-224
lines changed

3 files changed

+117
-224
lines changed

apps/webapp/app/presenters/v3/ApiRetrieveRunPresenter.server.ts

Lines changed: 88 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
RunStatus,
55
SerializedError,
66
TaskRunError,
7+
TriggerFunction,
78
conditionallyImportPacket,
89
createJsonErrorObject,
910
logger,
@@ -14,6 +15,47 @@ import assertNever from "assert-never";
1415
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
1516
import { generatePresignedUrl } from "~/v3/r2.server";
1617
import { BasePresenter } from "./basePresenter.server";
18+
import { prisma } from "~/db.server";
19+
20+
// Build 'select' object
21+
const commonRunSelect = {
22+
id: true,
23+
friendlyId: true,
24+
status: true,
25+
taskIdentifier: true,
26+
createdAt: true,
27+
startedAt: true,
28+
updatedAt: true,
29+
completedAt: true,
30+
expiredAt: true,
31+
delayUntil: true,
32+
ttl: true,
33+
tags: true,
34+
costInCents: true,
35+
baseCostInCents: true,
36+
usageDurationMs: true,
37+
idempotencyKey: true,
38+
isTest: true,
39+
depth: true,
40+
lockedToVersion: {
41+
select: {
42+
version: true,
43+
},
44+
},
45+
resumeParentOnCompletion: true,
46+
batch: {
47+
select: {
48+
id: true,
49+
friendlyId: true,
50+
},
51+
},
52+
} satisfies Prisma.TaskRunSelect;
53+
54+
type CommonRelatedRun = Prisma.Result<
55+
typeof prisma.taskRun,
56+
{ select: typeof commonRunSelect },
57+
"findFirstOrThrow"
58+
>;
1759

1860
export class ApiRetrieveRunPresenter extends BasePresenter {
1961
public async call(
@@ -36,92 +78,21 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
3678
lockedToVersion: true,
3779
schedule: true,
3880
tags: true,
39-
parentTaskRun: {
81+
batch: {
4082
select: {
4183
id: true,
4284
friendlyId: true,
43-
status: true,
44-
taskIdentifier: true,
45-
createdAt: true,
46-
startedAt: true,
47-
updatedAt: true,
48-
completedAt: true,
49-
expiredAt: true,
50-
delayUntil: true,
51-
ttl: true,
52-
tags: true,
53-
costInCents: true,
54-
baseCostInCents: true,
55-
usageDurationMs: true,
56-
idempotencyKey: true,
57-
isTest: true,
58-
depth: true,
59-
lockedToVersion: {
60-
select: {
61-
version: true,
62-
},
63-
},
6485
},
6586
},
87+
parentTaskRun: {
88+
select: commonRunSelect,
89+
},
6690
rootTaskRun: {
67-
select: {
68-
id: true,
69-
friendlyId: true,
70-
status: true,
71-
taskIdentifier: true,
72-
createdAt: true,
73-
startedAt: true,
74-
updatedAt: true,
75-
completedAt: true,
76-
expiredAt: true,
77-
delayUntil: true,
78-
ttl: true,
79-
tags: true,
80-
costInCents: true,
81-
baseCostInCents: true,
82-
usageDurationMs: true,
83-
idempotencyKey: true,
84-
isTest: true,
85-
depth: true,
86-
lockedToVersion: {
87-
select: {
88-
version: true,
89-
},
90-
},
91-
},
91+
select: commonRunSelect,
9292
},
9393
childRuns: {
9494
select: {
95-
id: true,
96-
taskIdentifier: true,
97-
friendlyId: true,
98-
resumeParentOnCompletion: true,
99-
status: true,
100-
createdAt: true,
101-
startedAt: true,
102-
updatedAt: true,
103-
completedAt: true,
104-
expiredAt: true,
105-
delayUntil: true,
106-
ttl: true,
107-
tags: true,
108-
costInCents: true,
109-
baseCostInCents: true,
110-
usageDurationMs: true,
111-
idempotencyKey: true,
112-
isTest: true,
113-
depth: true,
114-
lockedToVersion: {
115-
select: {
116-
version: true,
117-
},
118-
},
119-
batch: {
120-
select: {
121-
id: true,
122-
friendlyId: true,
123-
},
124-
},
95+
...commonRunSelect,
12596
},
12697
},
12798
},
@@ -189,30 +160,11 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
189160
const apiStatus = ApiRetrieveRunPresenter.apiStatusFromRunStatus(taskRun.status);
190161

191162
return {
192-
id: taskRun.friendlyId,
193-
status: apiStatus,
194-
taskIdentifier: taskRun.taskIdentifier,
195-
idempotencyKey: taskRun.idempotencyKey ?? undefined,
196-
version: taskRun.lockedToVersion ? taskRun.lockedToVersion.version : undefined,
197-
createdAt: taskRun.createdAt ?? undefined,
198-
updatedAt: taskRun.updatedAt ?? undefined,
199-
startedAt: taskRun.startedAt ?? taskRun.lockedAt ?? undefined,
200-
finishedAt: ApiRetrieveRunPresenter.isStatusFinished(apiStatus)
201-
? taskRun.updatedAt
202-
: undefined,
203-
delayedUntil: taskRun.delayUntil ?? undefined,
163+
...createCommonRunStructure(taskRun),
204164
payload: $payload,
205165
payloadPresignedUrl: $payloadPresignedUrl,
206166
output: $output,
207167
outputPresignedUrl: $outputPresignedUrl,
208-
isTest: taskRun.isTest,
209-
ttl: taskRun.ttl ?? undefined,
210-
expiredAt: taskRun.expiredAt ?? undefined,
211-
tags: taskRun.tags.map((t) => t.name).sort((a, b) => a.localeCompare(b)),
212-
costInCents: taskRun.costInCents,
213-
baseCostInCents: taskRun.baseCostInCents,
214-
durationMs: taskRun.usageDurationMs,
215-
depth: taskRun.depth,
216168
schedule: taskRun.schedule
217169
? {
218170
id: taskRun.schedule.friendlyId,
@@ -227,7 +179,6 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
227179
},
228180
}
229181
: undefined,
230-
...ApiRetrieveRunPresenter.apiBooleanHelpersFromRunStatus(apiStatus),
231182
attempts: !showSecretDetails
232183
? []
233184
: taskRun.attempts.map((a) => ({
@@ -240,92 +191,11 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
240191
error: ApiRetrieveRunPresenter.apiErrorFromError(a.error),
241192
})),
242193
relatedRuns: {
243-
root: taskRun.rootTaskRun
244-
? {
245-
id: taskRun.rootTaskRun.friendlyId,
246-
taskIdentifier: taskRun.rootTaskRun.taskIdentifier,
247-
idempotencyKey: taskRun.rootTaskRun.idempotencyKey ?? undefined,
248-
version: taskRun.rootTaskRun.lockedToVersion?.version,
249-
status: ApiRetrieveRunPresenter.apiStatusFromRunStatus(taskRun.rootTaskRun.status),
250-
createdAt: taskRun.rootTaskRun.createdAt,
251-
startedAt: taskRun.rootTaskRun.startedAt ?? undefined,
252-
updatedAt: taskRun.rootTaskRun.updatedAt,
253-
finishedAt: taskRun.rootTaskRun.completedAt ?? undefined,
254-
expiredAt: taskRun.rootTaskRun.expiredAt ?? undefined,
255-
delayedUntil: taskRun.rootTaskRun.delayUntil ?? undefined,
256-
ttl: taskRun.rootTaskRun.ttl ?? undefined,
257-
costInCents: taskRun.rootTaskRun.costInCents,
258-
baseCostInCents: taskRun.rootTaskRun.baseCostInCents,
259-
durationMs: taskRun.rootTaskRun.usageDurationMs,
260-
isTest: taskRun.rootTaskRun.isTest,
261-
depth: taskRun.rootTaskRun.depth,
262-
tags: taskRun.rootTaskRun.tags
263-
.map((t) => t.name)
264-
.sort((a, b) => a.localeCompare(b)),
265-
...ApiRetrieveRunPresenter.apiBooleanHelpersFromTaskRunStatus(
266-
taskRun.rootTaskRun.status
267-
),
268-
}
269-
: undefined,
194+
root: taskRun.rootTaskRun ? createCommonRunStructure(taskRun.rootTaskRun) : undefined,
270195
parent: taskRun.parentTaskRun
271-
? {
272-
id: taskRun.parentTaskRun.friendlyId,
273-
taskIdentifier: taskRun.parentTaskRun.taskIdentifier,
274-
idempotencyKey: taskRun.parentTaskRun.idempotencyKey ?? undefined,
275-
version: taskRun.parentTaskRun.lockedToVersion?.version,
276-
status: ApiRetrieveRunPresenter.apiStatusFromRunStatus(
277-
taskRun.parentTaskRun.status
278-
),
279-
createdAt: taskRun.parentTaskRun.createdAt,
280-
startedAt: taskRun.parentTaskRun.startedAt ?? undefined,
281-
updatedAt: taskRun.parentTaskRun.updatedAt,
282-
finishedAt: taskRun.parentTaskRun.completedAt ?? undefined,
283-
expiredAt: taskRun.parentTaskRun.expiredAt ?? undefined,
284-
delayedUntil: taskRun.parentTaskRun.delayUntil ?? undefined,
285-
isRoot: taskRun.parentTaskRun.id === taskRun.rootTaskRun?.id,
286-
ttl: taskRun.parentTaskRun.ttl ?? undefined,
287-
costInCents: taskRun.parentTaskRun.costInCents,
288-
baseCostInCents: taskRun.parentTaskRun.baseCostInCents,
289-
durationMs: taskRun.parentTaskRun.usageDurationMs,
290-
isTest: taskRun.parentTaskRun.isTest,
291-
depth: taskRun.parentTaskRun.depth,
292-
tags: taskRun.parentTaskRun.tags
293-
.map((t) => t.name)
294-
.sort((a, b) => a.localeCompare(b)),
295-
...ApiRetrieveRunPresenter.apiBooleanHelpersFromTaskRunStatus(
296-
taskRun.parentTaskRun.status
297-
),
298-
}
196+
? createCommonRunStructure(taskRun.parentTaskRun)
299197
: undefined,
300-
children: taskRun.childRuns.map((r) => ({
301-
id: r.friendlyId,
302-
taskIdentifier: r.taskIdentifier,
303-
idempotencyKey: r.idempotencyKey ?? undefined,
304-
version: r.lockedToVersion?.version,
305-
triggerFunction: r.batch
306-
? r.resumeParentOnCompletion
307-
? "batchTriggerAndWait"
308-
: "batchTrigger"
309-
: r.resumeParentOnCompletion
310-
? "triggerAndWait"
311-
: "trigger",
312-
batchId: r.batch?.friendlyId,
313-
status: ApiRetrieveRunPresenter.apiStatusFromRunStatus(r.status),
314-
createdAt: r.createdAt,
315-
startedAt: r.startedAt ?? undefined,
316-
updatedAt: r.updatedAt,
317-
finishedAt: r.completedAt ?? undefined,
318-
expiredAt: r.expiredAt ?? undefined,
319-
delayedUntil: r.delayUntil ?? undefined,
320-
ttl: r.ttl ?? undefined,
321-
tags: r.tags.map((t) => t.name).sort((a, b) => a.localeCompare(b)),
322-
costInCents: r.costInCents,
323-
baseCostInCents: r.baseCostInCents,
324-
durationMs: r.usageDurationMs,
325-
depth: r.depth,
326-
isTest: r.isTest,
327-
...ApiRetrieveRunPresenter.apiBooleanHelpersFromTaskRunStatus(r.status),
328-
})),
198+
children: taskRun.childRuns.map((r) => createCommonRunStructure(r)),
329199
},
330200
};
331201
});
@@ -458,3 +328,39 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
458328
}
459329
}
460330
}
331+
332+
function createCommonRunStructure(run: CommonRelatedRun) {
333+
return {
334+
id: run.friendlyId,
335+
taskIdentifier: run.taskIdentifier,
336+
idempotencyKey: run.idempotencyKey ?? undefined,
337+
version: run.lockedToVersion?.version,
338+
status: ApiRetrieveRunPresenter.apiStatusFromRunStatus(run.status),
339+
createdAt: run.createdAt,
340+
startedAt: run.startedAt ?? undefined,
341+
updatedAt: run.updatedAt,
342+
finishedAt: run.completedAt ?? undefined,
343+
expiredAt: run.expiredAt ?? undefined,
344+
delayedUntil: run.delayUntil ?? undefined,
345+
ttl: run.ttl ?? undefined,
346+
costInCents: run.costInCents,
347+
baseCostInCents: run.baseCostInCents,
348+
durationMs: run.usageDurationMs,
349+
isTest: run.isTest,
350+
depth: run.depth,
351+
tags: run.tags
352+
.map((t: { name: string }) => t.name)
353+
.sort((a: string, b: string) => a.localeCompare(b)),
354+
...ApiRetrieveRunPresenter.apiBooleanHelpersFromTaskRunStatus(run.status),
355+
triggerFunction: resolveTriggerFunction(run),
356+
batchId: run.batch?.friendlyId,
357+
};
358+
}
359+
360+
function resolveTriggerFunction(run: CommonRelatedRun): TriggerFunction {
361+
if (run.batch) {
362+
return run.resumeParentOnCompletion ? "batchTriggerAndWait" : "batchTrigger";
363+
} else {
364+
return run.resumeParentOnCompletion ? "triggerAndWait" : "trigger";
365+
}
366+
}

docs/v3-openapi.yaml

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,6 @@ components:
18291829
- taskIdentifier
18301830
- createdAt
18311831
- updatedAt
1832-
- attempts
18331832
properties:
18341833
id:
18351834
type: string
@@ -1911,9 +1910,21 @@ components:
19111910
example: 491
19121911
description: The duration of compute (so far) in milliseconds. This does not include waits.
19131912
depth:
1914-
type: number
1913+
type: integer
19151914
example: 0
19161915
description: The depth of the run in the task run hierarchy. The root run has a depth of 0.
1916+
batchId:
1917+
type: string
1918+
description: The ID of the batch that this run belongs to
1919+
example: batch_1234
1920+
triggerFunction:
1921+
type: string
1922+
description: The name of the function that triggered the run
1923+
enum:
1924+
- trigger
1925+
- triggerAndWait
1926+
- batchTrigger
1927+
- batchTriggerAndWait
19171928

19181929
RetrieveRunResponse:
19191930
allOf:
@@ -1945,35 +1956,13 @@ components:
19451956
$ref: "#/components/schemas/CommonRunObject"
19461957
description: The root run of the run hierarchy. Will be omitted if the run is the root run
19471958
parent:
1959+
$ref: "#/components/schemas/CommonRunObject"
19481960
description: The parent run of the run. Will be omitted if the run is the root run
1949-
allOf:
1950-
- $ref: "#/components/schemas/CommonRunObject"
1951-
- type: object
1952-
properties:
1953-
isRoot:
1954-
type: boolean
1955-
example: false
1956-
description: Whether this run is the root run
19571961
children:
19581962
description: The immediate children of the run. Will be omitted if the run has no children
19591963
type: array
19601964
items:
1961-
allOf:
1962-
- $ref: "#/components/schemas/CommonRunObject"
1963-
- type: object
1964-
properties:
1965-
batchId:
1966-
type: string
1967-
description: The ID of the batch that this run belongs to
1968-
example: batch_1234
1969-
triggerFunction:
1970-
type: string
1971-
description: The name of the function that triggered the run
1972-
enum:
1973-
- trigger
1974-
- triggerAndWait
1975-
- batchTrigger
1976-
- batchTriggerAndWait
1965+
$ref: "#/components/schemas/CommonRunObject"
19771966
schedule:
19781967
type: object
19791968
description: The schedule that triggered the run. Will be omitted if the run was not triggered by a schedule

0 commit comments

Comments
 (0)