-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cs
More file actions
109 lines (96 loc) · 3.98 KB
/
Copy pathutil.cs
File metadata and controls
109 lines (96 loc) · 3.98 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
98
99
100
101
102
103
104
105
106
107
108
109
using Cronos;
using CLOOPS.NATS;
using NATS.Client.Core;
namespace CLOOPS.microservices;
/// <summary>
/// Contains utility functions for the application
/// </summary>
public class BaseUtil : CLOOPS.NATS.BaseNatsUtil
{
/// <summary>
/// Default amount of time to wait for a NATS connection during startup coordination.
/// </summary>
public static readonly TimeSpan NatsConnectionWaitTimeout = TimeSpan.FromSeconds(60);
/// <summary>
/// Default polling interval while waiting for a NATS connection during startup coordination.
/// </summary>
public static readonly TimeSpan NatsConnectionPollInterval = TimeSpan.FromMilliseconds(250);
/// <summary>
/// Checks whether a type inherits from an open generic type, such as BaseCacheService<>.
/// </summary>
/// <param name="type">The concrete type to check</param>
/// <param name="openGenericType">The open generic type definition</param>
/// <returns>True when the type inherits from the open generic type</returns>
public static bool IsAssignableToOpenGeneric(Type type, Type openGenericType)
{
var currentType = type;
while (currentType != null && currentType != typeof(object))
{
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == openGenericType)
{
return true;
}
currentType = currentType.BaseType;
}
return false;
}
/// <summary>
/// Parses a cron expression and returns a CronExpression object
/// </summary>
/// <param name="cron">The cron expression to parse</param>
/// <returns>A CronExpression object</returns>
/// <exception cref="Exception">Thrown if the cron expression is invalid</exception>
public static CronExpression GetCronExpression(string cron)
{
var mode = cron.Split(" ").Count() == 5 ? CronFormat.Standard : CronFormat.IncludeSeconds;
var cronExpression = CronExpression.Parse(cron, mode);
if (cronExpression is null)
{
throw new Exception($"Invalid cron expression: {cron}");
}
return cronExpression;
}
/// <summary>
/// Waits until a NATS client reports an open connection or the timeout elapses.
/// </summary>
/// <param name="natsClient">The NATS client to observe.</param>
/// <param name="ct">Cancellation token.</param>
/// <param name="timeout">Optional wait timeout. Defaults to <see cref="NatsConnectionWaitTimeout"/>.</param>
/// <param name="pollInterval">Optional poll interval. Defaults to <see cref="NatsConnectionPollInterval"/>.</param>
/// <returns>True when NATS is connected; otherwise false.</returns>
public static async Task<bool> WaitForNatsConnectionAsync(
ICloopsNatsClient? natsClient,
CancellationToken ct,
TimeSpan? timeout = null,
TimeSpan? pollInterval = null)
{
if (natsClient == null)
{
return false;
}
if (natsClient.Connection.ConnectionState == NatsConnectionState.Open)
{
return true;
}
var deadline = DateTimeOffset.UtcNow + (timeout ?? NatsConnectionWaitTimeout);
var delay = pollInterval ?? NatsConnectionPollInterval;
while (natsClient.Connection.ConnectionState != NatsConnectionState.Open)
{
if (ct.IsCancellationRequested || DateTimeOffset.UtcNow >= deadline)
{
return false;
}
await Task.Delay(delay, ct).ConfigureAwait(false);
}
return true;
}
/// <summary>
/// Returns true when the NATS client exists and currently reports an open connection.
/// </summary>
/// <param name="natsClient">The NATS client to inspect.</param>
/// <returns>True when NATS is connected; otherwise false.</returns>
public static bool IsNatsConnected(ICloopsNatsClient? natsClient)
{
return natsClient?.Connection.ConnectionState == NatsConnectionState.Open;
}
}