| 152 | |
| 153 | /** DynamicSchedule` allows you to define a scheduled trigger that can be configured dynamically at runtime. */ |
| 154 | export class DynamicSchedule implements Trigger<ScheduledEventSpecification> { |
| 155 | /** |
| 156 | * @param client The `TriggerClient` instance to use for registering the trigger. |
| 157 | * @param options The options for the schedule. |
| 158 | */ |
| 159 | constructor( |
| 160 | private client: TriggerClient, |
| 161 | private options: DynamicIntervalOptions |
| 162 | ) { |
| 163 | client.attachDynamicSchedule(this.options.id); |
| 164 | } |
| 165 | |
| 166 | get id() { |
| 167 | return this.options.id; |
| 168 | } |
| 169 | |
| 170 | get event() { |
| 171 | return { |
| 172 | name: "trigger.scheduled", |
| 173 | title: "Dynamic Schedule", |
| 174 | source: "trigger.dev", |
| 175 | icon: "schedule-dynamic", |
| 176 | examples, |
| 177 | parsePayload: ScheduledPayloadSchema.parse, |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | async register(key: string, metadata: ScheduleMetadata) { |
| 182 | const runStore = runLocalStorage.getStore(); |
| 183 | |
| 184 | if (!runStore) { |
| 185 | return this.client.registerSchedule(this.id, key, metadata); |
| 186 | } |
| 187 | |
| 188 | const { io } = runStore; |
| 189 | |
| 190 | return await io.runTask( |
| 191 | [key, "register"], |
| 192 | async (task) => { |
| 193 | return this.client.registerSchedule(this.id, key, metadata); |
| 194 | }, |
| 195 | { |
| 196 | name: "Register Schedule", |
| 197 | icon: metadata.type === "cron" ? "schedule-cron" : "schedule-interval", |
| 198 | properties: [ |
| 199 | { label: "Dynamic Schedule", text: this.id }, |
| 200 | { label: "Schedule ID", text: key }, |
| 201 | ], |
| 202 | params: metadata, |
| 203 | } |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | async unregister(key: string) { |
| 208 | const runStore = runLocalStorage.getStore(); |
| 209 | |
| 210 | if (!runStore) { |
| 211 | return this.client.unregisterSchedule(this.id, key); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…