Scheduled function patterns for background tasks including interval scheduling, cron expressions, job monitoring, retry strategies, and best practices for long-running tasks
Convex cron jobs allow you to schedule functions to run at regular intervals or specific times. Key features:
Run functions on a fixed schedule
Support for interval-based and cron expression scheduling
Automatic retries on failure
Monitoring via the Convex dashboard
Basic Cron Setup
// convex/crons.tsimport { cronJobs } from "convex/server";import { internal } from "./_generated/api";const crons = cronJobs();// Run every hourcrons.interval( "cleanup expired sessions", { hours: 1 }, internal.tasks.cleanupExpiredSessions, {});
Interval-Based Scheduling
Use crons.interval for simple recurring tasks:
// convex/crons.tsimport { cronJobs } from "convex/server";import { internal } from "./_generated/api";const crons = cronJobs();// Every 5 minutescrons.interval( "sync external data", { minutes: 5 }, internal.sync.fetchExternalData, {}
Cron Expression Scheduling
Use crons.cron for precise scheduling with cron expressions:
// convex/crons.tsimport { cronJobs } from "convex/server";import { internal } from "./_generated/api";const crons = cronJobs();// Every day at 9 AM UTCcrons.cron( "morning notifications", "0 9 * * *", internal.notifications.sendMorningDigest,
Cron Expression Reference
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sunday=0)
│ │ │ │ │
* * * * *
Common patterns:
* * * * * - Every minute
0 * * * * - Every hour
0 0 * * * - Every day at midnight
0 0 * * 0 - Every Sunday at midnight
0 0 1 * * - First day of every month
*/5 * * * * - Every 5 minutes
0 9-17 * * 1-5 - Every hour from 9 AM to 5 PM, Monday through Friday
Internal Functions for Crons
Cron jobs should call internal functions for security:
// convex/tasks.tsimport { internalMutation, internalQuery } from "./_generated/server";import { v } from "convex/values";// Cleanup expired sessionsexport const cleanupExpiredSessions = internalMutation({ args: {},
Cron Jobs with Arguments
Pass static arguments to cron jobs:
// convex/crons.tsimport { cronJobs } from "convex/server";import { internal } from "./_generated/api";const crons = cronJobs();// Different cleanup intervals for different typescrons.interval( "cleanup temp files", { hours: 1 }, internal.cleanup.cleanupByType, { fileType: "temp"
// convex/cleanup.tsimport { internalMutation } from "./_generated/server";import { v } from "convex/values";export const cleanupByType = internalMutation({ args: { fileType: v.string(), maxAge: v.number(), }, returns: v.
Monitoring and Logging
Add logging to track cron job execution:
// convex/tasks.tsimport { internalMutation } from "./_generated/server";import { v } from "convex/values";export const cleanupWithLogging = internalMutation({
Batching for Large Datasets
Handle large datasets in batches to avoid timeouts:
// convex/tasks.tsimport { internalMutation } from "./_generated/server";import { internal } from "./_generated/api";import { v } from "convex/values";const BATCH_SIZE = 100;export const processBatch
External API Calls in Crons
Use actions for external API calls:
// convex/sync.ts"use node";import { internalAction } from "./_generated/server";import { internal } from "./_generated/api";import { v } from "convex/values";export const syncExternalData
// convex/schema.tsimport { defineSchema, defineTable } from "convex/server";import { v } from "convex/values";export default defineSchema({ cronLogs: defineTable({ jobName: v.