-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathscheduled.ts
More file actions
70 lines (58 loc) · 2.14 KB
/
scheduled.ts
File metadata and controls
70 lines (58 loc) · 2.14 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
import { logger, schedules, task } from "@trigger.dev/sdk/v3";
export const firstScheduledTask = schedules.task({
id: "first-scheduled-task",
//every other minute - only run in production and staging environments (skip development)
cron: {
pattern: "0 */2 * * *",
environments: ["PRODUCTION", "STAGING"],
},
run: async (payload, { ctx }) => {
const distanceInMs =
payload.timestamp.getTime() - (payload.lastTimestamp ?? new Date()).getTime();
logger.log(payload.timezone);
logger.log("First scheduled tasks", { payload, distanceInMs });
const formatted = payload.timestamp.toLocaleString("en-US", {
timeZone: payload.timezone,
});
logger.log(formatted);
},
});
export const secondScheduledTask = schedules.task({
id: "second-scheduled-task",
cron: {
pattern: "0 5 * * *",
timezone: "Asia/Tokyo",
environments: ["PRODUCTION"], // Only run in production
},
run: async (payload) => {},
});
export const manageSchedules = task({
id: "manage-schedules",
run: async (payload) => {
const createdSchedule = await schedules.create({
//The id of the scheduled task you want to attach to.
task: firstScheduledTask.id,
//The schedule in CRON format.
cron: "* * * * *",
deduplicationKey: `create-schedule-1718277290717`,
timezone: "Asia/Tokyo",
});
logger.log("Created schedule", createdSchedule);
const editedSchedule = await schedules.update(createdSchedule.id, {
//The id of the scheduled task you want to attach to.
task: firstScheduledTask.id,
//The schedule in CRON format.
cron: "* * * * *",
timezone: "Europe/Athens",
});
logger.log("Edited schedule", editedSchedule);
const sched = await schedules.retrieve(createdSchedule.id);
logger.log("Retrieved schedule", sched);
const allSchedules = await schedules.list();
logger.log("All schedules", { allSchedules });
const { timezones } = await schedules.timezones();
logger.log("Timezones", { timezones });
const withoutUtc = await schedules.timezones({ excludeUtc: true });
logger.log("Timezones without UTC", { withoutUtc });
},
});