-
Notifications
You must be signed in to change notification settings - Fork 422
/
events.ts
213 lines (186 loc) · 4.95 KB
/
events.ts
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
import { createExpressServer } from "@trigger.dev/express";
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
import { z } from "zod";
export const client = new TriggerClient({
id: "job-catalog",
apiKey: process.env["TRIGGER_API_KEY"],
apiUrl: process.env["TRIGGER_API_URL"],
verbose: false,
ioLogLocalEnabled: true,
});
client.defineJob({
id: "event-example-1",
name: "Event Example 1",
version: "1.0.0",
enabled: true,
trigger: eventTrigger({
name: "event.example",
}),
run: async (payload, io, ctx) => {
await io.runTask(
"task-example-1",
async () => {
return {
message: "Hello World",
};
},
{ icon: "360" }
);
await io.wait("wait-1", 1);
await io.logger.info("Hello World", { ctx });
},
});
client.defineJob({
id: "cancel-event-example",
name: "Cancel Event Example",
version: "1.0.0",
trigger: eventTrigger({
name: "cancel.event.example",
}),
run: async (payload, io, ctx) => {
const event = await io.sendEvent(
"send-event",
{ name: "Cancellable Event", id: payload.id, payload: { payload, ctx } },
{
deliverAt: new Date(Date.now() + 1000 * 60 * 60 * 24), // 24 hours from now
}
);
await io.getEvent("get-event", event.id);
await io.wait("wait-1", 60); // 1 minute
await io.cancelEvent("cancel-event", event.id);
await io.getEvent("get-event-2", event.id);
},
});
client.defineJob({
id: "zod-schema",
name: "Job with Zod Schema",
version: "0.0.2",
trigger: eventTrigger({
name: "zod.schema",
schema: z.object({
userId: z.string(),
delay: z.number(),
}),
}),
run: async (payload, io, ctx) => {
await io.logger.info("Hello World", { ctx, payload });
},
});
client.defineJob({
id: "no-real-task",
name: "No real Task",
version: "0.0.1",
trigger: eventTrigger({
name: "no.real.task",
schema: z.object({
userId: z.string(),
}),
}),
run: async (payload, io, ctx) => {
await io.logger.info("Hello World", { ctx, payload });
await io.wait("Wait 1 sec", 1);
//this is a real task
// await io.runTask("task-example-1", async () => {
// return {
// message: "Hello World",
// };
// });
},
});
client.defineJob({
id: "cancel-runs-example",
name: "Cancel Runs Example",
version: "1.0.0",
trigger: eventTrigger({
name: "cancel.runs.example",
}),
run: async (payload, io, ctx) => {
const event = await io.sendEvent("send-event", {
name: "foo.bar",
id: payload.id,
payload: { payload, ctx },
});
await io.wait("wait-1", 1); // 1 second
await io.runTask("cancel-runs", async () => {
return await client.cancelRunsForEvent(event.id);
});
},
});
client.defineJob({
id: "foo-bar-example",
name: "Foo Bar Example",
version: "1.0.0",
trigger: eventTrigger({
name: "foo.bar",
}),
run: async (payload, io, ctx) => {
await io.logger.info("Hello World", { ctx, payload });
await io.wait("wait-1", 10); // 10 seconds
await io.logger.info("Hello World 2", { ctx, payload });
},
});
client.defineJob({
id: "foo-bar-example-2",
name: "Foo Bar Example 2",
version: "1.0.0",
trigger: eventTrigger({
name: "foo.bar",
}),
run: async (payload, io, ctx) => {
await io.logger.info("Hello World", { ctx, payload });
await io.wait("wait-1", 10); // 10 seconds
await io.logger.info("Hello World 2", { ctx, payload });
},
});
const maxPayloads = 10;
const maxInterval = 10;
client.defineJob({
id: "batch-trigger-receive",
name: "Batch Trigger Receive",
version: "1.0.0",
trigger: eventTrigger({
name: "batch.trigger",
batch: {
maxPayloads,
maxInterval,
},
}),
run: async (payload, io) => {
await io.logger.info(`Should at most receive ${maxPayloads} payloads per batch`);
await io.logger.info(`Should wait no more than ${maxInterval} seconds between batches`);
const totalPayloadSize = payload.reduce((sum, p) => sum + JSON.stringify(p).length, 0);
return `Received ${payload.length} payloads. Total size in bytes: ${totalPayloadSize}`;
},
});
const getLargeString = (bytes: number) => {
return Array(bytes).fill("F").join("");
};
client.defineJob({
id: "batch-trigger-send",
name: "Batch Trigger Send",
version: "1.0.0",
trigger: eventTrigger({
name: "batch.trigger.send",
schema: z.object({
payloads: z.number().default(12),
// expect batches to be chunked as they will exceed the server limit
oversized: z.boolean().default(false),
}),
}),
run: async (payload, io) => {
for (let i = 0; i < payload.payloads; i++) {
await io.sendEvent(`send-${i}`, {
name: "batch.trigger",
payload: {
count: i,
...(payload.oversized
? {
largePayload: getLargeString(250 * 1000), // 250KB
}
: undefined),
},
});
}
},
});
createExpressServer(client);