Skip to content

Commit afa08dc

Browse files
committed
feat: InvokeHelper.TryInvoke support retry
1 parent 0937309 commit afa08dc

File tree

2 files changed

+70
-22
lines changed

2 files changed

+70
-22
lines changed

samples/DotNetCoreSample/Program.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Console.WriteLine("----------DotNetCoreSample----------");
88

9-
InvokeHelper.OnInvokeException = ex => ConsoleHelper.ErrorWriteWithColor(ex.ToString(), ConsoleColor.DarkRed);
9+
InvokeHelper.OnInvokeException = ex => ConsoleHelper.ErrorWriteLineWithColor(ex.ToString(), ConsoleColor.DarkRed);
1010

1111
// ServiceDecoratorTest.MainTest();
1212

@@ -345,6 +345,8 @@
345345

346346
// InvokeHelper.TryInvoke(CommandExecutorTest.MainTest);
347347

348+
// InvokeHelper.TryInvoke(() => throw null, 3);
349+
348350
await InvokeHelper.TryInvokeAsync(TemplatingSample.MainTest);
349351

350352
ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");

src/WeihanLi.Common/Helpers/InvokeHelper.cs

+67-21
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static async Task<double> ProfileAsync<T1, T2, T3>(Func<T1, T2, T3, Task>
9999

100100
public static Action<Exception>? OnInvokeException { get; set; }
101101

102-
private static readonly Lock _exitLock = new();
102+
private static readonly Lock ExitLock = new();
103103
private static volatile bool _exited;
104104
private static readonly Lazy<CancellationTokenSource> LazyCancellationTokenSource = new();
105105
private static void InvokeExitHandler(object? sender, EventArgs? args)
@@ -116,7 +116,7 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
116116
// posixSignalContext.Cancel = true;
117117
// }
118118
// #endif
119-
lock (_exitLock)
119+
lock (ExitLock)
120120
{
121121
if (_exited) return;
122122
Debug.WriteLine("exiting...");
@@ -130,105 +130,148 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
130130
}
131131
}
132132

133-
[Obsolete("Please use ApplicationHelper.ExitToken instead", true)]
134-
public static CancellationToken GetExitToken() => GetExitTokenInternal();
135-
136133
internal static CancellationToken GetExitTokenInternal() => LazyCancellationTokenSource.Value.Token;
137134

138-
public static void TryInvoke(Action action)
135+
public static void TryInvoke(Action action, int? maxRetryCount = null)
139136
{
140-
Guard.NotNull(action, nameof(action));
137+
Guard.NotNull(action);
138+
139+
var maxRetry = maxRetryCount.GetValueOrDefault();
140+
invoke:
141+
141142
try
142143
{
143144
action();
144145
}
145146
catch (Exception ex)
146147
{
147148
OnInvokeException?.Invoke(ex);
149+
if (maxRetry-- > 0)
150+
goto invoke;
148151
}
149152
}
150153

151-
public static void TryInvoke<T>(Action<T> action, T t)
154+
public static void TryInvoke<T>(Action<T> action, T t, int? maxRetryCount = null)
152155
{
153-
Guard.NotNull(action, nameof(action));
156+
Guard.NotNull(action);
157+
158+
var maxRetry = maxRetryCount.GetValueOrDefault();
159+
invoke:
160+
154161
try
155162
{
156163
action(t);
157164
}
158165
catch (Exception ex)
159166
{
160167
OnInvokeException?.Invoke(ex);
168+
if (maxRetry-- > 0)
169+
goto invoke;
161170
}
162171
}
163172

164-
public static void TryInvoke<T1, T2>(Action<T1, T2> action, T1 t1, T2 t2)
173+
public static void TryInvoke<T1, T2>(Action<T1, T2> action, T1 t1, T2 t2, int? maxRetryCount = null)
165174
{
166-
Guard.NotNull(action, nameof(action));
175+
Guard.NotNull(action);
176+
177+
var maxRetry = maxRetryCount.GetValueOrDefault();
178+
invoke:
179+
167180
try
168181
{
169182
action(t1, t2);
170183
}
171184
catch (Exception ex)
172185
{
173186
OnInvokeException?.Invoke(ex);
187+
if (maxRetry -- > 0)
188+
goto invoke;
174189
}
175190
}
176191

177-
public static async Task TryInvokeAsync<T1, T2>(Func<T1, T2, Task> func, T1 t1, T2 t2)
192+
public static async Task TryInvokeAsync<T1, T2>(Func<T1, T2, Task> func, T1 t1, T2 t2, int? maxRetryCount = null)
178193
{
179-
Guard.NotNull(func, nameof(func));
194+
Guard.NotNull(func);
195+
196+
var maxRetry = maxRetryCount.GetValueOrDefault();
197+
invoke:
198+
180199
try
181200
{
182201
await func(t1, t2);
183202
}
184203
catch (Exception ex)
185204
{
186205
OnInvokeException?.Invoke(ex);
206+
if (maxRetry -- > 0)
207+
goto invoke;
187208
}
188209
}
189210

190-
public static void TryInvoke<T1, T2, T3>(Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
211+
public static void TryInvoke<T1, T2, T3>(Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3, int? maxRetryCount = null)
191212
{
192-
Guard.NotNull(action, nameof(action));
213+
Guard.NotNull(action);
214+
215+
var maxRetry = maxRetryCount.GetValueOrDefault();
216+
invoke:
217+
193218
try
194219
{
195220
action(t1, t2, t3);
196221
}
197222
catch (Exception ex)
198223
{
199224
OnInvokeException?.Invoke(ex);
225+
if (maxRetry -- > 0)
226+
goto invoke;
200227
}
201228
}
202229

203-
public static async Task TryInvokeAsync(Func<Task> func)
230+
public static async Task TryInvokeAsync(Func<Task> func, int? maxRetryCount = null)
204231
{
205-
Guard.NotNull(func, nameof(func));
232+
Guard.NotNull(func);
233+
234+
var maxRetry = maxRetryCount.GetValueOrDefault();
235+
invoke:
236+
237+
206238
try
207239
{
208240
await func();
209241
}
210242
catch (Exception ex)
211243
{
212244
OnInvokeException?.Invoke(ex);
245+
if (maxRetry -- > 0)
246+
goto invoke;
213247
}
214248
}
215249

216-
public static async Task TryInvokeAsync<T>(Func<T, Task> func, T t)
250+
public static async Task TryInvokeAsync<T>(Func<T, Task> func, T t, int? maxRetryCount = null)
217251
{
218-
Guard.NotNull(func, nameof(func));
252+
Guard.NotNull(func);
253+
254+
var maxRetry = maxRetryCount.GetValueOrDefault();
255+
invoke:
256+
219257
try
220258
{
221259
await func(t);
222260
}
223261
catch (Exception ex)
224262
{
225263
OnInvokeException?.Invoke(ex);
264+
if (maxRetry -- > 0)
265+
goto invoke;
226266
}
227267
}
228268

229-
public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func, T1 t1, T2 t2, T3 t3)
269+
public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func, T1 t1, T2 t2, T3 t3, int? maxRetryCount = null)
230270
{
231-
Guard.NotNull(func, nameof(func));
271+
Guard.NotNull(func);
272+
273+
var maxRetry = maxRetryCount.GetValueOrDefault();
274+
invoke:
232275

233276
try
234277
{
@@ -237,6 +280,9 @@ public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func,
237280
catch (Exception ex)
238281
{
239282
OnInvokeException?.Invoke(ex);
283+
284+
if (maxRetry -- > 0)
285+
goto invoke;
240286
}
241287
}
242288

0 commit comments

Comments
 (0)