hywax / nuxt-cron

A Nuxt module for cron jobs in your app.
https://nuxt-cron.hywax.space
MIT License
43 stars 2 forks source link
cron node nuxt-module nuxt3 typescript

nuxt-cron

Nuxt Cron

npm version npm downloads License

A Nuxt module for cron jobs in your app.

Features

Installation

# Using pnpm
pnpm add nuxt-cron -D

# Using yarn
yarn add nuxt-cron -D

# Using npm
npm install nuxt-cron -D

Usage

Setup

Add nuxt-cron to the modules section of nuxt.config.ts

export default defineNuxtConfig({
  modules: ['nuxt-cron']
})

Configuration

You can configure the module by adding a nuxt-cron section to your nuxt.config file.

export default defineNuxtConfig({
  cron: {
    runOnInit: true,
    timeZone: 'Africa/Abidjan',
    jobsDir: 'cron'
  }
})

by default, nuxt-cron will auto-import your cron from the models directory from server directory. You can change this behavior by setting the jobsDir option.

API

defineCronHandler

This function creates a new cron job. Example usage:

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

export default defineCronHandler('everySecond', () => {
  console.log('I run every seconds')
})

or use with params:

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

export default defineCronHandler('everySecond', () => {
  console.log('I run every seconds')
}, { runOnInit: true })

More examples can be found here.

Credits

A huge thank you to everyone who is helping to improve nuxt-cron. Thanks to you, the project can evolve!

Contributors

To become a contributor, please follow our contributing guide.

nuxt-cron

License

This app is open-sourced software licensed under the MIT license.