-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatchdogService.cs
72 lines (67 loc) · 3.18 KB
/
WatchdogService.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
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using Microsoft.ServiceFabric.Services.Runtime;
using System;
using System.Collections.Generic;
using System.Fabric;
using System.Fabric.Health;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace WatchdogService
{
/// <summary>
/// The FabricRuntime creates an instance of this class for each service type instance.
/// </summary>
internal sealed class WatchdogService : StatelessService
{
private Uri applicationName = new Uri("fabric:/TimeServiceApplication");
private string serviceManifestName = "TimeServicePkg";
private string nodeName = FabricRuntime.GetNodeContext().NodeName;
private FabricClient Client = new FabricClient(new FabricClientSettings() { HealthReportSendInterval = TimeSpan.FromSeconds(0) });
public WatchdogService(StatelessServiceContext serviceContext) : base(serviceContext)
{
}
protected override async Task RunAsync(CancellationToken cancelServiceInstance)
{
int failedCount = 0;
while (!cancelServiceInstance.IsCancellationRequested)
{
using (WebClient client = new WebClient())
{
try
{
string payload = client.DownloadString(new Uri("http://localhost:8088/"));
if (!string.IsNullOrEmpty(payload))
{
failedCount = 0;
var deployedServicePackageHealthReport = new DeployedServicePackageHealthReport(
applicationName,
serviceManifestName,
nodeName,
new HealthInformation("CustomWatchDog", "WebServerHealth", HealthState.Ok));
Client.HealthManager.ReportHealth(deployedServicePackageHealthReport);
ServiceEventSource.Current.ServiceMessage(this.Context, "Watchdog is happy");
}
}
catch (WebException)
{
failedCount++;
ServiceEventSource.Current.ServiceMessage(this.Context, "Watchdog had detected " + failedCount + " failures.");
if (failedCount >= 5)
{
var deployedServicePackageHealthReport = new DeployedServicePackageHealthReport(
applicationName,
serviceManifestName,
nodeName,
new HealthInformation("CustomWatchDog", "WebServerHealth", HealthState.Error));
Client.HealthManager.ReportHealth(deployedServicePackageHealthReport);
ServiceEventSource.Current.ServiceMessage(this.Context, "Watchdog is sad.");
}
}
}
await Task.Delay(TimeSpan.FromSeconds(1), cancelServiceInstance);
}
}
}
}