Browse Source

perf: users can customize the number of Webhook send count

pull/532/head
cKey 4 years ago
parent
commit
8fa5253601
  1. 18
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/BackgroundWorker/WebhookSenderJob.cs
  2. 5
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookPublisher.cs
  3. 10
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookDefinition.cs
  4. 5
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookSenderArgs.cs

18
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<WebhookSenderArgs>, ITransientDependency public class WebhookSenderJob : AsyncBackgroundJob<WebhookSenderArgs>, ITransientDependency
{ {
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IWebhookDefinitionManager _webhookDefinitionManager;
private readonly IWebhookSubscriptionManager _webhookSubscriptionManager; private readonly IWebhookSubscriptionManager _webhookSubscriptionManager;
private readonly IWebhookSendAttemptStore _webhookSendAttemptStore; private readonly IWebhookSendAttemptStore _webhookSendAttemptStore;
private readonly IWebhookSender _webhookSender; private readonly IWebhookSender _webhookSender;
@ -19,12 +20,14 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker
public WebhookSenderJob( public WebhookSenderJob(
IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkManager unitOfWorkManager,
IWebhookDefinitionManager webhookDefinitionManager,
IWebhookSubscriptionManager webhookSubscriptionManager, IWebhookSubscriptionManager webhookSubscriptionManager,
IWebhookSendAttemptStore webhookSendAttemptStore, IWebhookSendAttemptStore webhookSendAttemptStore,
IWebhookSender webhookSender, IWebhookSender webhookSender,
IOptions<AbpWebhooksOptions> options) IOptions<AbpWebhooksOptions> options)
{ {
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
_webhookDefinitionManager = webhookDefinitionManager;
_webhookSubscriptionManager = webhookSubscriptionManager; _webhookSubscriptionManager = webhookSubscriptionManager;
_webhookSendAttemptStore = webhookSendAttemptStore; _webhookSendAttemptStore = webhookSendAttemptStore;
_webhookSender = webhookSender; _webhookSender = webhookSender;
@ -33,11 +36,13 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker
public override async Task ExecuteAsync(WebhookSenderArgs args) public override async Task ExecuteAsync(WebhookSenderArgs args)
{ {
if (args.TryOnce) var webhookDefinition = _webhookDefinitionManager.Get(args.WebhookName);
if (webhookDefinition.TryOnce)
{ {
try try
{ {
await SendWebhook(args); await SendWebhook(args, webhookDefinition);
} }
catch (Exception e) catch (Exception e)
{ {
@ -47,11 +52,11 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker
} }
else 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) if (args.WebhookEventId == default)
{ {
@ -63,7 +68,7 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker
return; return;
} }
if (!args.TryOnce) if (!webhookDefinition.TryOnce)
{ {
var sendAttemptCount = await _webhookSendAttemptStore.GetSendAttemptCountAsync( var sendAttemptCount = await _webhookSendAttemptStore.GetSendAttemptCountAsync(
args.TenantId, args.TenantId,
@ -71,7 +76,8 @@ namespace LINGYUN.Abp.Webhooks.BackgroundWorker
args.WebhookSubscriptionId args.WebhookSubscriptionId
); );
if (sendAttemptCount > _options.MaxSendAttemptCount) if ((webhookDefinition.MaxSendAttemptCount > 0 && sendAttemptCount > webhookDefinition.MaxSendAttemptCount) ||
sendAttemptCount > _options.MaxSendAttemptCount)
{ {
return; return;
} }

5
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; } public IWebhookEventStore WebhookEventStore { get; set; }
private readonly ICurrentTenant _currentTenant; private readonly ICurrentTenant _currentTenant;
private readonly IGuidGenerator _guidGenerator;
private readonly IBackgroundJobManager _backgroundJobManager; private readonly IBackgroundJobManager _backgroundJobManager;
private readonly IWebhookSubscriptionManager _webhookSubscriptionManager; private readonly IWebhookSubscriptionManager _webhookSubscriptionManager;
public DefaultWebhookPublisher( public DefaultWebhookPublisher(
IWebhookSubscriptionManager webhookSubscriptionManager, IWebhookSubscriptionManager webhookSubscriptionManager,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IGuidGenerator guidGenerator,
IBackgroundJobManager backgroundJobManager) IBackgroundJobManager backgroundJobManager)
{ {
_currentTenant = currentTenant; _currentTenant = currentTenant;
_guidGenerator = guidGenerator;
_backgroundJobManager = backgroundJobManager; _backgroundJobManager = backgroundJobManager;
_webhookSubscriptionManager = webhookSubscriptionManager; _webhookSubscriptionManager = webhookSubscriptionManager;
@ -103,7 +100,7 @@ namespace LINGYUN.Abp.Webhooks
} }
} }
} }
await _backgroundJobManager.EnqueueAsync(new WebhookSenderArgs await _backgroundJobManager.EnqueueAsync(new WebhookSenderArgs
{ {
TenantId = webhookSubscription.TenantId, TenantId = webhookSubscription.TenantId,

10
aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookDefinition.cs

@ -11,6 +11,16 @@ namespace LINGYUN.Abp.Webhooks
/// </summary> /// </summary>
public string Name { get; } public string Name { get; }
/// <summary>
/// Tries to send webhook only one time without checking to send attempt count
/// </summary>
public bool TryOnce { get; set; }
/// <summary>
/// Defined maximum number of sending times
/// </summary>
public int MaxSendAttemptCount { get; set; }
/// <summary> /// <summary>
/// Display name of the webhook. /// Display name of the webhook.
/// Optional. /// Optional.

5
aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/WebhookSenderArgs.cs

@ -46,11 +46,6 @@ namespace LINGYUN.Abp.Webhooks
/// </summary> /// </summary>
public IDictionary<string, string> Headers { get; set; } public IDictionary<string, string> Headers { get; set; }
/// <summary>
/// Tries to send webhook only one time without checking to send attempt count
/// </summary>
public bool TryOnce { get; set; }
/// <summary> /// <summary>
/// True: It sends the exact same data as the parameter to clients. /// True: It sends the exact same data as the parameter to clients.
/// <para> /// <para>

Loading…
Cancel
Save