Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class RegisterNextTaskScheduleInstanceService extends BaseService {
"task_schedule_generator_expression",
instance.taskSchedule.generatorExpression
);
span.setAttribute(
"last_scheduled_timestamp",
instance.lastScheduledTimestamp?.toISOString() ?? new Date().toISOString()
);

return calculateNextScheduledTimestamp(
instance.taskSchedule.generatorExpression,
Expand Down
27 changes: 25 additions & 2 deletions apps/webapp/app/v3/utils/calculateNextSchedule.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@ export function calculateNextScheduledTimestamp(
timezone: string | null,
lastScheduledTimestamp: Date = new Date()
) {
const now = Date.now();

let nextStep = calculateNextStep(schedule, timezone, lastScheduledTimestamp);

while (nextStep.getTime() < Date.now()) {
nextStep = calculateNextStep(schedule, timezone, nextStep);
// If the next step is still in the past, we might need to skip ahead
if (nextStep.getTime() <= now) {
// Calculate a second step to determine the interval
const secondStep = calculateNextStep(schedule, timezone, nextStep);
const interval = secondStep.getTime() - nextStep.getTime();

// If we have a consistent interval and it would take many iterations,
// skip ahead mathematically instead of iterating
if (interval > 0) {
const stepsNeeded = Math.floor((now - nextStep.getTime()) / interval);

// Only skip ahead if it would save us more than a few iterations
if (stepsNeeded > 10) {
// Skip ahead by calculating how many intervals to add
const skipAheadTime = nextStep.getTime() + stepsNeeded * interval;
nextStep = calculateNextStep(schedule, timezone, new Date(skipAheadTime));
}
}

// Use the normal iteration for the remaining steps (should be <= 10 now)
while (nextStep.getTime() <= now) {
nextStep = calculateNextStep(schedule, timezone, nextStep);
}
}

return nextStep;
Expand Down
Loading