Job queues
Jobs are asynchronous tasks used to perform specific actions in the CMS at a scheduled time or at regular intervals. These tasks typically involve sending emails, clearing CDN cache, synchronizing data with remote servers, and other resource-intensive operations that could impact the response time of a single HTTP request.
Example
You can define jobs in the jobs
directory located in the root of your project. Here's an example:
# jobs/send-welcome-email.ts
import { defineJob } from '#pruvious'
export default defineJob({
name: 'send-welcome-email',
callback: async (emailAddress: string) => {
// Send welcome message to `emailAddress`
},
priority: 20,
})
Now you can add a job to the processing queue:
# examples/queue-job.ts
import { queueJob } from '#pruvious/server'
await queueJob('send-welcome-email', 'hello@pruvious.com')
Definition
You can use the defineJob
function to define the following properties (required properties are marked with the symbol R):
Property | Description |
---|---|
| A unique name for the job. |
| A function executed when the job is processed. It contains the entire job logic and can accept any number of arguments, return any value, and throw errors. |
| A self-invoking interval in seconds that triggers the processing of this job. The callback function is invoked without any arguments every interval seconds. By default, the interval is set to |
| Controls the processing priority of jobs in the queue. Jobs with higher priority values will be processed first. If priorities are equal, older jobs take precedence. |
| A function that is executed before the job is processed. It accepts a |
| A function that is executed after the job is processed. It accepts a |
Queue interval
The job queue is processed at a specified time interval, which you can customize in your nuxt.config.ts
file.
# nuxt.config.ts
export default defineNuxtConfig({
modules: ['pruvious'],
pruvious: {
jobs: {
searchInterval: 60, // 1 minute
},
},
})
To disable automatic queue processing, set the jobs
property to false
.
Triggers
Jobs without an enabled interval
must be called manually using the processJob
utility function. Here's an example:
# examples/process-job.ts
import { processJob } from '#pruvious/server'
await processJob('send-welcome-email', 'hello@pruvious.com')
To immediately process the job queue, use the processJobQueue
utility. This function will continuously process jobs until there are no more left in the queue.
# examples/process-job-queue.ts
import { processJobQueue } from '#pruvious/server'
await processJobQueue()
Standard jobs
Pruvious defines the following jobs by default:
Job | Description |
---|---|
| This job deletes records from the |
| This job deletes expired JSON web tokens every 30 minutes. |
| This job searches for and publishes scheduled |
You can disable these jobs in your nuxt.config.ts
file by setting their values to false
. Here's an example:
# nuxt.config.ts
export default defineNuxtConfig({
modules: ['pruvious'],
pruvious: {
standardJobs: {
"clean-expired-previews": false,
"clean-expired-tokens": false,
"publish-pages": false,
}
},
})
Last updated on March 18, 2024 at 17:59