-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExternalService.cs
80 lines (69 loc) · 2.88 KB
/
ExternalService.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
using System;
namespace DotStd
{
/// <summary>
/// We are dependent on some external service that we don't directly control.
/// Allow the system to continue to operate if this service is not up?
/// </summary>
public abstract class ExternalService
{
public abstract string Name { get; }
public abstract string BaseURL { get; } // General informational url for admin to look at.
public abstract string Icon { get; } // "<i class='fab fa-facebook'></i>"
public bool IsConfigured = false; // was Config called successfully? i have a password, etc. Is enabling this an option?
public bool IsEnabled = true; // false = We know the service is down. don't use it and use whatever backup or user preemptive warning of its failure.
public bool IsActive => IsEnabled && IsConfigured; // this service should be good ?
public DateTime? LastTry; // Last UTC time we tried to use this service.
public DateTime? LastSuccess; // Last UTC time the service seemed to work correctly.
public string? ErrorMessage; // how did this service provider fail?
/// <summary>
/// Describe the last known status of this service.
/// </summary>
/// <returns></returns>
public virtual string GetStatusStr()
{
// Date of failure? Dates even if disabled? Current first attempt looks like failure ?
if (!IsConfigured)
return "Not configured";
if (!IsEnabled)
return "Disabled";
if (LastTry == null)
return "Not attempted";
if (LastSuccess != null && LastSuccess >= LastTry)
return $"Success {LastSuccess}"; // FIX ME ?
if (!string.IsNullOrWhiteSpace(ErrorMessage))
return $"Error '{ErrorMessage}'";
if (LastSuccess == null)
return "No success";
return "Failed";
}
public virtual string GetDescHtml()
{
return $"{Icon} <a href='{BaseURL}'>{Name}</a> {GetStatusStr()}";
}
/// <summary>
/// We are about to try to use the service now.
/// Expect a call to UpdateSuccess or UpdateFailure next.
/// </summary>
public void UpdateTry()
{
LastTry = TimeNow.Utc;
}
/// <summary>
/// It worked!
/// </summary>
public void UpdateSuccess()
{
LastSuccess = LastTry = TimeNow.Utc;
}
/// <summary>
/// It failed. Maybe just the call and not the service itself. check for StringUtil._NoErrorMsg ?
/// </summary>
/// <param name="errorMsg"></param>
public void UpdateFailure(string errorMsg)
{
LastTry = TimeNow.Utc;
ErrorMessage = errorMsg;
}
}
}