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 () {
return true // returning `false` will prevent the task from enqueueing
},
postEnqueue: async function () {
log("Task successfully enqueued!")
}
}
api.tasks.addMiddleware(middleware)

Hierarchy

  • TaskMiddleware

Properties

global: boolean

Is this middleware applied to all tasks?

name: string

Unique name for the middleware.

postEnqueue?: (() => boolean)

Type declaration

    • (): boolean
    • Called after a task using this middleware is enqueued.

      Returns boolean

postProcessor?: (() => boolean)

Type declaration

    • (): boolean
    • Called after the task runs.

      Returns boolean

preEnqueue?: (() => Promise<boolean>)

Type declaration

    • (): Promise<boolean>
    • Called before a task using this middleware is enqueued.

      Returns Promise<boolean>

preProcessor?: (() => boolean)

Type declaration

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

      Returns boolean

priority?: number

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

Generated using TypeDoc