-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathreplayTaskRun.server.ts
More file actions
182 lines (165 loc) · 6.07 KB
/
replayTaskRun.server.ts
File metadata and controls
182 lines (165 loc) · 6.07 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
import {
type MachinePresetName,
conditionallyImportPacket,
parsePacket,
stringifyIO,
} from "@trigger.dev/core/v3";
import { type TaskRun } from "@trigger.dev/database";
import { findEnvironmentById } from "~/models/runtimeEnvironment.server";
import { logger } from "~/services/logger.server";
import { BaseService } from "./baseService.server";
import { OutOfEntitlementError, TriggerTaskService } from "./triggerTask.server";
import { type RunOptionsData } from "../testTask";
import { replaceSuperJsonPayload } from "@trigger.dev/core/v3/utils/ioSerialization";
import { determineRealtimeStreamsVersion } from "~/services/realtime/v1StreamsGlobal.server";
type OverrideOptions = {
environmentId?: string;
payload?: string;
metadata?: unknown;
bulkActionId?: string;
triggerSource?: string;
} & RunOptionsData;
export class ReplayTaskRunService extends BaseService {
public async call(existingTaskRun: TaskRun, overrideOptions: OverrideOptions = {}) {
const authenticatedEnvironment = await findEnvironmentById(
overrideOptions.environmentId ?? existingTaskRun.runtimeEnvironmentId
);
if (!authenticatedEnvironment) {
return;
}
if (authenticatedEnvironment.archivedAt) {
throw new Error("Can't replay a run on an archived environment");
}
logger.info("Replaying task run", {
taskRunId: existingTaskRun.id,
taskRunFriendlyId: existingTaskRun.friendlyId,
});
const existingEnvironment = await this._prisma.runtimeEnvironment.findFirstOrThrow({
where: {
id: existingTaskRun.runtimeEnvironmentId,
},
select: {
id: true,
type: true,
},
});
const payloadPacket = await this.overrideExistingPayloadPacket(
existingTaskRun,
overrideOptions.payload
);
const parsedPayload =
payloadPacket.dataType === "application/json"
? await parsePacket(payloadPacket)
: payloadPacket.data;
const payloadType = payloadPacket.dataType;
const metadata = overrideOptions.metadata ?? (await this.getExistingMetadata(existingTaskRun));
const tags = overrideOptions.tags ?? existingTaskRun.runTags;
// Only use the region from the existing run if V2 engine and neither environment is dev
const ignoreRegion =
existingTaskRun.engine === "V1" ||
existingEnvironment.type === "DEVELOPMENT" ||
authenticatedEnvironment.type === "DEVELOPMENT";
const region = ignoreRegion ? undefined : overrideOptions.region ?? existingTaskRun.workerQueue;
try {
const taskQueue = await this._prisma.taskQueue.findFirst({
where: {
runtimeEnvironmentId: authenticatedEnvironment.id,
name: overrideOptions.queue ?? existingTaskRun.queue,
},
});
const triggerTaskService = new TriggerTaskService();
const result = await triggerTaskService.call(
existingTaskRun.taskIdentifier,
authenticatedEnvironment,
{
payload: parsedPayload,
options: {
payloadType,
queue: taskQueue
? {
name: taskQueue.name,
}
: undefined,
test: existingTaskRun.isTest,
tags,
metadata: metadata,
delay: overrideOptions.delaySeconds
? new Date(Date.now() + overrideOptions.delaySeconds * 1000)
: undefined,
ttl: overrideOptions.ttlSeconds,
idempotencyKey: overrideOptions.idempotencyKey,
idempotencyKeyTTL: overrideOptions.idempotencyKeyTTLSeconds
? `${overrideOptions.idempotencyKeyTTLSeconds}s`
: undefined,
concurrencyKey:
overrideOptions.concurrencyKey ?? existingTaskRun.concurrencyKey ?? undefined,
maxAttempts: overrideOptions.maxAttempts,
maxDuration: overrideOptions.maxDurationSeconds,
machine:
overrideOptions.machine ??
(existingTaskRun.machinePreset as MachinePresetName) ??
undefined,
lockToVersion:
overrideOptions.version === "latest" ? undefined : overrideOptions.version,
bulkActionId: overrideOptions?.bulkActionId,
region,
priority: overrideOptions.prioritySeconds,
},
},
{
spanParentAsLink: true,
parentAsLinkType: "replay",
replayedFromTaskRunFriendlyId: existingTaskRun.friendlyId,
traceContext: {
traceparent: `00-${existingTaskRun.traceId}-${existingTaskRun.spanId}-01`,
},
realtimeStreamsVersion: determineRealtimeStreamsVersion(
existingTaskRun.realtimeStreamsVersion
),
triggerSource: overrideOptions.triggerSource ?? "api",
triggerAction: "replay",
}
);
return result?.run;
} catch (error) {
if (error instanceof OutOfEntitlementError) {
return;
}
logger.error("Failed to replay a run", {
error: error instanceof Error ? error.message : error,
});
return;
}
}
private async overrideExistingPayloadPacket(
existingTaskRun: TaskRun,
stringifiedPayloadOverride: string | undefined
) {
if (existingTaskRun.payloadType === "application/store") {
return conditionallyImportPacket({
data: existingTaskRun.payload,
dataType: existingTaskRun.payloadType,
});
}
if (stringifiedPayloadOverride && existingTaskRun.payloadType === "application/super+json") {
const newPayload = await replaceSuperJsonPayload(
existingTaskRun.payload,
stringifiedPayloadOverride
);
return stringifyIO(newPayload);
}
return conditionallyImportPacket({
data: stringifiedPayloadOverride ?? existingTaskRun.payload,
dataType: existingTaskRun.payloadType,
});
}
private async getExistingMetadata(existingTaskRun: TaskRun) {
if (!existingTaskRun.seedMetadata) {
return undefined;
}
return parsePacket({
data: existingTaskRun.seedMetadata,
dataType: existingTaskRun.seedMetadataType,
});
}
}