On this page

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):

PropertyDescription

name R

A unique name for the job.

callback R

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.

interval

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 false, so the job can only be manually triggered.

priority

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.

beforeProcess

A function that is executed before the job is processed. It accepts a context argument that includes the resolved job definition and callback args.

afterProcess

A function that is executed after the job is processed. It accepts a context argument that includes the resolved job definition and callback args, processedAt and duration timestamps. Additionally, it includes a success parameter with an optional response or error depending on the success of the job processing.

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:

JobDescription

clean-expired-previews

This job deletes records from the previews collection that haven't been updated in the last 24 hours. It runs every 30 minutes.

clean-expired-tokens

This job deletes expired JSON web tokens every 30 minutes.

publish-pages

This job searches for and publishes scheduled pages (and other records from page-like collections) every minute.

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