Class: Worker
worker.Worker
The temporal Worker connects to Temporal Server and runs Workflows and Activities.
Properties
options
• Readonly
options: CompiledWorkerOptions
Accessors
numInFlightActivations$
• get
numInFlightActivations$(): Observable
<number
>
An Observable which emits each time the number of in flight activations changes
Returns
Observable
<number
>
numInFlightActivities$
• get
numInFlightActivities$(): Observable
<number
>
An Observable which emits each time the number of in flight Activity tasks changes
Returns
Observable
<number
>
numRunningWorkflowInstances$
• get
numRunningWorkflowInstances$(): Observable
<number
>
An Observable which emits each time the number of cached workflows changes
Returns
Observable
<number
>
Methods
getState
▸ getState(): State
Get the poll state of this worker
Returns
getStatus
▸ getStatus(): WorkerStatus
Get a status overview of this Worker
Returns
run
▸ run(): Promise
<void
>
Start polling on the Task Queue for tasks. Completes after graceful shutdown, once the Worker reaches the
'STOPPED'
state.
Throws on a fatal error or failure to shutdown gracefully.
Returns
Promise
<void
>
See
To stop polling, call shutdown or send one of Runtime.options.shutdownSignals.
runUntil
▸ runUntil<R
>(fnOrPromise
): Promise
<R
>
Run the Worker until fnOrPromise
completes. Then shutdown and wait for run to complete.
Type parameters
Name |
---|
R |
Parameters
Name | Type |
---|---|
fnOrPromise | Promise <R > | () => Promise <R > |
Returns
Promise
<R
>
the result of fnOrPromise
Throws on fatal Worker errors.
SDK versions < 1.5.0
:
This method would not wait for worker to complete shutdown if the inner fnOrPromise
threw an error.
SDK versions >=1.5.0
:
This method always waits for both worker shutdown and inner fnOrPromise
to resolve.
If one of worker run -or- the inner promise throw an error, that error is rethrown.
If both throw an error, a CombinedWorkerRunError with a cause
attribute containing both errors.
shutdown
▸ shutdown(): void
Start shutting down the Worker. The Worker stops polling for new tasks and sends
cancellation (via a
CancelledFailure with message
set to 'WORKER_SHUTDOWN'
) to running Activities. Note: if the Activity
accepts cancellation (i.e. re-throws or allows the CancelledFailure
to be thrown out of the Activity function),
the Activity Task will be marked as failed, not cancelled. It's helpful for the Activity Task to be marked failed
during shutdown because the Server will retry the Activity sooner (than if the Server had to wait for the Activity
Task to time out).
When called, immediately transitions state to 'STOPPING'
and asks Core to shut down. Once Core has
confirmed that it's shutting down, the Worker enters 'DRAINING'
state unless the Worker has already been
'DRAINED'
. Once all currently running Activities and Workflow Tasks have completed, the Worker transitions to
'STOPPED'
.
Returns
void
create
▸ create(options
): Promise
<Worker
>
Create a new Worker. This method initiates a connection to the server and will throw (asynchronously) on connection failure.
Parameters
Name | Type |
---|---|
options | WorkerOptions |
Returns
Promise
<Worker
>
runReplayHistories
▸ runReplayHistories(options
, histories
): AsyncIterableIterator
<ReplayResult
>
Create a replay Worker, running all histories provided by the passed in iterable.
Returns an async iterable of results for each history replayed.
Parameters
Name | Type |
---|---|
options | ReplayWorkerOptions |
histories | ReplayHistoriesIterable |
Returns
AsyncIterableIterator
<ReplayResult
>
runReplayHistory
▸ runReplayHistory(options
, history
, workflowId?
): Promise
<void
>
Create a replay Worker, and run the provided history against it. Will resolve as soon as the history has finished being replayed, or if the workflow produces a nondeterminism error.
Parameters
Name | Type | Description |
---|---|---|
options | ReplayWorkerOptions | - |
history | unknown | - |
workflowId? | string | If provided, use this as the workflow id during replay. Histories do not contain a workflow id, so it must be provided separately if your workflow depends on it. |
Returns
Promise
<void
>
Throws
DeterminismViolationError if the workflow code is not compatible with the history.
Throws
ReplayError on any other replay related error.