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 consumer workers which process jobs in a consistent manner.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
T extends KyooClient | - | Broker client: amqp, bullmq, sqs, pgboss, or sql. |
M | KyooSerializable | Serializable job payload type. |
Accessors
name
Get Signature
get name(): string;Reads the worker identifier used in processor messages.
Returns
string
The name assigned when the worker was created.
running
Get Signature
get running(): boolean;Indicates whether continuous polling is enabled and the worker is not terminated.
Returns
boolean
True while polling is enabled, including between tick iterations.
terminated
Get Signature
get terminated(): boolean;Indicates whether the worker's lifetime cancellation signal has been aborted.
Returns
boolean
True after the parent queue terminates this worker.
version
Get Signature
get version(): string;The version of the library
Returns
string
The version of the library
version
Get Signature
get static version(): string;The version of the library
Returns
string
The version of the library
Methods
addListener()
addListener<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;Registers an application listener; equivalent to KyooWorker.on.
Type Parameters
| Type Parameter |
|---|
K extends keyof KyooWorkerEvents |
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | K | Worker event to observe. |
listener | KyooWorkerToApplicationEventEmitterListener<K> | Callback invoked with the event's arguments. |
Returns
this
This worker for chaining.
Throws
when the worker has been terminated.
off()
off<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;Removes one listener registration; equivalent to removeListener.
Type Parameters
| Type Parameter |
|---|
K extends keyof KyooWorkerEvents |
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | K | Worker event whose listener should be removed. |
listener | KyooWorkerToApplicationEventEmitterListener<K> | Registered callback to remove. |
Returns
this
This worker for chaining.
on()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooWorkerToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This worker for chaining.
Throws
when the worker has been terminated.
once()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooWorkerToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This worker for chaining.
Throws
when the worker has been terminated.
pause()
pause(): Promise<void>;Stops future polling without cancelling an in-progress processor.
Returns
Promise<void>
A promise fulfilled after polling is disabled and the paused event is emitted.
Throws
when the worker has been terminated.
prependListener()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooWorkerToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This worker for chaining.
Throws
when the worker has been terminated.
prependOnceListener()
prependOnceListener<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;Adds a one-time listener 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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooWorkerToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This worker for chaining.
Throws
when the worker has been terminated.
processNext()
processNext(count: number, signal?: AbortSignal): Promise<boolean>;Manually process next jobs using the worker's processor function.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
count | number | 1 | Maximum jobs to retrieve and process; defaults to 1. Values at or below zero return false immediately. |
signal? | AbortSignal | undefined | An optional AbortSignal that stops fetching further jobs; it does not cancel an active processor. |
Returns
Promise<boolean>
True if the requested number of jobs was retrieved; false for a non-positive count or cancellation before that target.
Throws
when the worker has already been terminated.
removeAllListeners()
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
| Parameter | Type | Description |
|---|---|---|
eventName? | K | Event whose listeners to clear, or omit to clear all events. |
Returns
this
This worker for chaining.
removeListener()
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 from emit() 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
| Parameter | Type | Description |
|---|---|---|
eventName | K | Worker event whose listener should be removed. |
listener | KyooWorkerToApplicationEventEmitterListener<K> | Callback whose most recent registration will be removed. |
Returns
this
This worker for chaining.
resume()
resume(): Promise<void>;Starts or resumes the 10 ms polling loop.
Returns
Promise<void>
A promise fulfilled once a tick has begun, or immediately if one is already active.
Throws
when the worker has been terminated.
