Table of Contents

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

workManager WorkManager
actorProxies IActorProxyFactory
lifecyclePublisher ApiLifecyclePublisher
lifetime IHostApplicationLifetime
logger ILogger<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

workerId Guid
actorType string

StartAsync(CancellationToken)

Triggered when the application host is ready to start the service.

public Task StartAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Indicates that the start process has been aborted.

Returns

Task

A Task that represents the asynchronous Start operation.

StopAsync(CancellationToken)

Triggered when the application host is performing a graceful shutdown.

public Task StopAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Indicates that the shutdown process should no longer be graceful.

Returns

Task

A Task that represents the asynchronous Stop operation.

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

workerId Guid
start bool
cancellationToken CancellationToken

Returns

Task<bool>