-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from Poltuu/up
ADD : WebHookReplayService
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |