Skip to content

Commit

Permalink
Merge pull request #101 from Poltuu/up
Browse files Browse the repository at this point in the history
ADD : WebHookReplayService
  • Loading branch information
Poltuu authored Jun 16, 2020
2 parents 3cdf73d + ee3804b commit 1aca6d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static IHarpoonBuilder RegisterWebHooksUsingEfStorage<TContext>(this IHar
harpoon.Services.TryAddScoped<IWebHookStore, WebHookStore<TContext>>();
harpoon.Services.TryAddScoped<IWebHookRegistrationStore, WebHookRegistrationStore<TContext>>();

harpoon.Services.TryAddScoped<WebHookReplayService<TContext>>();

return harpoon;
}

Expand Down
44 changes: 44 additions & 0 deletions Harpoon.Registrations.EFStorage/WebHookReplayService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Harpoon.Registrations.EFStorage
{
/// <summary>
/// A class able to replay failed notifications
/// </summary>
/// <typeparam name="TContext"></typeparam>
public class WebHookReplayService<TContext>
where TContext : DbContext, IRegistrationsContext
{
private readonly TContext _context;
private readonly IWebHookSender _sender;

/// <summary>Initializes a new instance of the <see cref="WebHookReplayService{TContext}"/> class.</summary>
public WebHookReplayService(TContext context, IWebHookSender sender)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_sender = sender ?? throw new ArgumentNullException(nameof(sender));
}

/// <summary>
/// Replays every failed notification
/// </summary>
/// <param name="start"></param>
/// <returns></returns>
public async Task ReplayFailedNotification(DateTime start)
{
var failedNotifications = await _context.WebHookLogs
.Where(l => l.Error != null && l.CreatedAt >= start)
.Include(e => e.WebHookNotification)
.Include(e => e.WebHook)
.ToListAsync();

foreach (var fail in failedNotifications)
{
await _sender.SendAsync(new WebHookWorkItem(fail.WebHookNotificationId, fail.WebHookNotification, fail.WebHook), default);
}
}
}
}

0 comments on commit 1aca6d3

Please sign in to comment.