Skip to content
Open
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
20 changes: 12 additions & 8 deletions src/component/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ async function scheduleNextRun(
id: Id<"crons">,
lastScheduled: Date,
schedule: Schedule,
extraPatch?: Partial<Pick<Doc<"crons">, "executionJobId">>,
) {
const nextRun = calculateNextRun(lastScheduled, schedule);
const schedulerJobId = await ctx.scheduler.runAt(
nextRun,
internal.public.rescheduler,
{ id },
);
await ctx.db.patch(id, { schedulerJobId });
await ctx.db.patch(id, { schedulerJobId, ...extraPatch });
}

function calculateNextRun(lastScheduled: Date, schedule: Schedule): Date {
Expand Down Expand Up @@ -296,18 +297,21 @@ export const rescheduler = internalMutation({
console.log(`Cron ${cronJob._id} still running, skipping this run.`);
} else {
console.log(`Running cron ${cronJob._id}.`);
await ctx.scheduler.runAfter(
const executionJobId = await ctx.scheduler.runAfter(
0,
cronJob.functionHandle as FunctionHandle<"mutation" | "action">,
cronJob.args,
);
await scheduleNextRun(
ctx,
id,
new Date(schedulerJob.scheduledTime),
cronJob.schedule,
{ executionJobId },
);
return;
}

await scheduleNextRun(
ctx,
id,
new Date(schedulerJob.scheduledTime),
cronJob.schedule,
);
await scheduleNextRun(ctx, id, new Date(schedulerJob.scheduledTime), cronJob.schedule);
},
});