Table of Contents

Interface IFlowRunControlStore

Namespace
FlowOrchestrator.Core.Storage
Assembly
FlowOrchestrator.Core.dll

Persistence contract for run-level control signals: cancellation, timeout, and idempotency. Decoupled from IFlowRunStore so control records can be written before the run is committed.

public interface IFlowRunControlStore

Methods

ConfigureRunAsync(Guid, Guid, string, string?, DateTimeOffset?)

Persists the control record for a new run, including an optional idempotency key and an absolute timeout deadline.

Task ConfigureRunAsync(Guid runId, Guid flowId, string triggerKey, string? idempotencyKey, DateTimeOffset? timeoutAtUtc)

Parameters

runId Guid
flowId Guid
triggerKey string
idempotencyKey string
timeoutAtUtc DateTimeOffset?

Returns

Task

ExtendDeadlineAsync(Guid, DateTimeOffset?)

Grants a run a fresh execution window: sets TimeoutAtUtc to newTimeoutAtUtc (or clears the deadline when null) and un-latches any timeout-induced termination — clears TimedOutAtUtc and the cancellation fields that MarkTimedOutAsync(Guid, string?) set. A genuine user cancellation (recorded via RequestCancelAsync(Guid, string?) while the run had not timed out) is preserved.

Task<bool> ExtendDeadlineAsync(Guid runId, DateTimeOffset? newTimeoutAtUtc)

Parameters

runId Guid

The run whose deadline is being refreshed.

newTimeoutAtUtc DateTimeOffset?

The new absolute deadline, or null to leave the run without a timeout bound.

Returns

Task<bool>

true if the record was found and updated; false otherwise.

Remarks

Called by FlowOrchestratorEngine.RetryStepAsync before re-dispatch so a step retried after the run's deadline lapsed can actually re-execute instead of being skipped by the termination gate. Default implementation is a no-op returning false so existing custom IFlowRunControlStore implementations continue to compile.

FindRunIdByIdempotencyKeyAsync(Guid, string, string)

Looks up an existing run that was started with the given idempotency key. Returns the RunId of the existing run, or null if none exists.

Task<Guid?> FindRunIdByIdempotencyKeyAsync(Guid flowId, string triggerKey, string idempotencyKey)

Parameters

flowId Guid
triggerKey string
idempotencyKey string

Returns

Task<Guid?>

GetRunControlAsync(Guid)

Returns the control record for the given run, or null if not found.

Task<FlowRunControlRecord?> GetRunControlAsync(Guid runId)

Parameters

runId Guid

Returns

Task<FlowRunControlRecord>

MarkTimedOutAsync(Guid, string?)

Marks the run as timed out.

Task<bool> MarkTimedOutAsync(Guid runId, string? reason)

Parameters

runId Guid
reason string

Returns

Task<bool>

true if the record was found and updated; false otherwise.

Remarks

Applies only while the timeout is not yet latched and no cancel has been requested: a user cancel recorded first wins, so the run stays Cancelled and keeps its reason. Without that guard a cancel landing between the caller's deadline check and this write was relabelled a timeout, and a later retry's ExtendDeadlineAsync(Guid, DateTimeOffset?) then cleared the user's cancel. When reason is null, an existing cancel reason is kept, falling back to "Run timed out.".

Invoked from two places: lazily by the engine when a step is dispatched after the deadline has passed, and proactively by the periodic timeout-enforcement hosted service (FlowTimeoutEnforcementHostedService). Also sets the cancellation latch so in-flight steps short-circuit on their next dispatch — a fresh execution window can be granted afterwards via ExtendDeadlineAsync(Guid, DateTimeOffset?).

ReleaseIdempotencyKeyAsync(Guid, string, string, Guid)

Removes the idempotency-key registration for runId, so a later trigger with the same key starts a new run. A registration that maps the key to a different run is left alone.

Task<bool> ReleaseIdempotencyKeyAsync(Guid flowId, string triggerKey, string idempotencyKey, Guid runId)

Parameters

flowId Guid

The flow the key was registered for.

triggerKey string

The trigger the key was registered for.

idempotencyKey string

The caller-supplied idempotency key.

runId Guid

The run the registration must still point at for it to be removed.

Returns

Task<bool>

true when a registration was removed.

Remarks

Called by the engine only when a trigger registered the key and then failed before dispatching any entry step: without it a caller retrying the failed delivery with the same key was answered "duplicate" of a run that never started work. Default implementation is a no-op returning false so existing custom stores keep compiling; such a store keeps the 1.x behaviour (the retry is absorbed as a duplicate of the dead run).

RequestCancelAsync(Guid, string?)

Marks a cancellation request for the run. Steps check this flag before executing.

Task<bool> RequestCancelAsync(Guid runId, string? reason)

Parameters

runId Guid
reason string

Returns

Task<bool>

true if the record was found and updated; false otherwise.

TryRegisterIdempotencyKeyAsync(Guid, string, string, Guid)

Atomically registers an idempotency key for the given run.

Task<bool> TryRegisterIdempotencyKeyAsync(Guid flowId, string triggerKey, string idempotencyKey, Guid runId)

Parameters

flowId Guid
triggerKey string
idempotencyKey string
runId Guid

Returns

Task<bool>

true if the key was registered by this call; false if a record with this key already existed (duplicate trigger).

TryTakeOverStaleIdempotencyKeyAsync(Guid, string, string, Guid, Guid, TimeSpan)

Re-points an idempotency reservation from staleRunId to newRunId when the reservation is stale and its run never started work, and returns the run the key maps to once the call is over.

Task<Guid?> TryTakeOverStaleIdempotencyKeyAsync(Guid flowId, string triggerKey, string idempotencyKey, Guid staleRunId, Guid newRunId, TimeSpan reservationTimeout)

Parameters

flowId Guid

The flow the key was registered for.

triggerKey string

The trigger the key was registered for.

idempotencyKey string

The caller-supplied idempotency key, normalised like every other key member.

staleRunId Guid

The run the registration must still point at for the takeover to apply.

newRunId Guid

The run of the trigger asking to take the key over.

reservationTimeout TimeSpan

Minimum age of the registration, measured on the clock that stamped it (the database clock on SQL Server / PostgreSQL), for it to count as stale.

Returns

Task<Guid?>

newRunId when this call took the key over; otherwise the run the key maps to now (staleRunId, or the run of a concurrent caller that won the takeover), or null when no registration exists for the key any more.

Remarks

TriggerAsync registers the key before it creates the run. A process killed between the two left the key pointing at a run that never started, so every retry of that delivery was answered "duplicate" of a dead run and the delivery was lost. A duplicate trigger calls this to take the key over in that case only.

The takeover applies only when, all decided in one atomic operation: the key still maps to staleRunId; the registration is at least reservationTimeout old; and the stale run has no dispatch-ledger row and no step status row (it never started work). It re-stamps the registration time, so the new reservation gets its own window. A check made separately from the write lets two callers both take the key over, or takes it from a run that dispatched its first step in between; exactly one of any number of concurrent callers may win.

In the same atomic operation the takeover sets the stale run's cancel latch, creating its control record when it has none, so a trigger that was only slow (not dead) and dispatches afterwards has its steps skipped by the termination gate instead of running beside the new run. The record may outlive the run row it belongs to when that run row was never written; it holds no deadline and is never swept. Completing the stale run is left to the engine.

No default implementation: a store that silently never took over would hide the lost delivery.