-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathsdkUsage.ts
More file actions
165 lines (136 loc) · 3.41 KB
/
sdkUsage.ts
File metadata and controls
165 lines (136 loc) · 3.41 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
import { task, tasks, runs, logger, schedules, envvars } from "@trigger.dev/sdk/v3";
export const sdkUsage = task({
id: "sdk-usage",
run: async (payload: any, { ctx }) => {
const $runs = await runs.list({
limit: 10,
status: "COMPLETED",
});
const $firstRun = await runs.retrieve($runs.data[0].id);
const handle = await tasks.trigger<typeof sdkChild>("sdk-child", {
run: $firstRun,
});
const replayedRun = await runs.replay($firstRun.id);
await runs.cancel(replayedRun.id);
const delayed = await tasks.trigger<typeof sdkChild>(
"sdk-child",
{
delay: "1h",
},
{
delay: "1h",
}
);
await runs.reschedule(delayed.id, {
delay: "1m",
});
for await (const run of runs.list({
limit: 10,
period: "1d",
})) {
logger.log(run.id, { run });
}
const batchHandle = await sdkChild.batchTrigger([
{
payload: {},
},
]);
const waitResult = await sdkChild.triggerAndWait({
payload: {},
});
await sdkChild.batchTriggerAndWait([
{
payload: {},
},
]);
await tasks.batchTrigger<typeof sdkChild>("sdk-child", [
{
payload: {},
},
]);
const schedule = await schedules.create({
cron: "0 0 * * *", // every day at midnight
deduplicationKey: ctx.run.id,
externalId: ctx.run.id,
task: "sdk-schedule",
});
await schedules.retrieve(schedule.id);
await schedules.del(schedule.id);
await envvars.upload({
variables: {
INSIDE_RUN: "true",
},
override: true,
});
await envvars.list({
retry: {
maxAttempts: 3,
},
});
await envvars.create(
{
name: "INSIDE_RUN_2",
value: "true",
},
{
retry: {
maxAttempts: 3,
},
}
);
await envvars.retrieve("INSIDE_RUN_2");
await envvars.update(
"INSIDE_RUN_2",
{
value: "false",
},
{
retry: {
maxAttempts: 3,
},
}
);
},
});
export const sdkChild = task({
id: "sdk-child",
run: async (payload: any) => {
return payload;
},
});
export const sdkSchedule = schedules.task({
id: "sdk-schedule",
run: async (payload: any) => {},
});
export const autoResolvePayloadAndOutput = task({
id: "auto-resolve-payload-and-output",
run: async (payload: any, { ctx }) => {
// Generate a large JSON payload (bigger than 128KB)
const childPayload = Array.from({ length: 10000 }, () => ({
key: "value",
date: new Date(),
}));
const handle = await tasks.trigger<typeof sdkChild>("sdk-child", childPayload);
const childRun = await runs.retrieve(handle.id);
if (childRun.payload) {
console.log("Child run payload exists", {
payloadPresignedUrl: childRun.payloadPresignedUrl,
});
} else {
console.log("Child run payload does not exist", {
payloadPresignedUrl: childRun.payloadPresignedUrl,
});
}
await runs.poll(handle.id);
const finishedRun = await runs.retrieve(handle.id);
if (finishedRun.output) {
console.log("Finished run output exists", {
outputPresignedUrl: finishedRun.outputPresignedUrl,
});
} else {
console.log("Finished run payload does not exist", {
outputPresignedUrl: finishedRun.outputPresignedUrl,
});
}
},
});