start/scheduler.ts file, you can use the @schedule decorator to define schedules directly on your command classes. This approach keeps scheduling configuration close to the command implementation.
Basic usage
Import the@schedule decorator from the scheduler package and apply it to your command class:
Passing command arguments
If your command requires arguments, pass them as the second parameter to the decorator:Using the fluent API
Instead of a cron expression, you can pass a callback function that receives a schedule instance. This allows you to use the fluent frequency API:ScheduleCommand instance that you can chain with any frequency method:
Combining with command arguments
When using the fluent API, you can still pass command arguments as the second parameter:Multiple schedules
You can apply the@schedule decorator multiple times to run the same command on different schedules:
purge:users command will run:
- Every minute with the argument
'30 days' - Every five seconds (and immediately on startup) with the argument
'7 days' - Every minute with the argument
'42 days'
Decorator vs file-based scheduling
Both approaches are valid and can be used together in the same application: Decorator approach:- Schedules are defined close to the command implementation
- Useful for commands that are always scheduled
- Makes the command’s scheduling behavior immediately visible
- Schedules are centralized in
start/scheduler.ts - Provides a single place to view all scheduled tasks
- Easier to modify schedules without touching command files
- Better for commands that may or may not be scheduled depending on environment