Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface TaskMiddleware

An example middleware

const middleware = {
  name: 'timer',
  global: true,
  priority: 90,
  preProcessor: async function () {
    const worker = this.worker
    worker.startTime = process.hrtime()
  },
  postProcessor: async function () {
    const worker = this.worker
    const elapsed = process.hrtime(worker.startTime)
    const seconds = elapsed[0]
    const millis = elapsed[1] / 1000000
    log(worker.job.class + ' done in ' + seconds + ' s and ' + millis + ' ms.', 'info')
  },
  preEnqueue: async function () {
    const arg = this.args[0]
    return (arg === 'ok') // returning `false` will prevent the task from enqueueing
  },
  postEnqueue: async function () {
    log("Task successfully enqueued!")
  }
}
api.tasks.addMiddleware(middleware)

Hierarchy

  • TaskMiddleware

Index

Properties

global

global: boolean

Is this middleware applied to all tasks?

name

name: string

Unique name for the middleware.

Optional postEnqueue

postEnqueue: Function

Called after a task using this middleware is enqueued.

Optional postProcessor

postProcessor: Function

Called after the task runs.

Optional preEnqueue

preEnqueue: Function

Called before a task using this middleware is enqueued.

Optional preProcessor

preProcessor: Function

Called berore the task runs. Has access to all params, before sanitization. Can modify the data object for use in tasks.

Optional priority

priority: number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

Generated using TypeDoc