diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-29 09:48:36 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-06-29 09:48:36 -0500 |
| commit | 5680bdf76da6c17b3bb2c7501fe2aea8cf456a59 (patch) | |
| tree | 29a99d65c5421fff4f797625bfdea6d7009b8ad3 /src | |
| parent | bb08368d27bee40f16e847ed95e87e602238172c (diff) | |
| download | slskd-5680bdf76da6c17b3bb2c7501fe2aea8cf456a59.tar.gz | |
add action on failure, trap errors executing delegates
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Retry.cs | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs index f18b7459..eccb6b62 100644 --- a/src/slskd/Common/Retry.cs +++ b/src/slskd/Common/Retry.cs @@ -31,16 +31,17 @@ namespace slskd /// </summary> /// <param name="task">The logic to execute.</param> /// <param name="isRetryable">A function returning a value indicating whether the last Exception is retryable.</param> + /// <param name="onFailure">An action to execute on failure.</param> /// <param name="maxAttempts">The maximum number of retry attempts.</param> /// <param name="maxDelayInMilliseconds">The maximum delay in milliseconds.</param> /// <returns>The execution context.</returns> - public static async Task Do(Func<Task> task, Func<int, Exception, bool> isRetryable = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue) + public static async Task Do(Func<Task> task, Func<int, Exception, bool> isRetryable = null, Action<int, Exception> onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue) { await Do<object>(async () => { await task(); return Task.FromResult<object>(null); - }, isRetryable, maxAttempts, maxDelayInMilliseconds); + }, isRetryable, onFailure, maxAttempts, maxDelayInMilliseconds); } /// <summary> @@ -48,13 +49,15 @@ namespace slskd /// </summary> /// <param name="task">The logic to execute.</param> /// <param name="isRetryable">A function returning a value indicating whether the last Exception is retryable.</param> + /// <param name="onFailure">An action to execute on failure.</param> /// <param name="maxAttempts">The maximum number of retry attempts.</param> /// <param name="maxDelayInMilliseconds">The maximum delay in miliseconds.</param> /// <typeparam name="T">The Type of the logic return value.</typeparam> /// <returns>The execution context.</returns> - public static async Task<T> Do<T>(Func<Task<T>> task, Func<int, Exception, bool> isRetryable = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue) + public static async Task<T> Do<T>(Func<Task<T>> task, Func<int, Exception, bool> isRetryable = null, Action<int, Exception> onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue) { isRetryable ??= (_, _) => true; + onFailure ??= (_, _) => { }; var exceptions = new List<Exception>(); @@ -74,9 +77,18 @@ namespace slskd { exceptions.Add(ex); - if (!isRetryable(attempts + 1, ex)) + try { - throw new AggregateException(exceptions); + onFailure(attempts + 1, ex); + + if (!isRetryable(attempts + 1, ex)) + { + break; + } + } + catch (Exception retryEx) + { + throw new RetryException($"Failed to retry operation: {retryEx.Message}", ex); } } } |
