public PositionDTO NetPosition(NetPositionFormDTO form) { var cache = WebCache.Instance; var prodDef = cache.ProdDefs.FirstOrDefault(o => o.Id == form.securityId); if (prodDef.QuoteType == enmQuoteType.Closed || prodDef.QuoteType == enmQuoteType.Inactive) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Resources.Resource.ProductClosed)); } var quote = cache.Quotes.FirstOrDefault(o => o.Id == form.securityId); var positionService = new PositionService(); var closedPosition = positionService.DoClosePosition(UserId, form.posId, form.securityId, Quotes.GetLastPrice(quote)); var posIdToFollowClose = closedPosition.Id; Task.Run(() => { YJYGlobal.LogLine("NEW THREAD: check and CLOSE follow positions for pos " + posIdToFollowClose); var checkAndCloseFollowPositions = PositionService.CheckAndCloseFollowPositions(posIdToFollowClose); MessageService.AddAutoCloseMessages(checkAndCloseFollowPositions); }); var result = Mapper.Map <PositionDTO>(closedPosition); return(result); }
static async Task ProcessMessagesAsync(Message message, CancellationToken token) { // Process the message //YJYGlobal.LogLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber}"); try { var posToClose = Serialization.ByteArrayToObject(message.Body) as PosToClose; YJYGlobal.LogLine( $"Received: {posToClose.Id} {posToClose.closeType} {posToClose.closePx} {posToClose.closePxTime}"); var autoClosePosition = PositionService.AutoClosePosition(posToClose); var checkAndCloseFollowPositions = PositionService.CheckAndCloseFollowPositions(autoClosePosition.Id); MessageService.AddAutoCloseMessage(autoClosePosition); MessageService.AddAutoCloseMessages(checkAndCloseFollowPositions); YJYGlobal.LogLine( $"pos closed: {autoClosePosition.Id} type:{autoClosePosition.CloseType} pl:{autoClosePosition.PL}"); // Complete the message so that it is not received again. // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default). await queueClient.CompleteAsync(message.SystemProperties.LockToken); // Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed. // If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls // to avoid unnecessary exceptions. } catch (Exception e) { YJYGlobal.LogException(e); await queueClient.AbandonAsync(message.SystemProperties.LockToken); } }