-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReplayRunDialog.tsx
More file actions
609 lines (581 loc) · 23.9 KB
/
ReplayRunDialog.tsx
File metadata and controls
609 lines (581 loc) · 23.9 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import { conform, useForm } from "@conform-to/react";
import { parse } from "@conform-to/zod";
import { DialogClose } from "@radix-ui/react-dialog";
import { Form, useActionData, useNavigation, useParams, useSubmit } from "@remix-run/react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { type UseDataFunctionReturn, useTypedFetcher } from "remix-typedjson";
import { TaskIcon } from "~/assets/icons/TaskIcon";
import { JSONEditor } from "~/components/code/JSONEditor";
import { EnvironmentCombo } from "~/components/environments/EnvironmentLabel";
import { Button } from "~/components/primitives/Buttons";
import { DialogContent, DialogHeader } from "~/components/primitives/Dialog";
import { DurationPicker } from "~/components/primitives/DurationPicker";
import { Fieldset } from "~/components/primitives/Fieldset";
import { FormError } from "~/components/primitives/FormError";
import { Hint } from "~/components/primitives/Hint";
import { Input } from "~/components/primitives/Input";
import { InputGroup } from "~/components/primitives/InputGroup";
import { Label } from "~/components/primitives/Label";
import { Paragraph } from "~/components/primitives/Paragraph";
import { type loader as queuesLoader } from "~/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.queues";
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "~/components/primitives/Resizable";
import { Select, SelectItem } from "~/components/primitives/Select";
import { Spinner, SpinnerWhite } from "~/components/primitives/Spinner";
import { TabButton, TabContainer } from "~/components/primitives/Tabs";
import { TextLink } from "~/components/primitives/TextLink";
import { type loader } from "~/routes/resources.taskruns.$runParam.replay";
import { docsPath } from "~/utils/pathBuilder";
import { ReplayRunData } from "~/v3/replayTask";
import { RectangleStackIcon } from "@heroicons/react/20/solid";
import { Badge } from "~/components/primitives/Badge";
import { RunTagInput } from "./RunTagInput";
import { MachinePresetName } from "@trigger.dev/core/v3";
import { InfoIconTooltip } from "~/components/primitives/Tooltip";
import { divide } from "effect/Duration";
type ReplayRunDialogProps = {
runFriendlyId: string;
failedRedirect: string;
};
export function ReplayRunDialog({ runFriendlyId, failedRedirect }: ReplayRunDialogProps) {
return (
<DialogContent
key={`replay`}
className="flex h-[85vh] max-h-[85vh] flex-col overflow-hidden px-0 md:max-w-3xl lg:max-w-5xl"
>
<ReplayContent runFriendlyId={runFriendlyId} failedRedirect={failedRedirect} />
</DialogContent>
);
}
function ReplayContent({ runFriendlyId, failedRedirect }: ReplayRunDialogProps) {
const replayDataFetcher = useTypedFetcher<typeof loader>();
const isLoading = replayDataFetcher.state === "loading";
const queueFetcher = useTypedFetcher<typeof queuesLoader>();
const [environmentIdOverride, setEnvironmentIdOverride] = useState<string | undefined>(undefined);
useEffect(() => {
const searchParams = new URLSearchParams();
if (environmentIdOverride) {
searchParams.set("environmentIdOverride", environmentIdOverride);
}
replayDataFetcher.load(
`/resources/taskruns/${runFriendlyId}/replay?${searchParams.toString()}`
);
}, [runFriendlyId, environmentIdOverride]);
const params = useParams();
useEffect(() => {
if (params.organizationSlug && params.projectParam && params.envParam) {
const searchParams = new URLSearchParams();
searchParams.set("type", "custom");
searchParams.set("per_page", "100");
let envSlug = params.envParam;
if (environmentIdOverride) {
const environmentOverride = replayDataFetcher.data?.environments.find(
(env) => env.id === environmentIdOverride
);
envSlug = environmentOverride?.slug ?? envSlug;
}
queueFetcher.load(
`/resources/orgs/${params.organizationSlug}/projects/${
params.projectParam
}/env/${envSlug}/queues?${searchParams.toString()}`
);
}
}, [params.organizationSlug, params.projectParam, params.envParam, environmentIdOverride]);
const customQueues = useMemo(() => {
return queueFetcher.data?.queues ?? [];
}, [queueFetcher.data?.queues]);
return (
<div className="flex flex-1 flex-col overflow-hidden">
<DialogHeader className="px-3">Replay this run</DialogHeader>
{isLoading && !replayDataFetcher.data ? (
<div className="flex h-full items-center justify-center p-6">
<Spinner />
</div>
) : replayDataFetcher.data ? (
<ReplayForm
replayData={replayDataFetcher.data}
failedRedirect={failedRedirect}
runFriendlyId={runFriendlyId}
customQueues={customQueues}
environmentIdOverride={environmentIdOverride}
setEnvironmentIdOverride={setEnvironmentIdOverride}
/>
) : (
<>Failed to get run data</>
)}
</div>
);
}
const startingJson = "{\n\n}";
const machinePresets = Object.values(MachinePresetName.enum);
function ReplayForm({
failedRedirect,
runFriendlyId,
replayData,
customQueues,
environmentIdOverride,
setEnvironmentIdOverride,
}: {
failedRedirect: string;
runFriendlyId: string;
replayData: UseDataFunctionReturn<typeof loader>;
customQueues: UseDataFunctionReturn<typeof queuesLoader>["queues"];
environmentIdOverride: string | undefined;
setEnvironmentIdOverride: (environment: string) => void;
}) {
const navigation = useNavigation();
const submit = useSubmit();
const [defaultPayloadJson, setDefaultPayloadJson] = useState<string>(
replayData.payload ?? startingJson
);
const setPayload = useCallback((code: string) => {
setDefaultPayloadJson(code);
}, []);
const currentPayloadJson = useRef<string>(replayData.payload ?? startingJson);
const [defaultMetadataJson, setDefaultMetadataJson] = useState<string>(
replayData.metadata ?? startingJson
);
const setMetadata = useCallback((code: string) => {
setDefaultMetadataJson(code);
}, []);
const currentMetadataJson = useRef<string>(replayData.metadata ?? startingJson);
const formAction = `/resources/taskruns/${runFriendlyId}/replay`;
const isSubmitting = navigation.formAction === formAction;
const editablePayload =
replayData.payloadType === "application/json" ||
replayData.payloadType === "application/super+json";
const [tab, setTab] = useState<"payload" | "metadata">(editablePayload ? "payload" : "metadata");
const { defaultTaskQueue } = replayData;
const queues =
defaultTaskQueue && !customQueues.some((q) => q.id === defaultTaskQueue.id)
? [defaultTaskQueue, ...customQueues]
: customQueues;
const queueItems = queues.map((q) => ({
value: q.type === "task" ? `task/${q.name}` : q.name,
label: q.name,
type: q.type,
paused: q.paused,
}));
const lastSubmission = useActionData();
const [
form,
{
environment,
payload,
metadata,
delaySeconds,
ttlSeconds,
idempotencyKey,
idempotencyKeyTTLSeconds,
queue,
concurrencyKey,
maxAttempts,
maxDurationSeconds,
tags,
version,
machine,
region,
prioritySeconds,
},
] = useForm({
id: "replay-task",
lastSubmission: lastSubmission as any,
onSubmit(event, { formData }) {
event.preventDefault();
if (editablePayload) {
formData.set(payload.name, currentPayloadJson.current);
}
formData.set(metadata.name, currentMetadataJson.current);
submit(formData, { method: "POST", action: formAction });
},
onValidate({ formData }) {
return parse(formData, { schema: ReplayRunData });
},
});
return (
<Form
action={formAction}
method="post"
className="flex flex-1 flex-col overflow-hidden px-3"
{...form.props}
>
<input type="hidden" name="failedRedirect" value={failedRedirect} />
<Paragraph className="pt-6">
Replaying will create a new run in the selected environment. You can modify the payload,
metadata and run options.
</Paragraph>
<ResizablePanelGroup
orientation="horizontal"
className="-mx-3 mt-3 w-auto flex-1 border-b border-t border-grid-dimmed"
>
<ResizablePanel id="payload" min="300px">
<div className="rounded-smbg-charcoal-900 mb-3 h-full min-h-40 overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600">
<JSONEditor
className="h-full"
autoFocus
defaultValue={tab === "payload" ? defaultPayloadJson : defaultMetadataJson}
readOnly={false}
basicSetup
onChange={(v) => {
if (tab === "payload") {
currentPayloadJson.current = v;
setPayload(v);
} else {
currentMetadataJson.current = v;
setMetadata(v);
}
}}
height="100%"
min-height="100%"
max-height="100%"
additionalActions={
<TabContainer className="flex grow items-baseline justify-between self-end border-none">
<div className="flex gap-5">
<div className="flex items-center gap-1.5">
<TabButton
disabled={!editablePayload}
isActive={tab === "payload"}
layoutId="replay-editor"
onClick={() => {
setTab("payload");
}}
>
Payload
</TabButton>
{!editablePayload && (
<InfoIconTooltip
content={
<span className="text-sm">
Payload is not editable for runs with{" "}
<TextLink to={docsPath("triggering#large-payloads")}>
large payloads.
</TextLink>
</span>
}
/>
)}
</div>
<TabButton
isActive={tab === "metadata"}
layoutId="replay-editor"
onClick={() => {
setTab("metadata");
}}
>
Metadata
</TabButton>
</div>
</TabContainer>
}
/>
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel id="test-task-options" min="300px" default="300px" max="360px">
<div className="h-full overflow-y-scroll scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600">
<Fieldset className="px-3 py-3">
<Hint>
Options enable you to control the execution behavior of your task.{" "}
<TextLink to={docsPath("triggering#options")}>Read the docs.</TextLink>
</Hint>
<InputGroup>
<Label htmlFor={machine.id} variant="small">
Machine
</Label>
<Select
{...conform.select(machine)}
variant="tertiary/small"
placeholder="Select machine type"
dropdownIcon
items={machinePresets}
defaultValue={replayData.machinePreset ?? undefined}
>
{machinePresets.map((machine) => (
<SelectItem key={machine} value={machine}>
{machine}
</SelectItem>
))}
</Select>
<Hint>Overrides the machine preset.</Hint>
<FormError id={machine.errorId}>{machine.error}</FormError>
</InputGroup>
<InputGroup>
<Label htmlFor={version.id} variant="small">
Version
</Label>
<Select
{...conform.select(version)}
defaultValue="latest"
variant="tertiary/small"
placeholder="Select version"
dropdownIcon
disabled={replayData.disableVersionSelection}
>
{replayData.latestVersions.length === 0 ? (
<SelectItem disabled>No versions available</SelectItem>
) : (
replayData.latestVersions.map((version, i) => (
<SelectItem key={version} value={i === 0 ? "latest" : version}>
{version} {i === 0 && "(latest)"}
</SelectItem>
))
)}
</Select>
{replayData.disableVersionSelection ? (
<Hint>Only the latest version is available in the development environment.</Hint>
) : (
<Hint>Runs task on a specific version.</Hint>
)}
<FormError id={version.errorId}>{version.error}</FormError>
</InputGroup>
{replayData.regions.length > 1 && (
<InputGroup>
<Label htmlFor={region.id} variant="small">
Region
</Label>
<Select
{...conform.select(region)}
variant="tertiary/small"
placeholder={replayData.disableVersionSelection ? "–" : undefined}
dropdownIcon
items={replayData.regions}
defaultValue={replayData.region ?? undefined}
disabled={replayData.disableVersionSelection}
>
{replayData.regions.map((r) => (
<SelectItem key={r.name} value={r.name}>
{r.description ? `${r.name} — ${r.description}` : r.name}
{r.isDefault ? " (default)" : ""}
</SelectItem>
))}
</Select>
{replayData.disableVersionSelection ? (
<Hint>Region is not available in the development environment.</Hint>
) : (
<Hint>Overrides the region for this run.</Hint>
)}
<FormError id={region.errorId}>{region.error}</FormError>
</InputGroup>
)}
<InputGroup>
<Label htmlFor={queue.id} variant="small">
Queue
</Label>
{replayData.allowArbitraryQueues ? (
<Input
{...conform.input(queue, { type: "text" })}
variant="small"
defaultValue={replayData.queue}
/>
) : (
<Select
name={queue.name}
id={queue.id}
placeholder="Select queue"
heading="Filter queues"
variant="tertiary/small"
dropdownIcon
items={queueItems}
filter={{ keys: ["label"] }}
defaultValue={replayData.queue}
>
{(matches) =>
matches.map((queueItem) => (
<SelectItem
key={queueItem.value}
value={queueItem.value}
className="max-w-[var(--popover-anchor-width)]"
icon={
queueItem.type === "task" ? (
<TaskIcon className="size-4 shrink-0 text-blue-500" />
) : (
<RectangleStackIcon className="size-4 shrink-0 text-purple-500" />
)
}
>
<div className="flex w-full min-w-0 items-center justify-between">
<span className="truncate">{queueItem.label}</span>
{queueItem.paused && (
<Badge variant="extra-small" className="ml-1 text-warning">
Paused
</Badge>
)}
</div>
</SelectItem>
))
}
</Select>
)}
<Hint>Assign run to a specific queue.</Hint>
<FormError id={queue.errorId}>{queue.error}</FormError>
</InputGroup>
<InputGroup>
<Label htmlFor={tags.id} variant="small">
Tags
</Label>
<RunTagInput
name={tags.name}
id={tags.id}
variant="small"
defaultTags={replayData.runTags}
/>
<Hint>Add tags to easily filter runs.</Hint>
<FormError id={tags.errorId}>{tags.error}</FormError>
</InputGroup>
<InputGroup>
<Label htmlFor={maxAttempts.id} variant="small">
Max attempts
</Label>
<Input
{...conform.input(maxAttempts, { type: "number" })}
className="[&::-webkit-inner-spin-button]:appearance-none"
variant="small"
min={1}
defaultValue={replayData.maxAttempts ?? undefined}
onKeyDown={(e) => {
// only allow entering integers > 1
if (["-", "+", ".", "e", "E"].includes(e.key)) {
e.preventDefault();
}
}}
onBlur={(e) => {
const value = parseInt(e.target.value);
if (value < 1 && e.target.value !== "") {
e.target.value = "1";
}
}}
/>
<Hint>Retries failed runs up to the specified number of attempts.</Hint>
<FormError id={maxAttempts.errorId}>{maxAttempts.error}</FormError>
</InputGroup>
<InputGroup>
<Label variant="small">Max compute time</Label>
<DurationPicker
name={maxDurationSeconds.name}
id={maxDurationSeconds.id}
defaultValueSeconds={replayData.maxDurationSeconds ?? undefined}
/>
<Hint>Overrides the maximum compute time limit for the run.</Hint>
<FormError id={maxDurationSeconds.errorId}>{maxDurationSeconds.error}</FormError>
</InputGroup>
<InputGroup>
<Label htmlFor={idempotencyKey.id} variant="small">
Idempotency key
</Label>
<Input {...conform.input(idempotencyKey, { type: "text" })} variant="small" />
<FormError id={idempotencyKey.errorId}>{idempotencyKey.error}</FormError>
<Hint>
Specify an idempotency key to ensure that a task is only triggered once with the
same key.
</Hint>
</InputGroup>
<InputGroup>
<Label variant="small">Idempotency key TTL</Label>
<DurationPicker
name={idempotencyKeyTTLSeconds.name}
id={idempotencyKeyTTLSeconds.id}
/>
<Hint>Keys expire after 30 days by default.</Hint>
<FormError id={idempotencyKeyTTLSeconds.errorId}>
{idempotencyKeyTTLSeconds.error}
</FormError>
</InputGroup>
<InputGroup>
<Label htmlFor={concurrencyKey.id} variant="small">
Concurrency key
</Label>
<Input
{...conform.input(concurrencyKey, { type: "text" })}
variant="small"
defaultValue={replayData.concurrencyKey ?? undefined}
/>
<Hint>
Limits concurrency by creating a separate queue for each value of the key.
</Hint>
<FormError id={concurrencyKey.errorId}>{concurrencyKey.error}</FormError>
</InputGroup>
<InputGroup>
<Label variant="small">Delay</Label>
<DurationPicker name={delaySeconds.name} id={delaySeconds.id} />
<Hint>Delays run by a specific duration.</Hint>
<FormError id={delaySeconds.errorId}>{delaySeconds.error}</FormError>
</InputGroup>
<InputGroup>
<Label variant="small">Priority</Label>
<DurationPicker name={prioritySeconds.name} id={prioritySeconds.id} />
<Hint>Sets the priority of the run. Higher values mean higher priority.</Hint>
<FormError id={prioritySeconds.errorId}>{prioritySeconds.error}</FormError>
</InputGroup>
<InputGroup>
<Label variant="small">TTL</Label>
<DurationPicker
name={ttlSeconds.name}
id={ttlSeconds.id}
defaultValueSeconds={replayData.ttlSeconds}
/>
<Hint>Expires the run if it hasn't started within the TTL.</Hint>
<FormError id={ttlSeconds.errorId}>{ttlSeconds.error}</FormError>
</InputGroup>
<FormError>{form.error}</FormError>
</Fieldset>
</div>
</ResizablePanel>
</ResizablePanelGroup>
<div className="flex items-center justify-between gap-2 border-t border-grid-dimmed pt-3.5">
<DialogClose asChild>
<Button variant="tertiary/medium">Cancel</Button>
</DialogClose>
<div className="flex items-center gap-3">
<InputGroup className="flex flex-row items-center gap-3">
<Label>Replay this run in</Label>
<Select
{...conform.select(environment)}
placeholder="Select an environment"
defaultValue={replayData.environment.id}
items={replayData.environments}
dropdownIcon
value={environmentIdOverride}
setValue={setEnvironmentIdOverride}
variant="tertiary/medium"
className="min-w-44"
filter={{
keys: [
(item) => item.type.replace(/\//g, " ").replace(/_/g, " "),
(item) => item.branchName?.replace(/\//g, " ").replace(/_/g, " ") ?? "",
],
}}
text={(value) => {
const env = replayData.environments.find((env) => env.id === value)!;
return (
<div className="flex items-center pl-1 pr-2">
<EnvironmentCombo environment={env} />
</div>
);
}}
>
{(matches) =>
matches.map((env) => (
<SelectItem key={env.id} value={env.id}>
<EnvironmentCombo environment={env} />
</SelectItem>
))
}
</Select>
</InputGroup>
<Button
type="submit"
variant="primary/medium"
LeadingIcon={isSubmitting ? SpinnerWhite : undefined}
disabled={isSubmitting}
shortcut={{ modifiers: ["mod"], key: "enter", enabledOnInputElements: true }}
>
{isSubmitting ? "Replaying..." : "Replay run"}
</Button>
</div>
</div>
</Form>
);
}