-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCustomChecksMailNotification.cs
More file actions
97 lines (83 loc) · 3.74 KB
/
CustomChecksMailNotification.cs
File metadata and controls
97 lines (83 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace ServiceControl.Notifications.Email
{
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Contracts.CustomChecks;
using Infrastructure.DomainEvents;
using Microsoft.Extensions.Logging;
using NServiceBus;
using ServiceBus.Management.Infrastructure.Settings;
class CustomChecksMailNotification : IDomainHandler<CustomCheckFailed>, IDomainHandler<CustomCheckSucceeded>
{
readonly IMessageSession messageSession;
readonly EmailThrottlingState throttlingState;
readonly string instanceName;
string instanceAddress;
string[] serviceControlHealthCustomCheckIds = {
"Audit Message Ingestion Process",
"Audit Message Ingestion",
"ServiceControl.Audit database",
"Dead Letter Queue",
"ServiceControl Primary Instance",
"ServiceControl database",
"ServiceControl Remotes",
"Error Message Ingestion Process",
"Error Message Ingestion"
};
public CustomChecksMailNotification(IMessageSession messageSession, Settings settings, EmailThrottlingState throttlingState, ILogger<CustomChecksMailNotification> logger)
{
this.messageSession = messageSession;
this.throttlingState = throttlingState;
this.logger = logger;
instanceName = settings.InstanceName;
instanceAddress = settings.ApiUrl;
if (string.IsNullOrWhiteSpace(settings.NotificationsFilter) == false)
{
serviceControlHealthCustomCheckIds = NotificationsFilterParser.Parse(settings.NotificationsFilter);
}
}
public Task Handle(CustomCheckFailed domainEvent, CancellationToken cancellationToken)
{
if (IsHealthCheck(domainEvent.CustomCheckId))
{
if (throttlingState.IsThrottling())
{
logger.LogWarning("Email notification throttled");
return Task.CompletedTask;
}
return messageSession.SendLocal(new SendEmailNotification
{
FailureNotification = true,
Subject = $"[{instanceName}] health check failed",
Body = $@"{domainEvent.Category} check for ServiceControl instance {instanceName} at {instanceAddress}.
{domainEvent.CustomCheckId} failed on {domainEvent.FailedAt}.
{domainEvent.FailureReason}"
}, cancellationToken: cancellationToken);
}
return Task.CompletedTask;
}
public Task Handle(CustomCheckSucceeded domainEvent, CancellationToken cancellationToken)
{
if (IsHealthCheck(domainEvent.CustomCheckId))
{
if (throttlingState.IsThrottling())
{
logger.LogWarning("Email notification throttled");
return Task.CompletedTask;
}
return messageSession.SendLocal(new SendEmailNotification
{
FailureNotification = false,
Subject = $"[{instanceName}] health check succeeded",
Body = $@"{domainEvent.Category} check for ServiceControl instance {instanceName} at {instanceAddress}.
{domainEvent.CustomCheckId} succeeded on {domainEvent.SucceededAt}."
}, cancellationToken: cancellationToken);
}
return Task.CompletedTask;
}
bool IsHealthCheck(string checkId) => serviceControlHealthCustomCheckIds.Any(id => string.Equals(id, checkId, StringComparison.InvariantCultureIgnoreCase));
readonly ILogger<CustomChecksMailNotification> logger;
}
}