|
8 | 8 | Tracer, |
9 | 9 | } from "@internal/tracing"; |
10 | 10 | import { Logger } from "@trigger.dev/core/logger"; |
11 | | -import { PrismaClient, TaskSchedule, TaskScheduleInstance } from "@trigger.dev/database"; |
| 11 | +import { PrismaClient } from "@trigger.dev/database"; |
12 | 12 | import { Worker, type JobHandlerParams } from "@trigger.dev/redis-worker"; |
13 | 13 | import { calculateDistributedExecutionTime } from "./distributedScheduling.js"; |
14 | 14 | import { calculateNextScheduledTimestamp, nextScheduledTimestamps } from "./scheduleCalculation.js"; |
@@ -122,6 +122,19 @@ export class ScheduleEngine { |
122 | 122 | return startSpan(this.tracer, "registerNextTaskScheduleInstance", async (span) => { |
123 | 123 | const startTime = Date.now(); |
124 | 124 |
|
| 125 | + if (this.options.onRegisterScheduleInstance) { |
| 126 | + const [registerError] = await tryCatch( |
| 127 | + this.options.onRegisterScheduleInstance(params.instanceId) |
| 128 | + ); |
| 129 | + |
| 130 | + if (registerError) { |
| 131 | + this.logger.error("Error calling the onRegisterScheduleInstance callback", { |
| 132 | + instanceId: params.instanceId, |
| 133 | + error: registerError, |
| 134 | + }); |
| 135 | + } |
| 136 | + } |
| 137 | + |
125 | 138 | span.setAttribute("instanceId", params.instanceId); |
126 | 139 |
|
127 | 140 | this.logger.debug("Starting schedule registration", { |
@@ -382,10 +395,11 @@ export class ScheduleEngine { |
382 | 395 | span.setAttribute("skip_reason", skipReason); |
383 | 396 | } |
384 | 397 |
|
385 | | - if (shouldTrigger) { |
386 | | - const scheduleTimestamp = |
387 | | - params.exactScheduleTime ?? instance.nextScheduledTimestamp ?? new Date(); |
| 398 | + // Calculate the schedule timestamp that will be used (regardless of whether we trigger or not) |
| 399 | + const scheduleTimestamp = |
| 400 | + params.exactScheduleTime ?? instance.nextScheduledTimestamp ?? new Date(); |
388 | 401 |
|
| 402 | + if (shouldTrigger) { |
389 | 403 | const payload = { |
390 | 404 | scheduleId: instance.taskSchedule.friendlyId, |
391 | 405 | type: instance.taskSchedule.type as "DECLARATIVE" | "IMPERATIVE", |
@@ -427,7 +441,7 @@ export class ScheduleEngine { |
427 | 441 | payload, |
428 | 442 | scheduleInstanceId: instance.id, |
429 | 443 | scheduleId: instance.taskSchedule.id, |
430 | | - exactScheduleTime: params.exactScheduleTime, |
| 444 | + exactScheduleTime: scheduleTimestamp, |
431 | 445 | }) |
432 | 446 | ); |
433 | 447 |
|
@@ -523,7 +537,7 @@ export class ScheduleEngine { |
523 | 537 | id: params.instanceId, |
524 | 538 | }, |
525 | 539 | data: { |
526 | | - lastScheduledTimestamp: instance.nextScheduledTimestamp, |
| 540 | + lastScheduledTimestamp: scheduleTimestamp, |
527 | 541 | }, |
528 | 542 | }); |
529 | 543 |
|
|
0 commit comments