Skip to main content
Scheduled jobs allow you to run background tasks on a recurring schedule. They’re useful for cleanup operations, data synchronization, report generation, and other periodic maintenance tasks.

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

1

Create the Job File

Create a file in src/jobs/ with a default export function and schedule configuration:
src/jobs/cleanup-expired-brands.ts
2

Access Services

Resolve services from the container:
3

Execute Workflows

Run workflows from scheduled jobs:

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:
  1. Comment out the job file
  2. Rename it to not match the job file pattern
  3. Add a condition to skip execution:

Next Steps

Event Subscribers

React to events instead of schedules

Create Workflows

Build complex job logic as workflows