Skip to content

Remove deprecated schedule trigger jobs when registering with the schedule engine #2204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025
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
18 changes: 18 additions & 0 deletions apps/webapp/app/v3/scheduleEngine.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { logger } from "~/services/logger.server";
import { singleton } from "~/utils/singleton";
import { TriggerTaskService } from "./services/triggerTask.server";
import { meter, tracer } from "./tracer.server";
import { workerQueue } from "~/services/worker.server";

export const scheduleEngine = singleton("ScheduleEngine", createScheduleEngine);

Expand Down Expand Up @@ -117,7 +118,24 @@ function createScheduleEngine() {
}
},
isDevEnvironmentConnectedHandler: isDevEnvironmentConnectedHandler,
onRegisterScheduleInstance: removeDeprecatedWorkerQueueItem,
});

return engine;
}

async function removeDeprecatedWorkerQueueItem(instanceId: string) {
// We need to dequeue the instance from the existing workerQueue
try {
await workerQueue.dequeue(`scheduled-task-instance:${instanceId}`);

logger.debug("Removed deprecated worker queue item", {
instanceId,
});
} catch (error) {
logger.error("Error dequeuing scheduled task instance from deprecated queue", {
instanceId,
error: error instanceof Error ? error.message : String(error),
});
}
}
26 changes: 20 additions & 6 deletions internal-packages/schedule-engine/src/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Tracer,
} from "@internal/tracing";
import { Logger } from "@trigger.dev/core/logger";
import { PrismaClient, TaskSchedule, TaskScheduleInstance } from "@trigger.dev/database";
import { PrismaClient } from "@trigger.dev/database";
import { Worker, type JobHandlerParams } from "@trigger.dev/redis-worker";
import { calculateDistributedExecutionTime } from "./distributedScheduling.js";
import { calculateNextScheduledTimestamp, nextScheduledTimestamps } from "./scheduleCalculation.js";
Expand Down Expand Up @@ -122,6 +122,19 @@ export class ScheduleEngine {
return startSpan(this.tracer, "registerNextTaskScheduleInstance", async (span) => {
const startTime = Date.now();

if (this.options.onRegisterScheduleInstance) {
const [registerError] = await tryCatch(
this.options.onRegisterScheduleInstance(params.instanceId)
);

if (registerError) {
this.logger.error("Error calling the onRegisterScheduleInstance callback", {
instanceId: params.instanceId,
error: registerError,
});
}
}

span.setAttribute("instanceId", params.instanceId);

this.logger.debug("Starting schedule registration", {
Expand Down Expand Up @@ -382,10 +395,11 @@ export class ScheduleEngine {
span.setAttribute("skip_reason", skipReason);
}

if (shouldTrigger) {
const scheduleTimestamp =
params.exactScheduleTime ?? instance.nextScheduledTimestamp ?? new Date();
// Calculate the schedule timestamp that will be used (regardless of whether we trigger or not)
const scheduleTimestamp =
params.exactScheduleTime ?? instance.nextScheduledTimestamp ?? new Date();

if (shouldTrigger) {
const payload = {
scheduleId: instance.taskSchedule.friendlyId,
type: instance.taskSchedule.type as "DECLARATIVE" | "IMPERATIVE",
Expand Down Expand Up @@ -427,7 +441,7 @@ export class ScheduleEngine {
payload,
scheduleInstanceId: instance.id,
scheduleId: instance.taskSchedule.id,
exactScheduleTime: params.exactScheduleTime,
exactScheduleTime: scheduleTimestamp,
})
);

Expand Down Expand Up @@ -523,7 +537,7 @@ export class ScheduleEngine {
id: params.instanceId,
},
data: {
lastScheduledTimestamp: instance.nextScheduledTimestamp,
lastScheduledTimestamp: scheduleTimestamp,
},
});

Expand Down
1 change: 1 addition & 0 deletions internal-packages/schedule-engine/src/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface ScheduleEngineOptions {
meter?: Meter;
onTriggerScheduledTask: TriggerScheduledTaskCallback;
isDevEnvironmentConnectedHandler: (environmentId: string) => Promise<boolean>;
onRegisterScheduleInstance?: (instanceId: string) => Promise<void>;
}

export interface UpsertScheduleParams {
Expand Down