Ejemplo n.º 1
0
        public async Task Should_Throw_Exception_Async()
        {
            await _webhookSender
            .SendWebhookAsync(new WebhookSenderArgs())
            .ShouldThrowAsync <ArgumentNullException>();

            var exception = await _webhookSender
                            .SendWebhookAsync(new WebhookSenderArgs
            {
                WebhookEventId        = Guid.NewGuid(),
                WebhookSubscriptionId = Guid.Empty
            })
                            .ShouldThrowAsync <ArgumentNullException>();

            exception.Message.ShouldContain(nameof(WebhookSenderArgs.WebhookSubscriptionId));

            var exception2 = await _webhookSender
                             .SendWebhookAsync(new WebhookSenderArgs()
            {
                WebhookEventId        = Guid.Empty,
                WebhookSubscriptionId = Guid.NewGuid()
            })
                             .ShouldThrowAsync <ArgumentNullException>();

            exception2.Message.ShouldContain(nameof(WebhookSenderArgs.WebhookEventId));
        }
Ejemplo n.º 2
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                //Delay 1 ngày và gửi webhook sau mỗi một ngày
                await Task.Delay(TimeSpan.FromDays(1));

                await webhookSender.WebhookSendRetryAsync();

                await webhookSender.SendWebhookAsync();
            }
        }
Ejemplo n.º 3
0
        private async Task SendWebhook(WebhookSenderArgs args)
        {
            if (args.WebhookEventId == default)
            {
                return;
            }

            if (args.WebhookSubscriptionId == default)
            {
                return;
            }

            if (!args.TryOnce)
            {
                var sendAttemptCount = await _webhookSendAttemptStore.GetSendAttemptCountAsync(
                    args.TenantId,
                    args.WebhookEventId,
                    args.WebhookSubscriptionId
                    );

                if (sendAttemptCount > _webhooksConfiguration.MaxSendAttemptCount)
                {
                    return;
                }
            }

            try
            {
                await _webhookSender.SendWebhookAsync(args);
            }
            catch (Exception)
            {
                // no need to retry to send webhook since subscription disabled
                if (!await TryDeactivateSubscriptionIfReachedMaxConsecutiveFailCount(
                        args.TenantId,
                        args.WebhookSubscriptionId))
                {
                    throw; //Throw exception to re-try sending webhook
                }
            }
        }