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 Parameter | Default type | Description |
---|---|---|
T extends KyooClient | - | The type of the Queue Broker client. Options are amqp , bullmq and sqs . |
M | KyooSerializable | The type of the job payload. This is a generic type which extends KyooSerializable . |
Accessors
name
Get Signature
get name(): string;
The name of the worker.
Returns
string
running
Get Signature
get running(): boolean;
Indicates whether the worker is running or not.
Returns
boolean
terminated
Get Signature
get terminated(): boolean;
Indicates whether the worker has been terminated or not.
Returns
boolean
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;
Alias for KyooWorker.on(eventName, listener)
.
Type Parameters
Type Parameter |
---|
K extends keyof KyooWorkerEvents |
Parameters
Parameter | Type |
---|---|
eventName | K |
listener | KyooWorkerToApplicationEventEmitterListener <K > |
Returns
this
off()
off<K>(eventName: K, listener: KyooWorkerToApplicationEventEmitterListener<K>): this;
Alias for KyooWorker.removeListener()
.
Type Parameters
Type Parameter |
---|
K extends keyof KyooWorkerEvents |
Parameters
Parameter | Type |
---|---|
eventName | K |
listener | KyooWorkerToApplicationEventEmitterListener <K > |
Returns
this
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
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
pause()
pause(): Promise<void>;
Pauses / Stops processing jobs.
Returns
Promise
<void
>
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
prependOnceListener()
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
Parameter | Type | Description |
---|---|---|
eventName | K | The name of the event. |
listener | KyooWorkerToApplicationEventEmitterListener <K > | The callback function |
Returns
this
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 | The number of jobs to process. If count is less than or equal to 0, the method will return immediately. |
signal ? | AbortSignal | undefined | An optional AbortSignal to abort fetching jobs to be processed and processing jobs where possible. |
Returns
Promise
<boolean
>
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 |
---|---|
eventName ? | K |
Returns
this
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 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
Parameter | Type |
---|---|
eventName | K |
listener | KyooWorkerToApplicationEventEmitterListener <K > |
Returns
this
resume()
resume(): Promise<void>;
Starts or Resumes processing jobs.
Returns
Promise
<void
>