Example #1
0
        internal async Task ProcessCustomer(QaConfigCustomer customer, bool isNotificationQueueEmpty)
        {
            var shouldSendHttpRequests = isNotificationQueueEmpty;
            var tableTypeModels        = GetCdcDataModels(customer, out var lastExecutedLsn);

            Ensure.Items(tableTypeModels, ttm => ttm.ToLsn == lastExecutedLsn, "All cdc tables should be proceeded at single transaction");

            string lastPushedLsn      = null;
            var    notSendedDtosQueue = new Queue <CdcDataTableDto>(GroupCdcTableTypeModelsByLsn(customer.CustomerName, tableTypeModels));

            while (shouldSendHttpRequests && notSendedDtosQueue.Any() && !_cts.Token.IsCancellationRequested)
            {
                shouldSendHttpRequests = false;
                var data = notSendedDtosQueue.Peek();
                Task <HttpResponseMessage> responseMessage = null;

                try
                {
                    responseMessage = PushDataToHttpChannel(data);
                    var response = await responseMessage.ReceiveJson <JSendResponse>();

                    if (response.Status != JSendStatus.Success || response.Code != 200)
                    {
                        Logger.Warn($"Http push notification response was failed for customer code: {customer.CustomerName} [{data.TransactionLsn}]: {response.ToJsonLog()}");
                        break;
                    }

                    shouldSendHttpRequests = true;
                    lastPushedLsn          = notSendedDtosQueue.Dequeue().TransactionLsn;
                    Logger.Trace($"Http push notification was pushed successfuly for customer code: {customer.CustomerName} [{data.TransactionLsn}]: {response.ToJsonLog()}");
                }
                catch (Exception ex) when(ex is JsonReaderException)
                {
                    var responseBodyMessage = $"Response body: {await responseMessage.ReceiveString()}.";

                    Logger.Warn(ex, $"Exception while parsing response for customer code: {customer.CustomerName}. {responseBodyMessage} Notification: {data.ToJsonLog()}");
                    break;
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, $"There was an http exception while sending push notification for customer code: {customer.CustomerName}. Notification: {data.ToJsonLog()}");
                    break;
                }
            }

            AddDataToNotificationQueue(customer, notSendedDtosQueue.ToList(), lastPushedLsn, lastExecutedLsn);
            if (!shouldSendHttpRequests)
            {
                Ensure.That(notSendedDtosQueue.Any() || !isNotificationQueueEmpty);
                if (isNotificationQueueEmpty)
                {
                    CdcSynchronizationContext.SetCustomerNotificationQueueNotEmpty(customer);
                }
            }
        }
Example #2
0
        public void Execute(IJobExecutionContext context)
        {
            var customers           = QPConfiguration.GetCustomers(AppName).Where(c => !(c.ExcludeFromSchedulers || c.ExcludeFromSchedulersCdcElastic)).ToList();
            var customersDictionary = new Dictionary <QaConfigCustomer, bool>();
            var prtgErrorsHandlerVm = new PrtgErrorsHandlerViewModel(customers);

            var customersWithEnabledCdc = customers.Where(customer =>
            {
                try
                {
                    return(ShouldUseCdcForCustomerCode(customer));
                }
                catch (Exception ex)
                {
                    ex.Data.Clear();
                    ex.Data.Add("CustomerCode", customer.CustomerName);
                    Logger.Log.Warn($"There was an error while reading customer code settings: {customer.CustomerName}", ex);
                    prtgErrorsHandlerVm.EnqueueNewException(ex);
                }

                return(false);
            }).ToList();

            foreach (var customer in customersWithEnabledCdc)
            {
                try
                {
                    customersDictionary.Add(customer, IsCustomerQueueEmpty(customer));
                }
                catch (Exception ex)
                {
                    ex.Data.Clear();
                    ex.Data.Add("CustomerCode", customer.CustomerName);
                    Logger.Log.Warn($"There was an error while reading customer code settings: {customer.CustomerName}", ex);
                    prtgErrorsHandlerVm.EnqueueNewException(ex);
                }
            }

            CdcSynchronizationContext.ReplaceData(customersDictionary);
            _prtgLogger.LogMessage(prtgErrorsHandlerVm);
        }
Example #3
0
        public Task Execute(IJobExecutionContext context)
        {
            var customers = QPConfiguration.GetCustomers(AppName)
                            .Where(c => c.DbType == DatabaseType.SqlServer)
                            .Where(c => !(c.ExcludeFromSchedulers || c.ExcludeFromSchedulersCdcElastic))
                            .ToList();
            var customersDictionary = new Dictionary <QaConfigCustomer, bool>();

            var customersWithEnabledCdc = customers.Where(customer =>
            {
                try
                {
                    return(ShouldUseCdcForCustomerCode(customer));
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "There was an error while reading customer code: {customerCode}", customer.CustomerName);
                }

                return(false);
            }).ToList();

            foreach (var customer in customersWithEnabledCdc)
            {
                if (context.CancellationToken.IsCancellationRequested)
                {
                    return(Task.FromCanceled(context.CancellationToken));
                }
                try
                {
                    customersDictionary.Add(customer, IsCustomerQueueEmpty(customer));
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "There was an error while processing customer code: {customerCode}", customer.CustomerName);
                }
            }

            CdcSynchronizationContext.ReplaceData(customersDictionary);
            return(Task.CompletedTask);
        }