public async Task Save <TData>(IOperationExecutionInfo <TData> executionInfo) where TData : class
        {
            var entity = Convert(executionInfo);

            entity.LastModified = _systemClock.UtcNow.UtcDateTime;
            await _tableStorage.ReplaceAsync(entity);
        }
Пример #2
0
        public async Task Save <TData>(IOperationExecutionInfo <TData> executionInfo) where TData : class
        {
            var entity = Convert(executionInfo);

            entity.LastModified = _dateService.Now();
            await _tableStorage.ReplaceAsync(entity);
        }
Пример #3
0
        private void RequestPrice(ICommandSender sender, IOperationExecutionInfo <SpecialLiquidationOperationData>
                                  executionInfo)
        {
            //hack, requested by the bank
            var positionsVolume = executionInfo.Data.Volume != 0 ? executionInfo.Data.Volume : 1;

            var command = new GetPriceForSpecialLiquidationCommand
            {
                OperationId   = executionInfo.Id,
                CreationTime  = _dateService.Now(),
                Instrument    = executionInfo.Data.Instrument,
                Volume        = positionsVolume,
                RequestNumber = executionInfo.Data.RequestNumber,
                RequestedFromCorporateActions = executionInfo.Data.RequestedFromCorporateActions
            };

            if (_marginTradingSettings.ExchangeConnector == ExchangeConnectorType.RealExchangeConnector)
            {
                //send it to the Gavel
                sender.SendCommand(command, _cqrsContextNamesSettings.Gavel);
            }
            else
            {
                _specialLiquidationService.SavePriceRequestForSpecialLiquidation(command);
            }

            //special command is sent instantly for timeout control.. it is retried until timeout occurs
            sender.SendCommand(new GetPriceForSpecialLiquidationTimeoutInternalCommand
            {
                OperationId    = executionInfo.Id,
                CreationTime   = _dateService.Now(),
                TimeoutSeconds = _marginTradingSettings.SpecialLiquidation.PriceRequestTimeoutSec,
                RequestNumber  = executionInfo.Data.RequestNumber
            }, _cqrsContextNamesSettings.TradingEngine);
        }
Пример #4
0
 public async Task DeleteAsync <TData>(IOperationExecutionInfo <TData> executionInfo) where TData : class
 {
     var entity = Convert(executionInfo);
     await _tableStorage.DeleteIfExistAsync(
         OperationExecutionInfoEntity.GeneratePartitionKey(entity.OperationName),
         OperationExecutionInfoEntity.GeneratePartitionKey(executionInfo.Id));
 }
 private static OperationExecutionInfoEntity Convert <TData>(IOperationExecutionInfo <TData> model)
     where TData : class
 {
     return(new OperationExecutionInfoEntity
     {
         Id = model.Id,
         OperationName = model.OperationName,
         Data = model.Data.ToJson(),
     });
 }
Пример #6
0
 private static OperationExecutionInfoEntity Convert <TData>(IOperationExecutionInfo <TData> model, DateTime now)
     where TData : class
 {
     return(new OperationExecutionInfoEntity
     {
         Id = model.Id,
         OperationName = model.OperationName,
         Data = model.Data.ToJson(),
         PrevLastModified = model.LastModified,
         LastModified = now
     });
 }
Пример #7
0
        private async Task InternalRetryPriceRequest(DateTime eventCreationTime,
                                                     ICommandSender sender,
                                                     IOperationExecutionInfo <SpecialLiquidationOperationData> executionInfo,
                                                     TimeSpan retryTimeout)
        {
            var shouldRetryAfter = eventCreationTime.Add(retryTimeout);

            var timeLeftBeforeRetry = shouldRetryAfter - _dateService.Now();

            if (timeLeftBeforeRetry > TimeSpan.Zero)
            {
                await Task.Delay(timeLeftBeforeRetry);
            }

            executionInfo.Data.RequestNumber++;

            RequestPrice(sender, executionInfo);

            await _operationExecutionInfoRepository.Save(executionInfo);
        }
        public async Task Save <TData>(IOperationExecutionInfo <TData> executionInfo) where TData : class
        {
            var entity = Convert(executionInfo);

            entity.LastModified = _systemClock.UtcNow.UtcDateTime;

            using (var conn = new SqlConnection(_connectionString))
            {
                try
                {
                    await conn.ExecuteAsync(
                        $"insert into {TableName} ({GetColumns}) values ({GetFields})", entity);
                }
                catch (SqlException)
                {
                    await conn.ExecuteAsync(
                        $"update {TableName} set {GetUpdateClause} where Id=@Id and OperationName=@OperationName", entity);
                }
            }
        }
Пример #9
0
        public async Task DeleteAsync <TData>(IOperationExecutionInfo <TData> executionInfo) where TData : class
        {
            var entity = Convert(executionInfo, _systemClock.UtcNow.UtcDateTime);

            using (var conn = new SqlConnection(_connectionString))
            {
                try
                {
                    await conn.ExecuteAsync(
                        $"DELETE {TableName} where Id=@Id " +
                        "and OperationName=@OperationName ",
                        entity);
                }
                catch (Exception ex)
                {
                    await _log.WriteErrorAsync(nameof(OperationExecutionInfoRepository), nameof(GetOrAddAsync), ex);

                    throw;
                }
            }
        }
Пример #10
0
        public async Task SaveAsync <TData>(IOperationExecutionInfo <TData> executionInfo) where TData : class
        {
            var entity = Convert(executionInfo, _systemClock.UtcNow.UtcDateTime);
            int affectedRows;

            using (var conn = new SqlConnection(_connectionString))
            {
                try
                {
                    affectedRows = await conn.ExecuteAsync(
                        $"update {TableName} set {GetUpdateClause} where Id=@Id " +
                        "and OperationName=@OperationName " +
                        "and LastModified=@PrevLastModified",
                        entity);
                }
                catch (Exception ex)
                {
                    await _log.WriteErrorAsync(nameof(OperationExecutionInfoRepository), nameof(GetOrAddAsync), ex);

                    throw;
                }
            }

            if (affectedRows == 0)
            {
                var existingExecutionInfo = await GetAsync <TData>(executionInfo.OperationName, executionInfo.Id);

                if (existingExecutionInfo == null)
                {
                    throw new InvalidOperationException(
                              $"Execution info {executionInfo.OperationName}:{executionInfo.Id} does not exist");
                }

                throw new InvalidOperationException(
                          $"Optimistic Concurrency Violation Encountered. " +
                          $"Existing info: [{existingExecutionInfo.ToJson()}] " +
                          $"New info: [{executionInfo.ToJson()}]");
            }
        }
Пример #11
0
        private async Task <bool> FailIfInstrumentDiscontinued(IOperationExecutionInfo <SpecialLiquidationOperationData> executionInfo, ICommandSender sender)
        {
            var isDiscontinued = _assetPairsCache.GetAssetPairById(executionInfo.Data.Instrument).IsDiscontinued;

            if (isDiscontinued)
            {
                if (executionInfo.Data.SwitchState(SpecialLiquidationOperationState.PriceRequested,
                                                   SpecialLiquidationOperationState.OnTheWayToFail))
                {
                    sender.SendCommand(new FailSpecialLiquidationInternalCommand
                    {
                        OperationId  = executionInfo.Id,
                        CreationTime = _dateService.Now(),
                        Reason       = "Instrument discontinuation",
                    }, _cqrsContextNamesSettings.TradingEngine);

                    await _operationExecutionInfoRepository.Save(executionInfo);
                }

                return(true);
            }

            return(false);
        }