forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecUtils.cs
More file actions
145 lines (129 loc) · 4.16 KB
/
ExecUtils.cs
File metadata and controls
145 lines (129 loc) · 4.16 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
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
using System;
using System.Collections.Generic;
using System.Threading;
using ServiceStack.Logging;
#if NETSTANDARD1_3
using System.Threading.Tasks;
#endif
namespace ServiceStack
{
public static class ExecUtils
{
public static void LogError(Type declaringType, string clientMethodName, Exception ex)
{
var log = LogManager.GetLogger(declaringType);
log.Error($"'{declaringType.FullName}' threw an error on {clientMethodName}: {ex.Message}", ex);
}
public static void ExecAll<T>(this IEnumerable<T> instances, Action<T> action)
{
foreach (var instance in instances)
{
try
{
action(instance);
}
catch (Exception ex)
{
LogError(instance.GetType(), action.GetType().Name, ex);
}
}
}
public static void ExecAllWithFirstOut<T, TReturn>(this IEnumerable<T> instances, Func<T, TReturn> action, ref TReturn firstResult)
{
foreach (var instance in instances)
{
try
{
var result = action(instance);
if (!Equals(firstResult, default(TReturn)))
{
firstResult = result;
}
}
catch (Exception ex)
{
LogError(instance.GetType(), action.GetType().Name, ex);
}
}
}
public static TReturn ExecReturnFirstWithResult<T, TReturn>(this IEnumerable<T> instances, Func<T, TReturn> action)
{
foreach (var instance in instances)
{
try
{
var result = action(instance);
if (!Equals(result, default(TReturn)))
{
return result;
}
}
catch (Exception ex)
{
LogError(instance.GetType(), action.GetType().Name, ex);
}
}
return default(TReturn);
}
public static void RetryUntilTrue(Func<bool> action, TimeSpan? timeOut)
{
var i = 0;
var firstAttempt = DateTime.UtcNow;
while (timeOut == null || DateTime.UtcNow - firstAttempt < timeOut.Value)
{
i++;
if (action())
{
return;
}
SleepBackOffMultiplier(i);
}
throw new TimeoutException($"Exceeded timeout of {timeOut.Value}");
}
public static void RetryOnException(Action action, TimeSpan? timeOut)
{
var i = 0;
Exception lastEx = null;
var firstAttempt = DateTime.UtcNow;
while (timeOut == null || DateTime.UtcNow - firstAttempt < timeOut.Value)
{
i++;
try
{
action();
return;
}
catch (Exception ex)
{
lastEx = ex;
SleepBackOffMultiplier(i);
}
}
throw new TimeoutException($"Exceeded timeout of {timeOut.Value}", lastEx);
}
public static void RetryOnException(Action action, int maxRetries)
{
for (var i = 0; i < maxRetries; i++)
{
try
{
action();
break;
}
catch
{
if (i == maxRetries - 1) throw;
SleepBackOffMultiplier(i);
}
}
}
private static void SleepBackOffMultiplier(int i)
{
//exponential/random retry back-off.
var rand = new Random(Guid.NewGuid().GetHashCode());
var nextTry = rand.Next(
(int)Math.Pow(i, 2), (int)Math.Pow(i + 1, 2) + 1);
TaskUtils.Sleep(nextTry);
}
}
}