OperationScheduler

Namespace: SnowBank.Threading.Operations · class

Implements: IOperationScheduler, IOperationOverlord

Service that tracks and runs Operations

Constructors

OperationScheduler

OperationScheduler(IEventBus eventBus, ILogger<OperationScheduler> logger)

Methods

Create

IOperationContext<TResult> Create<TResult>(string type, string key, IOperationContext parent = null, CancellationToken ct = null)

Creates a new context for running an operation

  • type — Operation type (for diagnostic purpose)
  • key — If not null, used to distinguish multiple concurrent operations of the type type
  • parent — If not null, parent operation that started this operation
  • ct — Token used to abort the execution of the operation

Returns: Context initialized and ready to run

Dispatch

void Dispatch(IOperationContext context, IEvent evt)

Dispatches an event linked to the specified IOperationContext

  • context — Context of the operation that is triggering this event
  • evt — Event triggered by this operation

ExecuteOperation

Task<OperationResult<TResult>> ExecuteOperation<TResult>(IOperationContext<TResult> context, Func<IOperationContext<TResult>, Task<OperationResult<TResult>>> handler)

Executes an operation under the control of this scheduler

  • context — Context of the operation
  • handler — Handler that will run the operation

Returns: Result of the operation.

Any exception thrown by handler will be intercepted and wrapped into a failed OperationResult.

The end result of the operation can be checked using the Check method.

ListOperations

IOperationContext[] ListOperations()

Lists all currently running operations

Log

void Log(IOperationContext context, LogLevel level, Exception exception, string message, params object[] args)

Logs a message that will be visible to the observers of an operation

  • context — Context of the operation that is producing this log
  • level — Log level
  • exception — Optional exception that is linked to this log message
  • message — Log message
  • args — Optional arguments used to format the log message

NewId

string NewId()

Generates a new unique Operation identifier

TryGetOperationById

bool TryGetOperationById(string id, out IOperationContext context)

Returns the context for the operation with the given unique identifier, if it exists

  • id — Identifier of the operation
  • context — Receives the context of the corresponding context, if found.

Returns: true if there is an operation with the given id; otherwise, false