Example #1
0
        void IEventConsumer <MarginCallEventArgs> .ConsumeEvent(object sender, MarginCallEventArgs ea)
        {
            var account   = ea.Account;
            var eventTime = _dateService.Now();
            var accountMarginEventMessage = AccountMarginEventMessageConverter.Create(account, false, eventTime);

            _threadSwitcher.SwitchThread(async() =>
            {
                if (LastNotifications.TryGetValue(account.Id, out var lastNotification) &&
                    lastNotification.AddMinutes(NotificationsTimeout) > eventTime)
                {
                    return;
                }

                var marginEventTask = _rabbitMqNotifyService.AccountMarginEvent(accountMarginEventMessage);

                _operationsLogService.AddLog("margin call", account.ClientId, account.Id, "", ea.ToJson());

                var marginUsageLevel = account.GetMarginUsageLevel();
                var marginUsedPerc   = marginUsageLevel == 0 ? 0 : 1 / marginUsageLevel;

                var notificationTask = SendMarginEventNotification(account.ClientId, string.Format(
                                                                       MtMessages.Notifications_MarginCall, marginUsedPerc,
                                                                       account.BaseAssetId));

                var emailTask = _emailService.SendMarginCallEmailAsync(account);

                await Task.WhenAll(marginEventTask, notificationTask, emailTask);

                LastNotifications.AddOrUpdate(account.Id, eventTime, (s, time) => eventTime);
            });
        }
Example #2
0
        void IEventConsumer <MarginCallEventArgs> .ConsumeEvent(object sender, MarginCallEventArgs ea)
        {
            var account   = ea.Account;
            var eventTime = _dateService.Now();

            var(level, lastNotifications) = LevelAndNotificationsCache(ea.MarginCallLevel);

            if (lastNotifications == null)
            {
                return;
            }

            var accountMarginEventMessage = AccountMarginEventMessageConverter.Create(account, level, eventTime);

            _threadSwitcher.SwitchThread(async() =>
            {
                if (lastNotifications.TryGetValue(account.Id, out var lastNotification) &&
                    lastNotification.AddMinutes(_settings.Throttling.MarginCallThrottlingPeriodMin) > eventTime)
                {
                    _log.WriteInfo(nameof(MarginCallConsumer), nameof(IEventConsumer <MarginCallEventArgs> .ConsumeEvent),
                                   $"MarginCall event is ignored for accountId {account.Id} because of throttling: event time {eventTime}, last notification was sent at {lastNotification}");
                    return;
                }

                var marginEventTask = _rabbitMqNotifyService.AccountMarginEvent(accountMarginEventMessage);

                _operationsLogService.AddLog($"margin call: {level.ToString()}", account.Id, "", ea.ToJson());

                var clientEmail = await _clientAccountService.GetEmail(account.ClientId);

                var emailTask = !string.IsNullOrEmpty(clientEmail)
                    ? _emailService.SendMarginCallEmailAsync(clientEmail, account.BaseAssetId, account.Id)
                    : Task.CompletedTask;

                await Task.WhenAll(marginEventTask, emailTask);

                lastNotifications.AddOrUpdate(account.Id, eventTime, (s, times) => eventTime);
            });
        }