Skip to content

Commit

Permalink
feat: InvokeHelper.TryInvoke support retry
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Oct 10, 2024
1 parent 0937309 commit afa08dc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
4 changes: 3 additions & 1 deletion samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

// ServiceDecoratorTest.MainTest();

Expand Down Expand Up @@ -345,6 +345,8 @@

// InvokeHelper.TryInvoke(CommandExecutorTest.MainTest);

// InvokeHelper.TryInvoke(() => throw null, 3);

await InvokeHelper.TryInvokeAsync(TemplatingSample.MainTest);

ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");
Expand Down
88 changes: 67 additions & 21 deletions src/WeihanLi.Common/Helpers/InvokeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static async Task<double> ProfileAsync<T1, T2, T3>(Func<T1, T2, T3, Task>

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

private static readonly Lock _exitLock = new();
private static readonly Lock ExitLock = new();
private static volatile bool _exited;
private static readonly Lazy<CancellationTokenSource> LazyCancellationTokenSource = new();
private static void InvokeExitHandler(object? sender, EventArgs? args)
Expand All @@ -116,7 +116,7 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
// posixSignalContext.Cancel = true;
// }
// #endif
lock (_exitLock)
lock (ExitLock)
{
if (_exited) return;
Debug.WriteLine("exiting...");
Expand All @@ -130,105 +130,148 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
}
}

[Obsolete("Please use ApplicationHelper.ExitToken instead", true)]
public static CancellationToken GetExitToken() => GetExitTokenInternal();

internal static CancellationToken GetExitTokenInternal() => LazyCancellationTokenSource.Value.Token;

public static void TryInvoke(Action action)
public static void TryInvoke(Action action, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action();
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry-- > 0)
goto invoke;
}
}

public static void TryInvoke<T>(Action<T> action, T t)
public static void TryInvoke<T>(Action<T> action, T t, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action(t);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry-- > 0)
goto invoke;
}
}

public static void TryInvoke<T1, T2>(Action<T1, T2> action, T1 t1, T2 t2)
public static void TryInvoke<T1, T2>(Action<T1, T2> action, T1 t1, T2 t2, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action(t1, t2);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync<T1, T2>(Func<T1, T2, Task> func, T1 t1, T2 t2)
public static async Task TryInvokeAsync<T1, T2>(Func<T1, T2, Task> func, T1 t1, T2 t2, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
await func(t1, t2);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static void TryInvoke<T1, T2, T3>(Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
public static void TryInvoke<T1, T2, T3>(Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3, int? maxRetryCount = null)
{
Guard.NotNull(action, nameof(action));
Guard.NotNull(action);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
action(t1, t2, t3);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync(Func<Task> func)
public static async Task TryInvokeAsync(Func<Task> func, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:


try
{
await func();
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync<T>(Func<T, Task> func, T t)
public static async Task TryInvokeAsync<T>(Func<T, Task> func, T t, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
await func(t);
}
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);
if (maxRetry -- > 0)
goto invoke;
}
}

public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func, T1 t1, T2 t2, T3 t3)
public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func, T1 t1, T2 t2, T3 t3, int? maxRetryCount = null)
{
Guard.NotNull(func, nameof(func));
Guard.NotNull(func);

var maxRetry = maxRetryCount.GetValueOrDefault();
invoke:

try
{
Expand All @@ -237,6 +280,9 @@ public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func,
catch (Exception ex)
{
OnInvokeException?.Invoke(ex);

if (maxRetry -- > 0)
goto invoke;
}
}

Expand Down

0 comments on commit afa08dc

Please sign in to comment.