This commit is contained in:
TidusJar 2016-05-12 23:06:17 -04:00
commit e9937884ad
4 changed files with 101 additions and 25 deletions

View file

@ -39,9 +39,6 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="IApiRequest.cs" /> <Compile Include="IApiRequest.cs" />

View file

@ -62,7 +62,13 @@ namespace PlexRequests.Api
request.AddUrlSegment("imdbid", imdbid); request.AddUrlSegment("imdbid", imdbid);
request.AddUrlSegment("title", title); request.AddUrlSegment("title", title);
var obj = Api.ExecuteJson<JObject>(request, baseUrl); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling AddMovie for CP, Retrying {0}", timespan));
var obj = (JObject)policy.Execute( () => Api.ExecuteJson<JObject>(request, baseUrl));
Log.Trace("CP movie Add result count {0}", obj.Count); Log.Trace("CP movie Add result count {0}", obj.Count);
if (obj.Count > 0) if (obj.Count > 0)
@ -99,8 +105,13 @@ namespace PlexRequests.Api
}; };
request.AddUrlSegment("apikey", apiKey); request.AddUrlSegment("apikey", apiKey);
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 CP, Retrying {0}", timespan));
return Api.Execute<CouchPotatoStatus>(request,url); return (CouchPotatoStatus)policy.Execute( () => Api.Execute<CouchPotatoStatus>(request,url));
} }
public CouchPotatoProfiles GetProfiles(Uri url, string apiKey) public CouchPotatoProfiles GetProfiles(Uri url, string apiKey)
@ -114,7 +125,13 @@ namespace PlexRequests.Api
request.AddUrlSegment("apikey", apiKey); request.AddUrlSegment("apikey", apiKey);
return Api.Execute<CouchPotatoProfiles>(request, url); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetProfiles for CP, Retrying {0}", timespan));
return (CouchPotatoProfiles)policy.Execute( () => Api.Execute<CouchPotatoProfiles>(request,url));
} }
public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status) public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status)
@ -128,7 +145,13 @@ namespace PlexRequests.Api
request.AddUrlSegment("status", string.Join(",", status)); request.AddUrlSegment("status", string.Join(",", status));
try try
{ {
return Api.Execute<CouchPotatoMovies>(request, baseUrl); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetMovies for CP, Retrying {0}", timespan));
return (CouchPotatoMovies)policy.Execute( () => Api.Execute<CouchPotatoMovies>(request,baseUrl));
} }
catch (Exception e) // Request error is already logged in the ApiRequest class catch (Exception e) // Request error is already logged in the ApiRequest class
{ {

View file

@ -24,6 +24,7 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/ // ************************************************************************/
using Polly;
#endregion #endregion
@ -82,9 +83,14 @@ namespace PlexRequests.Api
request.AddQueryParameter("initial", quality); request.AddQueryParameter("initial", quality);
} }
Log.Trace("Entering `Execute<SickRageTvAdd>`"); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
var obj = Api.Execute<SickRageTvAdd>(request, baseUrl); TimeSpan.FromSeconds (2),
Log.Trace("Exiting `Execute<SickRageTvAdd>`"); TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling AddSeries for SR, Retrying {0}", timespan));
var obj = policy.Execute( () => Api.Execute<SickRageTvAdd>(request, baseUrl));
Log.Trace("obj Result:"); Log.Trace("obj Result:");
Log.Trace(obj.DumpJson()); Log.Trace(obj.DumpJson());
@ -95,7 +101,6 @@ namespace PlexRequests.Api
var seasonIncrement = 0; var seasonIncrement = 0;
var seasonList = new SickRageSeasonList(); var seasonList = new SickRageSeasonList();
Log.Trace("while (seasonIncrement < seasonCount) where seasonCount = {0}", seasonCount);
try try
{ {
while (seasonIncrement < seasonCount) while (seasonIncrement < seasonCount)
@ -132,7 +137,8 @@ namespace PlexRequests.Api
foreach (var s in seasons) foreach (var s in seasons)
{ {
Log.Trace("Adding season {0}", s); Log.Trace("Adding season {0}", s);
var result = await AddSeason(tvdbId, s, apiKey, baseUrl);
var result = await AddSeason(tvdbId, s, apiKey, baseUrl);
Log.Trace("SickRage adding season results: "); Log.Trace("SickRage adding season results: ");
Log.Trace(result.DumpJson()); Log.Trace(result.DumpJson());
} }
@ -157,8 +163,17 @@ namespace PlexRequests.Api
Method = Method.GET Method = Method.GET
}; };
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling Ping for SR, Retrying {0}", timespan));
request.AddUrlSegment("apiKey", apiKey); request.AddUrlSegment("apiKey", apiKey);
var obj = Api.ExecuteJson<SickRagePing>(request, baseUrl); var obj = policy.Execute( () => Api.ExecuteJson<SickRagePing>(request, baseUrl));
return obj; return obj;
} }
@ -176,9 +191,14 @@ namespace PlexRequests.Api
try try
{ {
Log.Trace("Entering `ExecuteJson<SickRageSeasonList>`"); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
var obj = Api.ExecuteJson<SickRageSeasonList>(request, baseUrl); TimeSpan.FromSeconds (2),
Log.Trace("Exited `ExecuteJson<SickRageSeasonList>`"); TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}", timespan));
var obj = policy.Execute( () => Api.ExecuteJson<SickRageSeasonList>(request, baseUrl));
return obj; return obj;
} }
catch (Exception e) catch (Exception e)
@ -203,10 +223,16 @@ namespace PlexRequests.Api
await Task.Run(() => Thread.Sleep(2000)); await Task.Run(() => Thread.Sleep(2000));
return await Task.Run(() => return await Task.Run(() =>
{ {
Log.Trace("Entering `Execute<SickRageTvAdd>` in a new `Task<T>`");
var result = Api.Execute<SickRageTvAdd>(request, baseUrl);
Log.Trace("Exiting `Execute<SickRageTvAdd>` and yeilding `Task<T>` result"); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling AddSeason for SR, Retrying {0}", timespan));
var result = policy.Execute(() => Api.Execute<SickRageTvAdd>(request, baseUrl));
return result; return result;
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
@ -225,7 +251,15 @@ namespace PlexRequests.Api
{ {
try try
{ {
return Api.Execute<SickrageShows>(request, baseUrl); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetShows for SR, Retrying {0}", timespan));
return policy.Execute(() => Api.Execute<SickrageShows>(request, baseUrl));
} }
catch (ApiRequestException) catch (ApiRequestException)
{ {

View file

@ -56,8 +56,13 @@ namespace PlexRequests.Api
var request = new RestRequest { Resource = "/api/profile", Method = Method.GET }; var request = new RestRequest { Resource = "/api/profile", Method = Method.GET };
request.AddHeader("X-Api-Key", apiKey); request.AddHeader("X-Api-Key", apiKey);
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetProfiles for Sonarr, Retrying {0}", timespan));
var obj = Api.ExecuteJson<List<SonarrProfile>>(request, baseUrl); var obj = policy.Execute(() => Api.ExecuteJson<List<SonarrProfile>>(request, baseUrl));
return obj; return obj;
} }
@ -102,7 +107,13 @@ namespace PlexRequests.Api
SonarrAddSeries result; SonarrAddSeries result;
try try
{ {
result = Api.ExecuteJson<SonarrAddSeries>(request, baseUrl); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan));
result = policy.Execute(() => Api.ExecuteJson<SonarrAddSeries>(request, baseUrl));
} }
catch (JsonSerializationException jse) catch (JsonSerializationException jse)
{ {
@ -121,7 +132,13 @@ namespace PlexRequests.Api
var request = new RestRequest { Resource = "/api/system/status", Method = Method.GET }; var request = new RestRequest { Resource = "/api/system/status", Method = Method.GET };
request.AddHeader("X-Api-Key", apiKey); request.AddHeader("X-Api-Key", apiKey);
var obj = Api.ExecuteJson<SystemStatus>(request, baseUrl); var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}, (exception, timespan) => Log.Error (exception, "Exception when calling SystemStatus for Sonarr, Retrying {0}", timespan));
var obj = policy.Execute(() => Api.ExecuteJson<SystemStatus>(request, baseUrl));
return obj; return obj;
} }
@ -132,9 +149,14 @@ namespace PlexRequests.Api
request.AddHeader("X-Api-Key", apiKey); request.AddHeader("X-Api-Key", apiKey);
try try
{ {
var policy = RetryHandler.RetryAndWaitPolicy (new TimeSpan[] {
TimeSpan.FromSeconds (5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
}, (exception, timespan) => Log.Error (exception, "Exception when calling GetSeries for Sonarr, Retrying {0}", timespan));
return Api.Execute<List<Series>>(request, baseUrl); return policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));
} }
catch (ApiRequestException) catch (ApiRequestException)
{ {
Log.Error("There has been an API exception when getting the Sonarr Series"); Log.Error("There has been an API exception when getting the Sonarr Series");