Esempio n. 1
0
        public async Task <OperationStatusUpdateResultModel> FailAsync(string hash)
        {
            if (string.IsNullOrEmpty(hash))
            {
                _log.Warning("Operation hash is empty");
                return(OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.OperationNotFound));
            }

            return(await _transactionScopeHandler.WithTransactionAsync(async() =>
            {
                var operation = await _operationsRepository.GetByHashAsync(hash);
                if (operation == null)
                {
                    _log.Warning("Operation not found by hash", context: hash);
                    return OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.OperationNotFound);
                }

                if (operation.Status == OperationStatus.Failed)
                {
                    _log.Warning("Operation is already failed", context: hash);
                    return OperationStatusUpdateResultModel.Succeeded();
                }

                switch (operation.Status)
                {
                case OperationStatus.Accepted:
                    await _operationsRepository.SetStatusAsync(operation.Id, OperationStatus.Failed);
                    _log.Info("Operation has been failed", new { operationId = operation.Id });
                    break;

                case OperationStatus.Created:
                case OperationStatus.Failed:
                case OperationStatus.Succeeded:
                    _log.Warning("Operation status can't be updated", context:
                                 new
                    {
                        hash,
                        currentStatus = operation.Status.ToString(),
                        desiredStatus = OperationStatus.Failed.ToString()
                    });
                    return OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.InvalidStatus);

                default:
                    _log.Error(message: "Current operation status is not expected",
                               context: new { hash, currentStatus = operation.Status.ToString() });
                    return OperationStatusUpdateResultModel.Failed(OperationStatusUpdateError.InvalidStatus);
                }

                return OperationStatusUpdateResultModel.Succeeded();
            }));
        }
        public Task <IOperation> GetByHashAsync(string hash)
        {
            if (string.IsNullOrEmpty(hash))
            {
                throw new ArgumentNullException(nameof(hash));
            }

            return(_operationsRepository.GetByHashAsync(hash));
        }