Quick Start Example
Let's walk through a practical example of setting up a simple job queue. We'll create a task that sends a welcome email when a user signs up.
You might wonder: "Why not just send the email directly in the afterChange hook?"
- Non-blocking: If your email service takes 2-3 seconds to send, your API response would be delayed. With jobs, the API returns immediately.
- Resilience: If the email service is temporarily down, the hook would fail and potentially block the user creation. Jobs can retry automatically.
- Scalability: As your app grows, you can move job processing to dedicated servers, keeping your API fast.
- Monitoring: All jobs are tracked in the database, so you can see if emails failed and why.
Now let's build this example step by step.
Step 1: Define a Task
First, create a task in your payload.config.ts:
This defines a reusable task with a unique slug, an inputSchema that validates and types the input data, and a handler function containing the work to be performed. The retries option ensures the task will automatically retry up to 3 times if it fails. Learn more about Tasks.
Step 2: Queue the Job trigger
This uses payload.jobs.queue() to create a job instance from the task definition. The job is added to the queue immediately but runs asynchronously, so the API response returns right away without waiting for the email to send. Jobs are stored in the database as documents in the payload-jobs collection.
Step 3: Run the Jobs
The autoRun configuration automatically processes queued jobs on a schedule using cron syntax. In this example, Payload checks for pending jobs every 5 minutes and executes them. Alternatively, you can manually trigger job processing with payload.jobs.run() or use the API endpoint for serverless platforms.
That's it! Now when users sign up, a job is queued and will be processed within 5 minutes without blocking the API response.
Example 2: Recurring Scheduled Job
The previous example showed manual job queuing (jobs triggered by user actions). Now let's look at a job that runs automatically on a schedule without any user action.
We'll create a task that generates a daily analytics report every morning at 8 AM.
Why Use Scheduled Jobs?
- Automated recurring tasks: No need to manually trigger them
- Predictable timing: Reports, cleanups, syncs run at exact times
- No manual intervention: Set it once and forget it
Step 1: Define a Task with Schedule
The schedule property defines when this job should run (every day at 8 AM).
Step 2: Configure the Job Runner
To actually queue and execute scheduled jobs, you need to configure the autoRun property. This handles both queuing jobs based on their schedule and running them:
How It Works
Here's the complete flow:
- At 8:00 AM: The
scheduleconfiguration automatically queues a new job in the 'reports' queue - Within 1 minute: The
autoRuncron checks the 'reports' queue and finds the job - Execution: The job runs and generates the report
- The next day: The process repeats automatically at 8:00 AM
Complete Configuration
Here's the full config with both the task and runner:
When to Use Each Approach
Approach | When to Use | Example |
|---|---|---|
Manual Queuing | Jobs triggered by user actions or data changes | Welcome emails, payment processing, notifications |
Scheduled Jobs | Jobs that run automatically at regular intervals | Daily reports, weekly cleanups, nightly syncs |
Scheduled with Future | One-time job in the future | Publish post at 3pm tomorrow, trial expiry reminders |
For scheduled jobs with waitUntil:
This is different from the schedule property, which repeats automatically.
See Job Schedules for more details on scheduling options and advanced patterns.
Was this page helpful?