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)
interface TaskMiddleware {
    global: boolean;
    name: string;
    postEnqueue?: (() => boolean);
    postProcessor?: (() => boolean);
    preEnqueue?: (() => Promise<boolean>);
    preProcessor?: (() => boolean);
    priority?: number;
}

Properties

global: boolean

Is this middleware applied to all tasks?

name: string

Unique name for the middleware.

postEnqueue?: (() => boolean)

Called after a task using this middleware is enqueued.

Type declaration

    • (): boolean
    • Returns boolean

postProcessor?: (() => boolean)

Called after the task runs.

Type declaration

    • (): boolean
    • Returns boolean

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

Called before a task using this middleware is enqueued.

Type declaration

    • (): Promise<boolean>
    • Returns Promise<boolean>

preProcessor?: (() => boolean)

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

Type declaration

    • (): boolean
    • Returns boolean

priority?: number

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