IAPICommunicationModeManager
using Polly;
using Polly.Retry;
using Relativity.DataExchange.Logger;
using Relativity.DataTransfer.Legacy.SDK.ImportExport.V1.Models;
using Relativity.Kepler.Exceptions;
using Relativity.Logging;
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Relativity.DataExchange.Service.WebApiVsKeplerSwitch
{
public class IAPICommunicationModeManager : IIAPICommunicationModeManager
{
private const int DefaultRetryCount = 1;
private const int DefaultSleepDurationTimeInSeconds = 1;
private const string GetRelativityVersionUrl = "/Relativity.Rest/api/Relativity.Services.InstanceDetails.IInstanceDetailsModule/InstanceDetailsService/GetRelativityVersionAsync";
private readonly IKeplerProxy keplerProxy;
private readonly IAppSettings settings;
private readonly ILog logger;
private readonly Func<string> correlationIdFunc;
public IAPICommunicationModeManager(IKeplerProxy keplerProxy, Func<string> correlationIdFunc, IAppSettings settings, ILog logger)
{
this.keplerProxy = keplerProxy;
this.correlationIdFunc = correlationIdFunc;
this.settings = settings;
this.logger = (((object)logger) ?? ((object)RelativityLogger.Instance));
}
public IAPICommunicationModeManager(IKeplerProxy keplerProxy, Func<string> correlationIdFunc)
: this(keplerProxy, correlationIdFunc, AppSettings.Instance, RelativityLogger.Instance)
{
}
[AsyncStateMachine(typeof(<ReadImportApiCommunicationMode>d__9))]
public Task<IAPICommunicationMode> ReadImportApiCommunicationMode()
{
<ReadImportApiCommunicationMode>d__9 stateMachine = default(<ReadImportApiCommunicationMode>d__9);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<IAPICommunicationMode>.Create();
stateMachine.<>4__this = this;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
private Task<IAPICommunicationMode> GetIAPICommunicationMode(RetryPolicy retryPolicy)
{
return retryPolicy.ExecuteAsync<IAPICommunicationMode>((Func<Task<IAPICommunicationMode>>)(() => keplerProxy.ExecuteAsync(delegate(IServiceProxyFactory serviceProxyFactory) {
<<GetIAPICommunicationMode>b__10_1>d stateMachine = default(<<GetIAPICommunicationMode>b__10_1>d);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<IAPICommunicationMode>.Create();
stateMachine.<>4__this = this;
stateMachine.serviceProxyFactory = serviceProxyFactory;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
})));
}
private RetryPolicy GetRetryPolicy()
{
int maxRetryCount = settings?.ReadCommunicationModeErrorNumberOfRetries ?? 1;
TimeSpan sleepDurationTime = TimeSpan.FromSeconds((double)(settings?.ReadCommunicationModeErrorWaitTimeInSeconds ?? 1));
return RetrySyntaxAsync.WaitAndRetryAsync(Policy.Handle<ServiceNotFoundException>(), maxRetryCount, (Func<int, TimeSpan>)((int i) => sleepDurationTime), (Action<Exception, TimeSpan, int, Context>)delegate(Exception exception, TimeSpan timeSpan, int retryCount, Context context) {
ILog obj = logger;
if (obj != null)
obj.LogWarning("Service connection failed. Retry policy triggered... Attempt #{retryCount} out of {maxRetryCount}. Wait time between retries: {waitTime}", new object[3] {
retryCount,
maxRetryCount,
sleepDurationTime
});
});
}
}
}