Skip to content

Cron Handler

Usage

This function creates a new cron job. Example usage:

ts
// server/cron/example.ts
import { defineCronHandler } from '#nuxt/cron'

export default defineCronHandler('everySecond', () => {
  // do action
})

Params

KeyTypeRequireDescription
timeCronPresets | CronTimetrueThe time to fire off your job. Presets or custom cron expression
callback() => voidtrueFunction to execute at the specified time
optionsCronOptionsfalseOptions for Cron

Type

ts
function defineCronHandler(
  time: CronPresets | CronTime,
  callback: CronTick,
  options?: CronOptions
): CronJob

export type CronTime =
  | 'everySecond'
  | 'everyMinute'
  | 'everyTwoMinutes'
  | 'everyThreeMinutes'
  | 'everyFourMinutes'
  | 'everyFiveMinutes'
  | 'everyTenMinutes'
  | 'everyFifteenMinutes'
  | 'everyThirtyMinutes'
  | 'hourly'
  | 'everyOddHour'
  | 'everyTwoHours'
  | 'everyThreeHours'
  | 'everyFourHours'
  | 'everySixHours'
  | 'daily'
  | 'weekly'
  | 'quarterly'
  | 'yearly'

export type CronTime = () => string

export type CronTick = () => void | Promise<void>

export interface CronOptions {
  runOnInit?: boolean
  timeZone?: string | null
}

export interface CronJob {
  time: CronTime
  callback: CronTick
  options?: CronOptions
}