Shaw
Shaw
CCConvex Community
Created by Shaw on 9/8/2024 in #support-community
Programmatically creating cron jobs
I've been attempting to programmatically set up cron jobs for different users with different cron expressions, calling them via actions and mutations but with no luck. It seems scouring other docs and other peoples questions that this isn't currently possible or even considered an anti-pattern. I'm not sure how to implement my use case just using scheduled functions. My current implementation:
import { cronJobs } from "convex/server";
import { v } from "convex/values";
import { internal } from "./_generated/api";
import { internalAction } from "./_generated/server";

const crons = cronJobs();

export const scheduleRecurringReminder = internalAction({
args: {
reminderId: v.id("reminders"),
cronExpression: v.string(),
},
handler: async (_ctx, { reminderId, cronExpression }) => {
console.info(
`Scheduling recurring reminder ${reminderId} with cron expression ${cronExpression}`,
);
crons.cron(
`Job ${reminderId}`,
cronExpression,
internal.reminders.scheduled.sendReminder,
{
reminderId,
},
);
},
});

export default crons;
import { cronJobs } from "convex/server";
import { v } from "convex/values";
import { internal } from "./_generated/api";
import { internalAction } from "./_generated/server";

const crons = cronJobs();

export const scheduleRecurringReminder = internalAction({
args: {
reminderId: v.id("reminders"),
cronExpression: v.string(),
},
handler: async (_ctx, { reminderId, cronExpression }) => {
console.info(
`Scheduling recurring reminder ${reminderId} with cron expression ${cronExpression}`,
);
crons.cron(
`Job ${reminderId}`,
cronExpression,
internal.reminders.scheduled.sendReminder,
{
reminderId,
},
);
},
});

export default crons;
If I need to do this via scheduled functions, not exactly sure where to start. For simple cron expressions it seems doable, but for my complex like:
00 10 * * 2,4
00 10 * * 2,4
would be At 10:00 AM, only on Tuesday and Thursday Not sure how'd I structure scheduled functions to get me the same behavior as a cron. Any advice appreciated! Thank in advanc 🙂
19 replies