From 8fa525360101ea0f2ff3ff8e0cd05fced9e9b56c Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sat, 26 Mar 2022 15:21:56 +0800 Subject: [PATCH] perf: users can customize the number of Webhook send count --- .../BackgroundWorker/WebhookSenderJob.cs | 18 ++++++++++++------ .../Abp/Webhooks/DefaultWebhookPublisher.cs | 5 +---- .../LINGYUN/Abp/Webhooks/WebhookDefinition.cs | 10 ++++++++++ .../LINGYUN/Abp/Webhooks/WebhookSenderArgs.cs | 5 ----- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/BackgroundWorker/WebhookSenderJob.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/BackgroundWorker/WebhookSenderJob.cs index 4d18cb209..b411e3a84 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/BackgroundWorker/WebhookSenderJob.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/BackgroundWorker/WebhookSenderJob.cs @@ -11,6 +11,7 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker public class WebhookSenderJob : AsyncBackgroundJob, ITransientDependency { private readonly IUnitOfWorkManager _unitOfWorkManager; + private readonly IWebhookDefinitionManager _webhookDefinitionManager; private readonly IWebhookSubscriptionManager _webhookSubscriptionManager; private readonly IWebhookSendAttemptStore _webhookSendAttemptStore; private readonly IWebhookSender _webhookSender; @@ -19,12 +20,14 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker public WebhookSenderJob( IUnitOfWorkManager unitOfWorkManager, + IWebhookDefinitionManager webhookDefinitionManager, IWebhookSubscriptionManager webhookSubscriptionManager, IWebhookSendAttemptStore webhookSendAttemptStore, IWebhookSender webhookSender, IOptions options) { _unitOfWorkManager = unitOfWorkManager; + _webhookDefinitionManager = webhookDefinitionManager; _webhookSubscriptionManager = webhookSubscriptionManager; _webhookSendAttemptStore = webhookSendAttemptStore; _webhookSender = webhookSender; @@ -33,11 +36,13 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker public override async Task ExecuteAsync(WebhookSenderArgs args) { - if (args.TryOnce) + var webhookDefinition = _webhookDefinitionManager.Get(args.WebhookName); + + if (webhookDefinition.TryOnce) { try { - await SendWebhook(args); + await SendWebhook(args, webhookDefinition); } catch (Exception e) { @@ -47,11 +52,11 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker } else { - await SendWebhook(args); + await SendWebhook(args, webhookDefinition); } } - private async Task SendWebhook(WebhookSenderArgs args) + private async Task SendWebhook(WebhookSenderArgs args, WebhookDefinition webhookDefinition) { if (args.WebhookEventId == default) { @@ -63,7 +68,7 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker return; } - if (!args.TryOnce) + if (!webhookDefinition.TryOnce) { var sendAttemptCount = await _webhookSendAttemptStore.GetSendAttemptCountAsync( args.TenantId, @@ -71,7 +76,8 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker args.WebhookSubscriptionId ); - if (sendAttemptCount > _options.MaxSendAttemptCount) + if ((webhookDefinition.MaxSendAttemptCount > 0 && sendAttemptCount > webhookDefinition.MaxSendAttemptCount) || + sendAttemptCount > _options.MaxSendAttemptCount) { return; } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookPublisher.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookPublisher.cs index 59ed0b102..be46197ae 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookPublisher.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookPublisher.cs @@ -15,18 +15,15 @@ namespace LINGYUN.Abp.Webhooks public IWebhookEventStore WebhookEventStore { get; set; } private readonly ICurrentTenant _currentTenant; - private readonly IGuidGenerator _guidGenerator; private readonly IBackgroundJobManager _backgroundJobManager; private readonly IWebhookSubscriptionManager _webhookSubscriptionManager; public DefaultWebhookPublisher( IWebhookSubscriptionManager webhookSubscriptionManager, ICurrentTenant currentTenant, - IGuidGenerator guidGenerator, IBackgroundJobManager backgroundJobManager) { _currentTenant = currentTenant; - _guidGenerator = guidGenerator; _backgroundJobManager = backgroundJobManager; _webhookSubscriptionManager = webhookSubscriptionManager; @@ -103,7 +100,7 @@ namespace LINGYUN.Abp.Webhooks } } } - + await _backgroundJobManager.EnqueueAsync(new WebhookSenderArgs { TenantId = webhookSubscription.TenantId, diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookDefinition.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookDefinition.cs index 88f3a074d..1c1461e8a 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookDefinition.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookDefinition.cs @@ -11,6 +11,16 @@ namespace LINGYUN.Abp.Webhooks /// public string Name { get; } + /// + /// Tries to send webhook only one time without checking to send attempt count + /// + public bool TryOnce { get; set; } + + /// + /// Defined maximum number of sending times + /// + public int MaxSendAttemptCount { get; set; } + /// /// Display name of the webhook. /// Optional. diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookSenderArgs.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookSenderArgs.cs index 7910ba0b1..cb30acbf9 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookSenderArgs.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookSenderArgs.cs @@ -46,11 +46,6 @@ namespace LINGYUN.Abp.Webhooks /// public IDictionary Headers { get; set; } - /// - /// Tries to send webhook only one time without checking to send attempt count - /// - public bool TryOnce { get; set; } - /// /// True: It sends the exact same data as the parameter to clients. ///