-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/notification cancellation hangfire job (#317)
* fix: 404 for deleting correspondence before publishing for recipient * move cancellation of notification to hangfire job * Add slack notifications whenever maxRetries occurs * add backgroundJob for failed correspondences in PublishCorrespondenceHandler also * remove unused return variable * remove unused line * refactor and add test for slack message posting * Added infra and ci/cd code for slack url * use appsettings instead of .env file for slackUrl * Add ISlackClient in DependencyInjection instead * implement SlackDevClient to be used during development * Make SlackDevClient usable when testing with actual Slack Url * check null or whitespace * fix: return true when webhookUri is empty during testing --------- Co-authored-by: Roar Mjelde <[email protected]>
- Loading branch information
1 parent
5a20d65
commit ec0a70b
Showing
19 changed files
with
246 additions
and
27 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
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
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
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
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
70 changes: 70 additions & 0 deletions
70
src/Altinn.Correspondence.Application/CancelNotification/CancelNotificationHandler.cs
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,70 @@ | ||
using System.Runtime.CompilerServices; | ||
using Altinn.Correspondence.Core.Models.Entities; | ||
using Altinn.Correspondence.Core.Repositories; | ||
using Hangfire; | ||
using Hangfire.Server; | ||
using Microsoft.Extensions.Logging; | ||
using Slack.Webhooks; | ||
|
||
[assembly: InternalsVisibleTo("Altinn.Correspondence.Tests")] | ||
namespace Altinn.Correspondence.Application.CancelNotification | ||
{ | ||
public class CancelNotificationHandler | ||
{ | ||
private readonly ILogger<CancelNotificationHandler> _logger; | ||
private readonly IAltinnNotificationService _altinnNotificationService; | ||
private readonly ISlackClient _slackClient; | ||
private const string TestChannel = "#test-varslinger"; | ||
private const string RetryCountKey = "RetryCount"; | ||
private const int MaxRetries = 10; | ||
public CancelNotificationHandler( | ||
ILogger<CancelNotificationHandler> logger, | ||
IAltinnNotificationService altinnNotificationService, | ||
ISlackClient slackClient) | ||
{ | ||
_logger = logger; | ||
_altinnNotificationService = altinnNotificationService; | ||
_slackClient = slackClient; | ||
} | ||
|
||
[AutomaticRetry(Attempts = MaxRetries)] | ||
public async Task Process(PerformContext context, List<CorrespondenceNotificationEntity> notificationEntities, CancellationToken cancellationToken = default) | ||
{ | ||
var retryAttempts = context.GetJobParameter<int>(RetryCountKey); | ||
_logger.LogInformation("Cancelling notifications for purged correspondence. Retry attempt: {retryAttempts}", retryAttempts); | ||
await CancelNotification(notificationEntities, retryAttempts, cancellationToken); | ||
} | ||
internal async Task CancelNotification(List<CorrespondenceNotificationEntity> notificationEntities, int retryAttempts, CancellationToken cancellationToken) | ||
{ | ||
foreach (var notification in notificationEntities) | ||
{ | ||
if (notification.RequestedSendTime <= DateTimeOffset.UtcNow) continue; // Notification has already been sent | ||
|
||
string? notificationOrderId = notification.NotificationOrderId?.ToString(); | ||
|
||
if (string.IsNullOrWhiteSpace(notificationOrderId)) | ||
{ | ||
var error = $"Error while cancelling notification. NotificationOrderId is null for notificationId: {notification.Id}"; | ||
if (retryAttempts == MaxRetries) SendSlackNotificationWithMessage(error); | ||
throw new Exception(error); | ||
} | ||
bool isCancellationSuccessful = await _altinnNotificationService.CancelNotification(notificationOrderId, cancellationToken); | ||
if (!isCancellationSuccessful) | ||
{ | ||
var error = $"Error while cancelling notification. Failed to cancel notification for notificationId: {notification.Id}"; | ||
if (retryAttempts == MaxRetries) SendSlackNotificationWithMessage(error); | ||
throw new Exception(error); | ||
} | ||
} | ||
} | ||
private void SendSlackNotificationWithMessage(string message) | ||
{ | ||
var slackMessage = new SlackMessage | ||
{ | ||
Text = message, | ||
Channel = TestChannel, | ||
}; | ||
_slackClient.Post(slackMessage); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.