Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Queues

Queues are the final aspect of Payload's Jobs Queue and deal with how to run your jobs. Up to this point, all we've covered is how to queue up jobs to run, but so far, we aren't actually running any jobs.

When you go to run jobs, Payload will query for any jobs that are added to the queue and then run them. By default, all queued jobs are added to the default queue.

But, imagine if you wanted to have some jobs that run nightly, and other jobs which should run every five minutes.

By specifying the queue name when you queue a new job using payload.jobs.queue(), you can queue certain jobs with queue: 'nightly', and other jobs can be left as the default queue.

Then, you could configure two different runner strategies:

  1. A cron that runs nightly, querying for jobs added to the nightly queue
  2. Another that runs any jobs that were added to the default queue every ~5 minutes or so

Executing jobs

As mentioned above, you can queue jobs, but the jobs won't run unless a worker picks up your jobs and runs them. This can be done in two ways:

Endpoint

You can execute jobs by making a fetch request to the /api/payload-jobs/run endpoint:

1
// Here, we're saying we want to run only 100 jobs for this invocation
2
// and we want to pull jobs from the `nightly` queue:
3
await fetch('/api/payload-jobs/run?limit=100&queue=nightly', {
4
method: 'GET',
5
headers: {
6
'Authorization': `Bearer ${token}`,
7
},
8
});

This endpoint is automatically mounted for you and is helpful in conjunction with serverless platforms like Vercel, where you might want to use Vercel Cron to invoke a serverless function that executes your jobs.

Vercel Cron Example

If you're deploying on Vercel, you can add a vercel.json file in the root of your project that configures Vercel Cron to invoke the run endpoint on a cron schedule.

Here's an example of what this file will look like:

1
{
2
"crons": [
3
{
4
"path": "/api/payload-jobs/run",
5
"schedule": "*/5 * * * *"
6
}
7
]
8
}

The configuration above schedules the endpoint /api/payload-jobs/run to be invoked every 5 minutes.

The last step will be to secure your run endpoint so that only the proper users can invoke the runner.

To do this, you can set an environment variable on your Vercel project called CRON_SECRET, which should be a random string—ideally 16 characters or longer.

Then, you can modify the access function for running jobs by ensuring that only Vercel can invoke your runner.

1
export default buildConfig({
2
// Other configurations...
3
jobs: {
4
access: {
5
run: ({ req }: { req: PayloadRequest }): boolean => {
6
// Allow logged in users to execute this endpoint (default)
7
if (req.user) return true
8
9
// If there is no logged in user, then check
10
// for the Vercel Cron secret to be present as an
11
// Authorization header:
12
const authHeader = req.headers.get('authorization');
13
return authHeader === `Bearer ${process.env.CRON_SECRET}`;
14
},
15
},
16
// Other job configurations...
17
}
18
})

This works because Vercel automatically makes the CRON_SECRET environment variable available to the endpoint as the Authorization header when triggered by the Vercel Cron, ensuring that the jobs can be run securely.

After the project is deployed to Vercel, the Vercel Cron job will automatically trigger the /api/payload-jobs/run endpoint in the specified schedule, running the queued jobs in the background.

Local API

If you want to process jobs programmatically from your server-side code, you can use the Local API:

Run all jobs:

1
const results = await payload.jobs.run()
2
3
// You can customize the queue name and limit by passing them as arguments:
4
await payload.jobs.run({ queue: 'nightly', limit: 100 })
5
6
// You can provide a where clause to filter the jobs that should be run:
7
await payload.jobs.run({ where: { 'input.message': { equals: 'secret' } } })

Run a single job:

1
const results = await payload.jobs.runByID({
2
id: myJobID
3
})

Bin script

Finally, you can process jobs via the bin script that comes with Payload out of the box.

1
npx payload jobs:run --queue default --limit 10

In addition, the bin script allows you to pass a --cron flag to the jobs:run command to run the jobs on a scheduled, cron basis:

1
npx payload jobs:run --cron "*/5 * * * *"
Next

TypeScript - Overview