Added a retry handler into the solution. We can now retry failed api requests.

this should resolve #202 and start on #208
pull/226/head
TidusJar 9 years ago
parent f9205fc027
commit adeeb7824d

@ -23,6 +23,9 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
using Polly;
#endregion
using System;
@ -67,7 +70,14 @@ namespace PlexRequests.Api
request.AddJsonBody(userModel);
var api = new ApiRequest();
return api.Execute<PlexAuthentication>(request, new Uri("https://plex.tv/users/sign_in.json"));
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling SignIn for Plex, Retrying {0}", timespan));
return (PlexAuthentication)policy.Execute(() => api.Execute<PlexAuthentication>(request, new Uri("https://plex.tv/users/sign_in.json")));
}
public PlexFriends GetUsers(string authToken)
@ -80,7 +90,13 @@ namespace PlexRequests.Api
AddHeaders(ref request, authToken);
var api = new ApiRequest();
var users = api.ExecuteXml<PlexFriends>(request, new Uri("https://plex.tv/pms/friends/all"));
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetUsers for Plex, Retrying {0}", timespan));
var users = (PlexFriends)policy.Execute(() =>api.ExecuteXml<PlexFriends>(request, new Uri("https://plex.tv/pms/friends/all")));
return users;
}
@ -104,7 +120,13 @@ namespace PlexRequests.Api
AddHeaders(ref request, authToken);
var api = new ApiRequest();
var search = api.ExecuteXml<PlexSearch>(request, plexFullHost);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling SearchContent for Plex, Retrying {0}", timespan));
var search = (PlexSearch)policy.Execute(() => api.ExecuteXml<PlexSearch>(request, plexFullHost));
return search;
}
@ -117,9 +139,14 @@ namespace PlexRequests.Api
};
AddHeaders(ref request, authToken);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetStatus for Plex, Retrying {0}", timespan));
var api = new ApiRequest();
var users = api.ExecuteXml<PlexStatus>(request, uri);
var users = (PlexStatus)policy.Execute(() => api.ExecuteXml<PlexStatus>(request, uri));
return users;
}
@ -133,8 +160,15 @@ namespace PlexRequests.Api
AddHeaders(ref request, authToken);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetAccount for Plex, Retrying: {0}", timespan));
var api = new ApiRequest();
var account = api.ExecuteXml<PlexAccount>(request, new Uri("https://plex.tv/users/account"));
var account = (PlexAccount)policy.Execute(() => api.ExecuteXml<PlexAccount>(request, new Uri("https://plex.tv/users/account")));
return account;
}
@ -152,8 +186,13 @@ namespace PlexRequests.Api
var api = new ApiRequest();
try
{
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrarySections for Plex, Retrying {0}", timespan));
return api.ExecuteXml<PlexLibraries>(request, plexFullHost);
return (PlexLibraries)policy.Execute(() => api.ExecuteXml<PlexLibraries>(request, plexFullHost));
}
catch (ApiRequestException)
{
@ -176,8 +215,13 @@ namespace PlexRequests.Api
var api = new ApiRequest();
try
{
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetLibrary for Plex, Retrying {0}", timespan));
return api.ExecuteXml<PlexSearch>(request, plexFullHost);
return (PlexSearch)policy.Execute(() => api.ExecuteXml<PlexSearch>(request, plexFullHost));
}
catch (ApiRequestException)
{

@ -57,6 +57,9 @@
<Reference Include="TMDbLib, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\TMDbLib.0.9.0.0-alpha\lib\net45\TMDbLib.dll</HintPath>
</Reference>
<Reference Include="Polly">
<HintPath>..\packages\Polly-Signed.4.2.0\lib\net45\Polly.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ApiRequest.cs" />
@ -81,6 +84,7 @@
<Compile Include="TheTvDbApi.cs" />
<Compile Include="TvMazeApi.cs" />
<Compile Include="TvMazeBase.cs" />
<Compile Include="RetryHandler.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />

@ -0,0 +1,34 @@
using System;
using Polly.Retry;
using Polly;
namespace PlexRequests.Api
{
public static class RetryHandler
{
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan, Action action)
{
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan, (exception, timeSpan) => action());
return policy;
}
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan)
{
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan);
return policy;
}
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] TimeSpan, Action<Exception, TimeSpan> action)
{
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(TimeSpan, (exception, timeSpan) => action(exception, timeSpan));
return policy;
}
}
}

@ -4,6 +4,7 @@
<package id="Nancy" version="1.4.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="NLog" version="4.2.3" targetFramework="net452" />
<package id="Polly-Signed" version="4.2.0" targetFramework="net45" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" requireReinstallation="True" />
<package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net452" />
</packages>

@ -38,7 +38,6 @@ namespace PlexRequests.UI.Validators
RuleFor(request => request.Ip).NotEmpty().WithMessage("You must specify a IP/Host name.");
RuleFor(request => request.Port).NotEmpty().WithMessage("You must specify a Port.");
RuleFor(request => request.QualityProfile).NotEmpty().WithMessage("You must specify a Quality Profile.");
RuleFor(request => request.RootPath).NotEmpty().WithMessage("You must specify a Root Path.");
}
}
}
Loading…
Cancel
Save