-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
NotificationService.cs
225 lines (201 loc) · 8.46 KB
/
NotificationService.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Linq;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.HealthCheck;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Indexers.Events;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Update.History.Events;
namespace NzbDrone.Core.Notifications
{
public class NotificationService
: IHandle<HealthCheckFailedEvent>,
IHandle<HealthCheckRestoredEvent>,
IHandleAsync<HealthCheckCompleteEvent>,
IHandle<UpdateInstalledEvent>,
IHandle<IndexerDownloadEvent>
{
private readonly INotificationFactory _notificationFactory;
private readonly INotificationStatusService _notificationStatusService;
private readonly Logger _logger;
public NotificationService(INotificationFactory notificationFactory, INotificationStatusService notificationStatusService, Logger logger)
{
_notificationFactory = notificationFactory;
_notificationStatusService = notificationStatusService;
_logger = logger;
}
private bool ShouldHandleHealthFailure(HealthCheck.HealthCheck healthCheck, bool includeWarnings)
{
return healthCheck.Type switch
{
HealthCheckResult.Error => true,
HealthCheckResult.Warning when includeWarnings => true,
_ => false
};
}
private bool ShouldHandleOnGrab(GrabMessage message, bool includeManual)
{
return message.GrabTrigger switch
{
GrabTrigger.Api => true,
GrabTrigger.Manual when includeManual => true,
_ => false
};
}
private string GetMessage(ReleaseInfo release, GrabTrigger grabTrigger, string source, string downloadClient)
{
var message = string.Format("{0} grabbed by {1} from {2}",
release.Title,
source,
release.Indexer);
if (grabTrigger == GrabTrigger.Manual)
{
message = string.Format("{0} manually grabbed in Prowlarr from {1}",
release.Title,
release.Indexer);
}
if (downloadClient.IsNotNullOrWhiteSpace())
{
message += $" and sent to {downloadClient}";
}
return message;
}
public void Handle(HealthCheckFailedEvent message)
{
// Don't send health check notifications during the start up grace period,
// once that duration expires they they'll be retested and fired off if necessary.
if (message.IsInStartupGracePeriod)
{
return;
}
foreach (var notification in _notificationFactory.OnHealthIssueEnabled())
{
try
{
if (ShouldHandleHealthFailure(message.HealthCheck, ((NotificationDefinition)notification.Definition).IncludeHealthWarnings))
{
notification.OnHealthIssue(message.HealthCheck);
_notificationStatusService.RecordSuccess(notification.Definition.Id);
}
}
catch (Exception ex)
{
_notificationStatusService.RecordFailure(notification.Definition.Id);
_logger.Warn(ex, "Unable to send OnHealthIssue notification to: " + notification.Definition.Name);
}
}
}
public void Handle(HealthCheckRestoredEvent message)
{
if (message.IsInStartupGracePeriod)
{
return;
}
foreach (var notification in _notificationFactory.OnHealthRestoredEnabled())
{
try
{
if (ShouldHandleHealthFailure(message.PreviousCheck, ((NotificationDefinition)notification.Definition).IncludeHealthWarnings))
{
notification.OnHealthRestored(message.PreviousCheck);
_notificationStatusService.RecordSuccess(notification.Definition.Id);
}
}
catch (Exception ex)
{
_notificationStatusService.RecordFailure(notification.Definition.Id);
_logger.Warn(ex, "Unable to send OnHealthRestored notification to: " + notification.Definition.Name);
}
}
}
public void HandleAsync(HealthCheckCompleteEvent message)
{
ProcessQueue();
}
public void Handle(UpdateInstalledEvent message)
{
var updateMessage = new ApplicationUpdateMessage();
updateMessage.Message = $"Prowlarr updated from {message.PreviousVerison.ToString()} to {message.NewVersion.ToString()}";
updateMessage.PreviousVersion = message.PreviousVerison;
updateMessage.NewVersion = message.NewVersion;
foreach (var notification in _notificationFactory.OnApplicationUpdateEnabled())
{
try
{
notification.OnApplicationUpdate(updateMessage);
_notificationStatusService.RecordSuccess(notification.Definition.Id);
}
catch (Exception ex)
{
_notificationStatusService.RecordFailure(notification.Definition.Id);
_logger.Warn(ex, "Unable to send OnApplicationUpdate notification to: " + notification.Definition.Name);
}
}
}
private void ProcessQueue()
{
foreach (var notification in _notificationFactory.GetAvailableProviders())
{
try
{
notification.ProcessQueue();
}
catch (Exception ex)
{
_logger.Warn(ex, "Unable to process notification queue for " + notification.Definition.Name);
}
}
}
public void Handle(IndexerDownloadEvent message)
{
var grabMessage = new GrabMessage
{
Release = message.Release,
Source = message.Source,
Host = message.Host,
Successful = message.Successful,
DownloadClientName = message.DownloadClientName,
DownloadClientType = message.DownloadClient,
DownloadId = message.DownloadId,
Redirect = message.Redirect,
GrabTrigger = message.GrabTrigger,
Message = GetMessage(message.Release, message.GrabTrigger, message.Source, message.DownloadClientName)
};
foreach (var notification in _notificationFactory.OnGrabEnabled())
{
try
{
if (ShouldHandleIndexer(notification.Definition, (IndexerDefinition)message.Indexer.Definition) &&
ShouldHandleOnGrab(grabMessage, ((NotificationDefinition)notification.Definition).IncludeManualGrabs))
{
notification.OnGrab(grabMessage);
_notificationStatusService.RecordSuccess(notification.Definition.Id);
}
}
catch (Exception ex)
{
_notificationStatusService.RecordFailure(notification.Definition.Id);
_logger.Error(ex, "Unable to send OnGrab notification to {0}", notification.Definition.Name);
}
}
}
private bool ShouldHandleIndexer(ProviderDefinition definition, ProviderDefinition indexer)
{
if (definition.Tags.Empty())
{
_logger.Debug("No tags set for this notification.");
return true;
}
if (definition.Tags.Intersect(indexer.Tags).Any())
{
_logger.Debug("Notification and indexer have one or more intersecting tags.");
return true;
}
_logger.Debug("{0} does not have any intersecting tags with {1}. Notification will not be sent.", definition.Name, indexer.Name);
return false;
}
}
}