-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathrealtime.ts
More file actions
61 lines (50 loc) · 1.41 KB
/
realtime.ts
File metadata and controls
61 lines (50 loc) · 1.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
import { logger, runs, task } from "@trigger.dev/sdk";
import { helloWorldTask } from "./example.js";
import { setTimeout } from "timers/promises";
export const realtimeByTagsTask = task({
id: "realtime-by-tags",
run: async (payload: any, { ctx, signal }) => {
await helloWorldTask.trigger(
{ hello: "world" },
{
tags: ["hello-world", "realtime"],
}
);
const timeoutSignal = AbortSignal.timeout(10000);
const $signal = AbortSignal.any([signal, timeoutSignal]);
$signal.addEventListener("abort", () => {
logger.info("signal aborted");
});
for await (const run of runs.subscribeToRunsWithTag(
"hello-world",
{ createdAt: "2m", skipColumns: ["payload", "output", "number"] },
{ signal: $signal }
)) {
logger.info("run", { run });
}
return {
message: "Hello, world!",
};
},
});
export const realtimeUpToDateTask = task({
id: "realtime-up-to-date",
run: async ({ runId }: { runId?: string }) => {
if (!runId) {
const handle = await helloWorldTask.trigger(
{ hello: "world" },
{
tags: ["hello-world", "realtime"],
}
);
runId = handle.id;
}
logger.info("runId", { runId });
for await (const run of runs.subscribeToRun(runId, { stopOnCompletion: true })) {
logger.info("run", { run });
}
return {
message: "Hello, world!",
};
},
});