using Microsoft.Extensions.Logging; using System; using System.Net.NetworkInformation; using System.Threading.Tasks; namespace LANCommander.SDK.Helpers { internal static class RetryHelper { internal static readonly ILogger Logger; internal static T RetryOnException(int maxAttempts, TimeSpan delay, T @default, Func action) { int attempts = 0; do { try { Logger?.LogTrace($"Attempt #{attempts + 1}/{maxAttempts}..."); attempts++; return action(); } catch (Exception ex) { Logger?.LogError(ex, $"Attempt failed!"); if (attempts >= maxAttempts) return @default; Task.Delay(delay).Wait(); } } while (true); } internal static void RetryOnException(int maxAttempts, TimeSpan delay, Action action) { int attempts = 0; do { try { Logger?.LogTrace($"Attempt #{attempts + 1}/{maxAttempts}..."); attempts++; action(); } catch (Exception ex) { Logger?.LogError(ex, $"Attempt failed!"); if (attempts >= maxAttempts) return; Task.Delay(delay).Wait(); } } while (true); } internal static async Task RetryOnExceptionAsync(int maxAttempts, TimeSpan delay, Func action) { int attempts = 0; do { try { Logger?.LogTrace($"Attempt #{attempts + 1}/{maxAttempts}..."); attempts++; await action(); } catch (Exception ex) { Logger?.LogError(ex, $"Attempt failed!"); if (attempts >= maxAttempts) return; Task.Delay(delay).Wait(); } } while (true); } internal static async Task RetryOnExceptionAsync(int maxAttempts, TimeSpan delay, T @default, Func> action) { int attempts = 0; do { try { Logger?.LogTrace($"Attempt #{attempts + 1}/{maxAttempts}..."); attempts++; return await action(); } catch (Exception ex) { Logger?.LogError(ex, $"Attempt failed!"); if (attempts >= maxAttempts) return @default; Task.Delay(delay).Wait(); } } while (true); } } }