Class TemporalWorker
- Namespace
- Temporalio.Worker
- Assembly
- Temporalio.dll
Worker for running Temporal workflows and/or activities. This intentionally matches
Microsoft.Extensions.Hosting.BackgroundService
structure.
public class TemporalWorker : IDisposable
- Inheritance
-
TemporalWorker
- Implements
- Inherited Members
Constructors
TemporalWorker(IWorkerClient, TemporalWorkerOptions)
Initializes a new instance of the TemporalWorker class. The options must have a task queue set and at least one workflow or activity.
public TemporalWorker(IWorkerClient client, TemporalWorkerOptions options)
Parameters
client
IWorkerClientClient for this workflow. This is implemented by the commonly used ITemporalClient.
options
TemporalWorkerOptionsOptions for the worker.
Properties
Client
Gets or sets the client for this worker.
public IWorkerClient Client { get; set; }
Property Value
Remarks
When this property is set, it actually replaces the underlying client that is being used by the worker. This means subsequent calls by the worker to Temporal (e.g. responding task completion, activity heartbeat, etc) will be on this new client, but outstanding calls will not be immediately interrupted.
Options
Gets the options this worker was created with.
public TemporalWorkerOptions Options { get; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Dispose the worker.
protected virtual void Dispose(bool disposing)
Parameters
disposing
boolWhether disposing.
ExecuteAsync(Func<Task>, CancellationToken)
Run this worker until failure, cancelled, or task from given function completes.
public Task ExecuteAsync(Func<Task> untilComplete, CancellationToken stoppingToken = default)
Parameters
untilComplete
Func<Task>If the task returned from this function completes, the worker will shutdown propagating exception as necessary.
stoppingToken
CancellationTokenCancellation token to stop the worker.
Returns
- Task
When the task is complete, the worker has completed shutdown.
Remarks
When shutting down, the worker will cancel and wait for completion for all executing activities. If an activity does not properly respond to cancellation, this may never return.
Exceptions
- InvalidOperationException
Already started.
- OperationCanceledException
Cancellation requested.
- Exception
Fatal worker failure.
ExecuteAsync(CancellationToken)
Run this worker until failure or cancelled.
public Task ExecuteAsync(CancellationToken stoppingToken)
Parameters
stoppingToken
CancellationTokenCancellation token to stop the worker.
Returns
- Task
Task that will never succeed, only fail. When the task is complete, the worker has completed shutdown.
Remarks
This intentionally matches
Microsoft.Extensions.Hosting.BackgroundService.ExecuteAsync
.
When shutting down, the worker will cancel and wait for completion for all executing activities. If an activity does not properly respond to cancellation, this may never return.
Exceptions
- InvalidOperationException
Already started.
- OperationCanceledException
Cancellation requested.
- Exception
Fatal worker failure.
ExecuteAsync<TResult>(Func<Task<TResult>>, CancellationToken)
Run this worker until failure, cancelled, or task from given function completes.
public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> untilComplete, CancellationToken stoppingToken = default)
Parameters
untilComplete
Func<Task<TResult>>If the task returned from this function completes, the worker will shutdown (propagating) exception as necessary.
stoppingToken
CancellationTokenCancellation token to stop the worker.
Returns
- Task<TResult>
When the task is complete, the worker has completed shutdown.
Type Parameters
TResult
Result of given function's task.
Remarks
When shutting down, the worker will cancel and wait for completion for all executing activities. If an activity does not properly respond to cancellation, this may never return.
Exceptions
- InvalidOperationException
Already started.
- OperationCanceledException
Cancellation requested.
- Exception
Fatal worker failure.
~TemporalWorker()
Finalizes an instance of the TemporalWorker class.
protected ~TemporalWorker()