@vafast/cron
v0.0.5
Published
Plugin for Vafast for cron jobs
Readme
@vafast/cron
Plugin for Vafast that adds support for running cron jobs.
Installation
npm install @vafast/cron
# or
npm install @vafast/cronExample
import { Server, createHandler } from 'vafast'
import { cron } from '@vafast/cron'
import type { Route } from 'vafast'
// Create cron jobs
const heartbeatCron = cron({
name: 'heartbeat',
pattern: '*/30 * * * * *', // Every 30 seconds
run() {
console.log('Heartbeat')
}
})
const loggerCron = cron({
name: 'logger',
pattern: '*/1 * * * * *', // Every second
run() {
console.log(new Date().toISOString())
}
})
// Define routes
const routes: Route[] = [
{
method: 'GET',
path: '/stop',
handler: createHandler(() => {
loggerCron.stop()
return { message: 'Stop logger' }
})
},
{
method: 'GET',
path: '/start',
handler: createHandler(() => {
loggerCron.resume()
return { message: 'Start logger' }
})
}
]
// Create server
const server = new Server(routes)
// Cron jobs start automatically, no need to call start()
export default {
fetch: (req: Request) => server.fetch(req)
}API
This plugin exports cron function using croner.
For documentation, cron uses the same syntax as croner, so please refer to croner documentation.
Cron Expression Format
┌────────────── second (optional)
│ ┌──────────── minute
│ │ ┌────────── hour
│ │ │ ┌──────── day of month
│ │ │ │ ┌────── month
│ │ │ │ │ ┌──── day of week
│ │ │ │ │ │
* * * * * *Methods
start()- Start the cron jobstop()- Stop the cron jobresume()- Resume a stopped cron jobisRunning()- Check if the cron job is runningnextRun()- Get the next run time
License
MIT
