Node.js - schedule.every()
Sets the frequency and one or many handlers to be triggered.
import { schedule } from '@nitric/sdk'
// Create a schedule that runs every 3 hours
schedule('send-reminder').every('3 hours', async (ctx) => {
// do some processing
})
Parameters
- Name
rate
- Required
- Required
- Type
- string
- Description
The rate to run the schedule, e.g., '7 days'. All rates accept a number and a frequency. Valid frequencies are 'days', 'hours', or 'minutes'.
- Name
middleware
- Required
- Required
- Type
- EventMiddleware or EventMiddleware[]
- Description
One or more middleware services to use as the handler which will run on the defined frequency.
Examples
Create a Schedule to run every 3 minutes
import { schedule } from '@nitric/sdk'
// Create a schedule that runs every 3 minutes
schedule('send-reminder').every('3 minutes', async (ctx) => {
// code which sends a reminder
})
Create a Schedule with multiple middleware/handlers
import { schedule, EventContext } from '@nitric/sdk'
// Create a middleware to handle report generation
async function generateReport(ctx: EventContext): Promise<void> {
// code which generates a report
}
// Create a middleware to handle notifications
async function sendNotification(ctx: EventContext): Promise<void> {
// code which sends a notification
}
// Create a schedule that runs every 7 days
schedule('release').every('7 days', generateReport, sendNotification)