Example #1
0
        /// <inheritdoc />
        public IOperation <TOperationState> CreateOperation(OperationId operationId, [DisallowNull] TOperationState state, IPropertyContainer?metadata = null)
        {
            var operation = Operation.CreateNotStarted(id: operationId, state: state, metadata: metadata);

            _operations[operationId] = operation;
            return(operation);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OperationManager{TSessionState, TOperationState}"/> class.
        /// </summary>
        /// <param name="sessionId">Session id.</param>
        /// <param name="sessionState">Initial session state.</param>
        /// <param name="sessionManager">Owner session manager.</param>
        /// <param name="executionOptions">Optional execution options. Also can be provided in Start method.</param>
        /// <param name="logger">Logger.</param>
        /// <param name="metadata">Optional metadata.</param>
        public OperationManager(
            OperationId sessionId,
            [DisallowNull] TSessionState sessionState,
            ISessionManager <TSessionState, TOperationState> sessionManager,
            IExecutionOptions <TSessionState, TOperationState>?executionOptions = null,
            ILogger?logger = null,
            IPropertyContainer?metadata = null)
        {
            sessionState.AssertArgumentNotNull(nameof(sessionState));
            sessionManager.AssertArgumentNotNull(nameof(sessionManager));

            _sessionManager = sessionManager;
            _logger         = logger ?? GetLoggerFactory().CreateLogger(sessionId.Value);
            _operations     = new ConcurrentDictionary <OperationId, IOperation <TOperationState> >();

            _session = Operation
                       .CreateNotStarted(sessionId, sessionState, metadata)
                       .ToSession(getOperations: GetOperations);

            if (executionOptions != null)
            {
                _session = _session.With(executionOptions: executionOptions);
            }

            ILoggerFactory GetLoggerFactory() =>
            (ILoggerFactory?)_sessionManager.Services.GetService(typeof(ILoggerFactory)) ?? NullLoggerFactory.Instance;
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Operation{TOperationState}"/> class.
        /// </summary>
        /// <param name="id">Operation id.</param>
        /// <param name="state">Operation state.</param>
        /// <param name="status">Operation status.</param>
        /// <param name="startedAt">Date and time of start.</param>
        /// <param name="finishedAt">Date and time of finish.</param>
        /// <param name="exception">Exception occured on task execution.</param>
        /// <param name="metadata">Optional metadata.</param>
        public Operation(
            OperationId id,
            [DisallowNull] TOperationState state,
            OperationStatus status,
            LocalDateTime?startedAt,
            LocalDateTime?finishedAt,
            Exception?exception,
            IPropertyContainer?metadata)
        {
            state.AssertArgumentNotNull(nameof(state));

            Id     = id;
            State  = state;
            Status = status;

            StartedAt  = startedAt;
            FinishedAt = finishedAt;
            Exception  = exception;

            if (metadata == null)
            {
                // If state is metadata provider itself then use it as operation metadata.
                if (state is IMetadataProvider stateMetadataProvider &&
                    stateMetadataProvider.GetMetadataContainer(autoCreate: false) is { } stateMetadata)
                {
                    Metadata = stateMetadata.ToReadOnly();
                }
                else
                {
                    Metadata = PropertyContainer.Empty;
                }
            }
Example #4
0
        /// <inheritdoc />
        public IOperation <TOperationState> UpdateOperation(OperationId operationId, [DisallowNull] Action <OperationUpdateContext <TOperationState> > updateAction)
        {
            updateAction.AssertArgumentNotNull(nameof(updateAction));

            IOperation <TOperationState> operation        = GetOperationOrThrow(operationId);
            IOperation <TOperationState> updatedOperation = operation;

            var updateContext = new OperationUpdateContext <TOperationState>(operation);

            updateAction(updateContext);
            if (updateContext.NewState.IsNotNull() && !ReferenceEquals(updateContext.Operation.State, updateContext.NewState))
            {
                updatedOperation = operation.WithState(state: updateContext.NewState);
                _operations.TryUpdate(operationId, updatedOperation, operation);
            }

            return(updatedOperation);
        }
Example #5
0
        /// <inheritdoc />
        public IOperation <TOperationState> UpdateOperation(OperationId operationId, [DisallowNull] IOperation <TOperationState> updatedOperation)
        {
            updatedOperation.AssertArgumentNotNull(nameof(updatedOperation));
            IOperation <TOperationState> operation = GetOperationOrThrow(operationId);

            if (!updatedOperation.Id.Equals(operationId))
            {
                throw new OperationManagerException(Errors.OperationIdDoesNotMatch(
                                                        providedOperationId: updatedOperation.Id,
                                                        existingOperationId: operationId));
            }

            if (!ReferenceEquals(updatedOperation, operation))
            {
                _operations.TryUpdate(operationId, updatedOperation, operation);
            }

            return(updatedOperation);
        }
Example #6
0
        public IOperationManager <TSessionState, TOperationState> CreateOperationManager(
            OperationId sessionId,
            TSessionState sessionState,
            IPropertyContainer?operationManagerMetadata = null)
        {
            ISessionManager <TSessionState, TOperationState> sessionManager = BuildSessionManager();

            IOperationManager <TSessionState, TOperationState> operationManager = new OperationManager <TSessionState, TOperationState>(
                sessionId: sessionId,
                sessionState: sessionState,
                sessionManager: sessionManager,
                executionOptions: _executionOptions,
                logger: null,
                metadata: operationManagerMetadata);

            sessionManager.AddOperationManager(operationManager);

            return(operationManager);
        }
Example #7
0
 /// <summary>
 /// Error: Provided OperationId does not match existing OperationId.
 /// </summary>
 /// <param name="providedOperationId">Provided operation identifier.</param>
 /// <param name="existingOperationId">Existing operation identifier.</param>
 /// <returns>Error instance.</returns>
 public static Error <ErrorCode> OperationIdDoesNotMatch(OperationId providedOperationId, OperationId existingOperationId) => Error.CreateError(
     errorCode: ErrorCode.OperationIdDoesNotMatch,
     messageTemplate: "Provided OperationId does not match existing OperationId. ProvidedOperationId: {providedOperationId}, ExistingOperationId: {existingOperationId}.",
     args: new object[] { providedOperationId, existingOperationId });
Example #8
0
 /// <summary>
 /// Error: Operation does not exists. OperationId: {operationId}.
 /// </summary>
 /// <param name="operationId">Operation identifier.</param>
 /// <returns>Error instance.</returns>
 public static Error <ErrorCode> OperationDoesNotExists(OperationId operationId) => Error.CreateError(
     errorCode: ErrorCode.OperationDoesNotExists,
     messageTemplate: "Operation does not exists. OperationId: {operationId}.",
     args: operationId);
Example #9
0
 /// <summary>
 /// Error: Session update is prohibited.
 /// </summary>
 /// <param name="sessionId">Session identifier.</param>
 /// <param name="sessionStatus">Current session status.</param>
 /// <returns>Error instance.</returns>
 public static Error <ErrorCode> SessionUpdateIsProhibited(OperationId sessionId, string sessionStatus) => Error.CreateError(
     errorCode: ErrorCode.SessionUpdateIsProhibited,
     messageTemplate: "Session update is prohibited. You can change session only in NotStarted status. SessionId: {sessionId}, SessionStatus: {sessionStatus}.",
     args: new object[] { sessionId, sessionStatus });
Example #10
0
 /// <summary>
 /// Error: Session is already started. SessionId: {sessionId}.
 /// </summary>
 /// <param name="sessionId">Session identifier.</param>
 /// <returns>Error instance.</returns>
 public static Error <ErrorCode> SessionIsAlreadyStarted(OperationId sessionId) => Error.CreateError(
     errorCode: ErrorCode.SessionIsAlreadyStarted,
     messageTemplate: "Session is already started. SessionId: {sessionId}.",
     args: sessionId);
Example #11
0
 /// <summary>
 /// Error: Session does not exists. SessionId: {sessionId}.
 /// </summary>
 /// <param name="sessionId">Session identifier.</param>
 /// <returns>Error instance.</returns>
 public static Error <ErrorCode> SessionDoesNotExists(OperationId sessionId) => Error.CreateError(
     errorCode: ErrorCode.SessionDoesNotExists,
     messageTemplate: "Session does not exists. SessionId: {sessionId}.",
     args: sessionId);
Example #12
0
 /// <inheritdoc />
 public IOperation <TOperationState>?DeleteOperation(OperationId operationId)
 {
     _operations.TryRemove(operationId, out var deleted);
     return(deleted);
 }
Example #13
0
 /// <inheritdoc />
 public IOperation <TOperationState>?GetOperation(OperationId operationId)
 {
     _operations.TryGetValue(operationId, out IOperation <TOperationState> operation);
     return(operation);
 }