forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecExtensions.cs
More file actions
144 lines (128 loc) · 4.33 KB
/
ExecExtensions.cs
File metadata and controls
144 lines (128 loc) · 4.33 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ServiceStack.Logging;
namespace ServiceStack.Common
{
public static class ExecExtensions
{
public static void LogError(Type declaringType, string clientMethodName, Exception ex)
{
var log = LogManager.GetLogger(declaringType);
log.Error(string.Format("'{0}' threw an error on {1}: {2}", declaringType.FullName, 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.Now;
while (timeOut == null || DateTime.Now - firstAttempt < timeOut.Value)
{
i++;
if (action())
{
return;
}
SleepBackOffMultiplier(i);
}
throw new TimeoutException(string.Format("Exceeded timeout of {0}", timeOut.Value));
}
public static void RetryOnException(Action action, TimeSpan? timeOut)
{
var i = 0;
Exception lastEx = null;
var firstAttempt = DateTime.Now;
while (timeOut == null || DateTime.Now - firstAttempt < timeOut.Value)
{
i++;
try
{
action();
return;
}
catch (Exception ex)
{
lastEx = ex;
SleepBackOffMultiplier(i);
}
}
throw new TimeoutException(string.Format("Exceeded timeout of {0}", 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);
Thread.Sleep(nextTry);
}
}
}