Class RuntimeWorkerLoadCoordinator
- Namespace
- Virtufin.WorkManager.Services
- Assembly
- Virtufin.WorkManager.dll
Asynchronous worker creation (worker-management spec, "Create returns before
the code is loaded"). CreateWorker registers a Runtime-backed worker as
Loading and returns; this coordinator then drives
the load in the background (LoadAsync(CancellationToken)),
commits the outcome to the worker's catalog record, starts it if a
StartWorker arrived meanwhile, and publishes the loaded /
load_failed lifecycle event.
Deferred start without a lost update. The outcome commit and
TryRequestStartAsync(Guid, bool, CancellationToken) both go through etag compare-and-swap on
the same catalog record, from any gateway replica. The commit reads
StartRequested under the etag it writes with,
and a start request only lands on a record still Loading. So
whichever writes second sees the other's write, and a start is never
dropped. Starting is idempotent, so a retried commit that starts twice is
harmless.
At startup it also re-drives any Runtime-backed worker left
Loading (a gateway replica that died mid-load). Every replica does
this; LoadAsync(CancellationToken) is idempotent and only one
commit can win the compare-and-swap, so the others just observe the outcome.
public sealed class RuntimeWorkerLoadCoordinator : IHostedService
- Inheritance
-
RuntimeWorkerLoadCoordinator
- Implements
- Inherited Members
Constructors
RuntimeWorkerLoadCoordinator(WorkManager, IActorProxyFactory, ApiLifecyclePublisher, IHostApplicationLifetime, ILogger<RuntimeWorkerLoadCoordinator>)
Asynchronous worker creation (worker-management spec, "Create returns before
the code is loaded"). CreateWorker registers a Runtime-backed worker as
Loading and returns; this coordinator then drives
the load in the background (LoadAsync(CancellationToken)),
commits the outcome to the worker's catalog record, starts it if a
StartWorker arrived meanwhile, and publishes the loaded /
load_failed lifecycle event.
Deferred start without a lost update. The outcome commit and
TryRequestStartAsync(Guid, bool, CancellationToken) both go through etag compare-and-swap on
the same catalog record, from any gateway replica. The commit reads
StartRequested under the etag it writes with,
and a start request only lands on a record still Loading. So
whichever writes second sees the other's write, and a start is never
dropped. Starting is idempotent, so a retried commit that starts twice is
harmless.
At startup it also re-drives any Runtime-backed worker left
Loading (a gateway replica that died mid-load). Every replica does
this; LoadAsync(CancellationToken) is idempotent and only one
commit can win the compare-and-swap, so the others just observe the outcome.
public RuntimeWorkerLoadCoordinator(WorkManager workManager, IActorProxyFactory actorProxies, ApiLifecyclePublisher lifecyclePublisher, IHostApplicationLifetime lifetime, ILogger<RuntimeWorkerLoadCoordinator> logger)
Parameters
workManagerWorkManageractorProxiesIActorProxyFactorylifecyclePublisherApiLifecyclePublisherlifetimeIHostApplicationLifetimeloggerILogger<RuntimeWorkerLoadCoordinator>
Methods
Drive(Guid, string)
Start driving a worker's load in the background (fire and forget).
public void Drive(Guid workerId, string actorType)
Parameters
StartAsync(CancellationToken)
Triggered when the application host is ready to start the service.
public Task StartAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenIndicates that the start process has been aborted.
Returns
StopAsync(CancellationToken)
Triggered when the application host is performing a graceful shutdown.
public Task StopAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenIndicates that the shutdown process should no longer be graceful.
Returns
TryRequestStartAsync(Guid, bool, CancellationToken)
StartWorker on a Loading worker: record
the request so the load commit starts it. Returns false if the worker is
no longer loading (the caller then starts it normally).
public Task<bool> TryRequestStartAsync(Guid workerId, bool start, CancellationToken cancellationToken)
Parameters
workerIdGuidstartboolcancellationTokenCancellationToken