Example #1
0
        public async Task SendWebhook(IntegrationEventSubscription eventSubscription, WebhookPayload payload,
                                      IntegrationEventSubscriptionAttempt subscriptionAttempt, bool isSystemEvent, string payloadJSON)
        {
            var attemptCount = _attemptManager.SaveAndGetAttemptCount(subscriptionAttempt, eventSubscription.Max_RetryCount);

            payload.AttemptCount = attemptCount;

            if (isSystemEvent == true)//event is system event use system generated payload
            {
                payloadJSON = JsonConvert.SerializeObject(payload);
            }

            bool isSuccessful;

            try
            {
                isSuccessful = await SendWebhookAsync(payloadJSON, eventSubscription.HTTP_URL, eventSubscription);
            }
            catch (Exception exception) //an internal error occurred
            {
                throw exception;
            }

            if (!isSuccessful)
            {
                if (attemptCount > (eventSubscription.Max_RetryCount ?? 1))
                {
                    var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                    previousAttempt.Status = "FailedFatally";
                    _attemptRepository.Update(previousAttempt);
                }
                else
                {
                    throw new Exception($"Webhook sending attempt failed.");
                }
            }
            else
            {
                var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                previousAttempt.Status = "Completed";
                _attemptRepository.Update(previousAttempt);
            }

            return;
        }
        public async Task SendWebhook(IntegrationEventSubscription eventSubscription, WebhookPayload payload,
                                      IntegrationEventSubscriptionAttempt subscriptionAttempt)
        {
            var attemptCount = _attemptManager.SaveAndGetAttemptCount(subscriptionAttempt, eventSubscription.Max_RetryCount);

            payload.AttemptCount = attemptCount;

            bool isSuccessful;

            try
            {
                isSuccessful = await SendWebhookAsync(payload, eventSubscription.HTTP_URL, eventSubscription);
            }
            catch (Exception exception) //an internal error occurred
            {
                throw exception;
            }

            if (!isSuccessful)
            {
                if (attemptCount > eventSubscription.Max_RetryCount)
                {
                    var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                    previousAttempt.Status = "FailedFatally";
                    _attemptRepository.Update(previousAttempt);
                }
                else
                {
                    throw new Exception($"Webhook sending attempt failed.");
                }
            }
            else
            {
                var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                previousAttempt.Status = "Completed";
                _attemptRepository.Update(previousAttempt);
            }

            return;
        }
        public int?SaveAndGetAttemptCount(IntegrationEventSubscriptionAttempt currentAttempt, int?maxRetryCount)
        {
            int?attemptCount    = 0;
            var previousAttempt = GetLastAttempt(currentAttempt);

            //if no attempt exists, then this is the first attempt
            if (previousAttempt == null)
            {
                attemptCount = 1;
            }
            else
            {
                previousAttempt.Status = "Failed";
                attemptCount           = previousAttempt.AttemptCounter;
                attemptCount++;
                repo.Update(previousAttempt);
            }
            currentAttempt.AttemptCounter = attemptCount;
            currentAttempt.CreatedOn      = DateTime.UtcNow;
            currentAttempt.Id             = Guid.NewGuid();
            repo.Add(currentAttempt);
            return(attemptCount);
        }