Skip to content

Commit

Permalink
Handle and deal with TMDbLib.Objects.Exceptions.RequestLimitExceededE…
Browse files Browse the repository at this point in the history
…xception
  • Loading branch information
SirSparkles committed Jul 26, 2023
1 parent 78a0322 commit 3f4e08d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion TVRename/Sources/TMDB/LocalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Humanizer;
using TMDbLib.Client;
using TMDbLib.Objects.Exceptions;
using TMDbLib.Objects.Find;
Expand Down Expand Up @@ -584,7 +585,7 @@ private T HandleWebErrorsFor<T>(Func<T> webCall, string errorMessage)
{
try
{
return webCall();
return webCall.WithRetry(3,2.Seconds(),ex=>ex is RequestLimitExceededException ,errorMessage);
}
catch (System.IO.IOException ioex)
{
Expand Down
22 changes: 22 additions & 0 deletions TVRename/Utility/Helper/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using System;
using System.Linq;
using System.Reflection;
using Polly;
using NLog;

#pragma warning disable CS0162

// Helpful functions and classes
Expand All @@ -17,6 +20,8 @@ namespace TVRename;

public static class Helpers
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

#region DebugingInfo

/// <summary>
Expand Down Expand Up @@ -210,4 +215,21 @@ public static int ToInt(this string text, int min, int def, int max)
}

#endregion

public static T WithRetry<T>(this Func<T> operation, int retryTimes, TimeSpan delay,
Func<Exception, bool> retryableException, string message)
{
Policy retryPolicy = Policy
.Handle(retryableException)
.WaitAndRetry(
retryTimes,
_ => delay,
(exception, timeSpan, retryCount, _) =>
{
Logger.Warn(exception,
$"Retry {retryCount}/{retryTimes}, waiting {timeSpan} to {message}.");
});

return retryPolicy.Execute(operation);
}
}

0 comments on commit 3f4e08d

Please sign in to comment.