Class WorkManager
- Namespace
- Virtufin.WorkManager.Components
- Assembly
- Virtufin.WorkManager.dll
Manages worker lifecycle, subscriptions, and processing.
public sealed class WorkManager : IAsyncDisposable, IWorkerRecoveryExecutor
- Inheritance
-
WorkManager
- Implements
- Inherited Members
Constructors
WorkManager(PubsubClient, StateClient, IHttpClientFactory, IEngineRegistry, ILogger<WorkManager>, ILoggerFactory, DaprResiliencePipeline, WorkerProcessingRetryPipeline, ResilientDaprPublisher, ApiLifecyclePublisher, IInstanceIdProvider, string?, string?)
Creates a new WorkManager instance.
public WorkManager(Pubsub.PubsubClient pubsubClient, State.StateClient stateClient, IHttpClientFactory httpClientFactory, IEngineRegistry engineRegistry, ILogger<WorkManager> logger, ILoggerFactory loggerFactory, DaprResiliencePipeline resilience, WorkerProcessingRetryPipeline workerRetry, ResilientDaprPublisher publisher, ApiLifecyclePublisher lifecyclePublisher, IInstanceIdProvider instanceIdProvider, string? stateStoreName = null, string? allowedCodeSourceHosts = null)
Parameters
pubsubClientPubsub.PubsubClientstateClientState.StateClienthttpClientFactoryIHttpClientFactoryengineRegistryIEngineRegistryloggerILogger<WorkManager>loggerFactoryILoggerFactoryresilienceDaprResiliencePipelineworkerRetryWorkerProcessingRetryPipelinepublisherResilientDaprPublisherlifecyclePublisherApiLifecyclePublisherinstanceIdProviderIInstanceIdProviderstateStoreNamestringallowedCodeSourceHostsstring
Fields
CodeFetcherHttpClientName
public const string CodeFetcherHttpClientName = "CodeFetcher"
Field Value
DeadLetterTopicSuffix
Suffix appended to the topic name for dead-letter messages that have exhausted retries.
public const string DeadLetterTopicSuffix = "-dead"
Field Value
DefaultCodeFetchTimeoutSeconds
public const int DefaultCodeFetchTimeoutSeconds = 30
Field Value
ProbeEventType
CloudEvent type marking a topic liveness probe. Probes carry the publishing instance id in the ProbeInstanceExtension CloudEvent extension attribute; the handler echoes probes carrying its own id and drops all probes without dispatching them to workers. The marker must travel as CloudEvent attributes inside the payload — NOT as Dapr publish metadata — because the streamer populates TopicMessage.Type/Extensions from the CloudEvent envelope parsed out of the stored payload, not from the broker-level metadata.
public const string ProbeEventType = "com.virtufin.workmanager.topic-probe"
Field Value
ProbeInstanceExtension
public const string ProbeInstanceExtension = "wmtopicprobe"
Field Value
PublishTopicAttribute
Extension attribute a worker's response CloudEvent can carry to
publish to a topic other than its own ce-type. Most workers
don't need this -- their response's Type
serves as both the wire ce-type and the publish topic, which
is why that's still the default. It exists for workers whose
ce-type must stay a stable identifier independent of a
per-scenario topic (e.g. the pubsub-topics spec's trading events:
topic sc.<scenarioId>.trading.order.submitted, ce-type
com.virtufin.trading.order.submitted). Stripped from the
outgoing envelope before publish -- it's WorkManager-internal
routing, not part of the event a subscriber should see.
public const string PublishTopicAttribute = "publishtopic"
Field Value
UnknownCloudEventSourceUri
public static readonly Uri UnknownCloudEventSourceUri
Field Value
UnknownCloudEventType
public const string UnknownCloudEventType = "unknown"
Field Value
Methods
CreateWorkerAsync(CreateWorkerRequest, SourceCredential?, CancellationToken)
Creates a new worker and starts processing.
public Task<Guid> CreateWorkerAsync(CreateWorkerRequest request, SourceCredential? credential = null, CancellationToken cancellationToken = default)
Parameters
requestCreateWorkerRequestThe worker creation request.
credentialSourceCredentialOptional, transient per-request fetch credential (see Virtufin.WorkManager.Protos.SourceCredential). Never persisted -- used only for this creation's own fetch. A
namedreference is instead captured onto NamedCredential by the caller (the gRPC service layer) beforerequestreaches here, so it survives into persisted state for recovery.cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- EngineNotFoundException
Thrown if no engine is registered for the requested MIME type.
DeleteWorkerAsync(Guid, CancellationToken)
Deletes a worker and its subscriptions.
public Task DeleteWorkerAsync(Guid id, CancellationToken cancellationToken = default)
Parameters
idGuidThe worker ID.
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- WorkerNotFoundException
Thrown if the worker is not found.
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
GetTag(Guid, string)
Gets the worker's current value for a single tag.
public string? GetTag(Guid id, string key)
Parameters
Returns
GetTags(Guid)
Returns a snapshot of the worker's full tag map.
public IReadOnlyDictionary<string, string> GetTags(Guid id)
Parameters
idGuid
Returns
GetWorkerHistory(Guid)
Gets the code change history for a worker.
public IReadOnlyList<HistoryEntry> GetWorkerHistory(Guid id)
Parameters
idGuidThe worker ID.
Returns
- IReadOnlyList<HistoryEntry>
A read-only list of history entries.
Exceptions
- WorkerNotFoundException
Thrown if the worker is not found.
ListWorkers()
Lists all workers with their current status.
public IReadOnlyList<WorkerInfo> ListWorkers()
Returns
- IReadOnlyList<WorkerInfo>
A read-only list of worker information.
LoadCode(Guid, CodeSource, SourceCredential?, CancellationToken)
Loads new code into an existing worker. The codeSource
carries either an in-memory byte payload or a URL to fetch — callers
construct the appropriate subtype via CodeSourceContent(byte[])
or CodeSourceUrl(Uri, string?) on the wire.
public Task LoadCode(Guid id, CodeSource codeSource, SourceCredential? credential = null, CancellationToken cancellationToken = default)
Parameters
idGuidThe worker ID.
codeSourceCodeSourceEither content bytes or a URL.
credentialSourceCredentialOptional, transient per-request fetch credential -- see CreateWorkerAsync(CreateWorkerRequest, SourceCredential?, CancellationToken).
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- WorkerNotFoundException
Thrown if the worker is not found.
- ArgumentException
Thrown on validation failure (empty content, non-HTTP URL).
RecoverWorkersAsync(CancellationToken)
Recovers all workers from persistent state and resumes processing.
public Task RecoverWorkersAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- EngineNotFoundException
Thrown if an engine is missing for a recovered worker.
RegisterEngine(ContentType, string, Func<IEngine>)
Registers an engine to handle code execution for a specific content type.
public void RegisterEngine(ContentType contentType, string languageName, Func<IEngine> engineFactory)
Parameters
contentTypeContentTypeThe content type handled by the engine (e.g., "text/x-python").
languageNamestringThe programming language name (e.g., "Python").
engineFactoryFunc<IEngine>Factory function that creates new engine instances.
Exceptions
- InvalidOperationException
Thrown if an engine is already registered for the content type.
SetTag(Guid, string, string)
Sets a single tag on the worker. Tags are resource metadata and are NOT injected into the engine process.
public void SetTag(Guid id, string key, string value)
Parameters
SetTags(Guid, IDictionary<string, string>)
Replaces the worker's entire tag map with the supplied dictionary.
public void SetTags(Guid id, IDictionary<string, string> tags)
Parameters
idGuidtagsIDictionary<string, string>
StartWorkerAsync(Guid, CancellationToken)
Starts a stopped worker, resuming topic subscriptions and processing.
public Task StartWorkerAsync(Guid id, CancellationToken cancellationToken = default)
Parameters
idGuidThe worker ID.
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- WorkerNotFoundException
Thrown if the worker is not found.
StopWorkerAsync(Guid, CancellationToken)
Stops a running worker, pausing topic subscriptions and processing.
public Task StopWorkerAsync(Guid id, CancellationToken cancellationToken = default)
Parameters
idGuidThe worker ID.
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- WorkerNotFoundException
Thrown if the worker is not found.