Shaw
Shaw
CCConvex Community
Created by Shaw on 9/8/2024 in #support-community
Programmatically creating cron jobs
Amazing, I'll take a look. Just asking the question help me come up with an dirty interim solution:
export const scheduleRecurringReminder = internalAction({
args: {
reminderId: v.id("reminders"),
cronExpression: v.string(),
},
handler: async (ctx, { reminderId, cronExpression }) => {
const interval = cronParser.parseExpression(cronExpression);
const nextDateTime = interval.next().toDate();
const now = new Date();
const delay = nextDateTime.getTime() - now.getTime();

// Schedule the next run
await ctx.scheduler.runAfter(
delay,
internal.reminders.scheduled.sendReminder,
{
reminderId,
},
);

// Reschedule this function to run again
await ctx.scheduler.runAfter(
delay + 1000, // Add 1 second to ensure we're past the execution time
internal.reminders.scheduled.scheduleRecurringReminder,
{
reminderId,
cronExpression,
},
);
},
});
export const scheduleRecurringReminder = internalAction({
args: {
reminderId: v.id("reminders"),
cronExpression: v.string(),
},
handler: async (ctx, { reminderId, cronExpression }) => {
const interval = cronParser.parseExpression(cronExpression);
const nextDateTime = interval.next().toDate();
const now = new Date();
const delay = nextDateTime.getTime() - now.getTime();

// Schedule the next run
await ctx.scheduler.runAfter(
delay,
internal.reminders.scheduled.sendReminder,
{
reminderId,
},
);

// Reschedule this function to run again
await ctx.scheduler.runAfter(
delay + 1000, // Add 1 second to ensure we're past the execution time
internal.reminders.scheduled.scheduleRecurringReminder,
{
reminderId,
cronExpression,
},
);
},
});
Just to recursively set up the next schedule function every time the current one is ran, will see. Thanks for the quick response!
19 replies