Skip to content

Class: KyooWorker<T, M>

The class responsible for receiving and processing jobs from a queue. A worker can be created by using KyooQueue.worker.

Remarks

While it is possible to retrieve jobs directly from the queue, this is the preferred method for creating conumer workers which process jobs in a consistent manner.

Type Parameters

Type ParameterDefault typeDescription
T extends KyooClient-The type of the Queue Broker client. Options are amqp, bullmq and sqs.
MKyooSerializableThe type of the job payload. This is a generic type which extends KyooSerializable.

Accessors

name

Get Signature

ts
get name(): string;

The name of the worker.

Returns

string


running

Get Signature

ts
get running(): boolean;

Indicates whether the worker is running or not.

Returns

boolean


terminated

Get Signature

ts
get terminated(): boolean;

Indicates whether the worker has been terminated or not.

Returns

boolean


version

Get Signature

ts
get version(): string;

The version of the library

Returns

string

The version of the library


version

Get Signature

ts
get static version(): string;

The version of the library

Returns

string

The version of the library

Methods

addListener()

ts
addListener<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;

Alias for KyooWorker.on(eventName, listener).

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterType
eventNameK
listenerKyooWorkerToApplicationEventEmitterListener<K>

Returns

this


off()

ts
off<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;

Alias for KyooWorker.removeListener().

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterType
eventNameK
listenerKyooWorkerToApplicationEventEmitterListener<K>

Returns

this


on()

ts
on<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the KyooWorker, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The KyooWorker.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooWorkerToApplicationEventEmitterListener<K>The callback function

Returns

this


once()

ts
once<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the KyooWorker, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The KyooWorker.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooWorkerToApplicationEventEmitterListener<K>The callback function

Returns

this


pause()

ts
pause(): Promise<void>;

Pauses / Stops processing jobs.

Returns

Promise<void>


prependListener()

ts
prependListener<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the KyooWorker, so that calls can be chained.

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooWorkerToApplicationEventEmitterListener<K>The callback function

Returns

this


prependOnceListener()

ts
prependOnceListener<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;

Adds a one-timelistener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the KyooWorker, so that calls can be chained.

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooWorkerToApplicationEventEmitterListener<K>The callback function

Returns

this


processNext()

ts
processNext(count: number, signal?: AbortSignal): Promise<boolean>;

Manually process next jobs using the worker's processor function.

Parameters

ParameterTypeDefault valueDescription
countnumber1The number of jobs to process. If count is less than or equal to 0, the method will return immediately.
signal?AbortSignalundefinedAn optional AbortSignal to abort fetching jobs to be processed and processing jobs where possible.

Returns

Promise<boolean>


removeAllListeners()

ts
removeAllListeners<K>(eventName?: K): this;

Removes all listeners, or those of the specified eventName.

It is bad practice to remove listeners added elsewhere in the code, particularly when the KyooWorker instance was created by some other component or module.

Returns a reference to the KyooWorker, so that calls can be chained.

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterType
eventName?K

Returns

this


removeListener()

ts
removeListener<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;

Removes the specified listener from the listener array for the event named eventName.

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() or removeAllListeners() calls after emitting and before the last listener finishes execution will not remove them fromemit() in progress. Subsequent events behave as expected.

Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called.

When a single function has been added as a handler multiple times for a single event, removeListener() will remove the most recently added instance.

Returns a reference to the KyooWorker, so that calls can be chained.

Type Parameters

Type Parameter
K extends keyof KyooWorkerEvents

Parameters

ParameterType
eventNameK
listenerKyooWorkerToApplicationEventEmitterListener<K>

Returns

this


resume()

ts
resume(): Promise<void>;

Starts or Resumes processing jobs.

Returns

Promise<void>