What is a Scheduled Job?
A scheduled job:- Runs automatically on a defined schedule (cron expression)
- Executes in the background without blocking requests
- Has access to the dependency injection container
- Can execute workflows and access services
- Is defined using a configuration export
Creating a Basic Scheduled Job
Create the Job File
Create a file in
src/jobs/ with a default export function and schedule configuration:src/jobs/cleanup-expired-brands.ts
Cron Schedule Syntax
Cron expressions define when jobs run:Common Examples
Real-World Examples
Cleanup Inactive Data
src/jobs/cleanup-inactive-brands.ts
Sync External Data
src/jobs/sync-external-inventory.ts
Generate Reports
src/jobs/generate-weekly-report.ts
Cache Warming
src/jobs/warm-product-cache.ts
Job Configuration Options
Error Handling
Always handle errors in scheduled jobs:Monitoring and Logging
Use structured logging for monitoring:Best Practices
- Use try-catch blocks to handle errors gracefully
- Log job start, completion, and errors with structured data
- Keep jobs idempotent (safe to run multiple times)
- Use workflows for complex operations instead of putting logic in jobs
- Set appropriate schedules - don’t run jobs more frequently than needed
- Consider timezone implications of cron schedules
- Test job logic independently before deploying
- Monitor job execution and set up alerts for failures
- Use soft deletes when cleaning up data
- Be cautious with bulk operations - consider batching
Testing Scheduled Jobs
Test your job logic independently:Disabling Jobs
To temporarily disable a job, you can:- Comment out the job file
- Rename it to not match the job file pattern
- Add a condition to skip execution:
Next Steps
Event Subscribers
React to events instead of schedules
Create Workflows
Build complex job logic as workflows