2017-12-18 15 views
1

私はNESTエラーがHttpCodeResponseの範囲内であると、私は一般的なポリシーに従ってきたときに再試行します:一般的なポリシータイプのリターン

public Policy<D> CreatePolicy<T, D>(
      PolicyType policyType) 
      where T : Exception where D : IApiCallDetails 
     { 
      switch (policyType) 
      { 
       case PolicyType.WaitAndRetryAsync: 

        var httpStatusCodesWorthRetrying = new List<string>(this.policyConfiguration.HttpStatusCodesToRetrying.Split(',')); 

        return Policy.Handle<T>() 
         .OrResult<D>(r => httpStatusCodesWorthRetrying.Select(int.Parse).ToList().Contains(r.HttpStatusCode.Value)) 
         .WaitAndRetryAsync(
          this.policyConfiguration.NumberOfRequestRetries, 
          retryAttempt => TimeSpan.FromSeconds(this.policyConfiguration.TimeInSecondsBetweenRetries), 
          onRetry: (exception, retryCount, context) => 
          { 
           Log.Error($"[{context.PolicyKey}] Retry {retryCount} due to {exception.Exception.Message}."); 
           throw exception.Exception; 
          })       
         .WithPolicyKey(nameof(PolicyType.WaitAndRetryAsync)); 

       default: 
        throw new ArgumentOutOfRangeException(nameof(policyType), policyType, null); 
      } 

しかし、私はelasticClient通話に適用しようとすると、私が受け取りますエラー:NEST 5.xの

cannot implicity convert System.Threading.Tasks.Task<Nest.ISearchResponse<Model.Product>> to System.Threading.Tasks.Task<Elasticsearch.Net.IApiCallDetails> 


Policy<IApiCallDetails> policyWaintAndRetry = this.policyFactory.CreatePolicy<ElasticsearchClientException, IApiCallDetails>(PolicyType.WaitAndRetryAsync); 


     var searchResponse = await policyWaintAndRetry.ExecuteAsync 
      action:() => 
       this.client.SearchAsync<Product>(s => s 
       .From((request.PageNumber - 1) * request.PageSize) 
       .Size(request.PageSize) 
       .Index(GetIndexName(request.TenantId)) 
       .Query(q => tq), CancellationToken.None), 
      contextData: new Polly.Context("SearchProductSearchAsync")) 
     .ConfigureAwait(false); 
+0

質問を編集して、実際のタイプが何であるか(2回目)、2番目のコード例に現在「var」があることを表示できますか? –

+0

完了、@mountaintraveller –

+0

'var searchResponse'の' var'とは何ですか? –

答えて

1

、私はDIResponseのジェネリックパラメータの制約を持つべきだと思います。 NEST内のすべての応答は、IBodyWithApiCallDetailsからから が継承され、にはHTTP status codeが含まれています。実は私の問題は、実際には、私はそれぞれに1つを作成する必要があり、その後、(結果に対する)policyWrap

第一方針とそれらをマージしている間、私は、例外と結果を処理するための単一のポリシーを作成しようとしていたということでした

0

: (例外の場合)

 public Policy<T> CreatePolicyForResult<T>(
     PolicyType policyType) 
     where T : HttpResponseMessage 
    { 
     switch (policyType) 
     { 
      case PolicyType.WaitAndRetryAsync: 

       var httpStatusCodesWorthRetrying = new List<string>(this.policyConfiguration.HttpStatusCodesToRetrying.Split(',')); 

       return Policy.HandleResult<T>(r => httpStatusCodesWorthRetrying.Select(int.Parse).ToList().Contains((int)r.StatusCode)) 
        .WaitAndRetryAsync(
         this.policyConfiguration.NumberOfRequestRetries, 
         retryAttempt => TimeSpan.FromSeconds(this.policyConfiguration.TimeInSecondsBetweenRetries)) 
        .WithPolicyKey(nameof(PolicyType.WaitAndRetryAsync));      

      default: 
       throw new ArgumentOutOfRangeException(nameof(policyType), policyType, null); 
     } 
    } 

2ポリシー:

public Policy CreatePolicyForException<T>(
      PolicyType policyType) 
      where T : Exception 
     { 
      switch (policyType) 
      { 
       case PolicyType.WaitAndRetryAsync: 

        return Policy.Handle<T>()       
         .WaitAndRetryAsync(
          this.policyConfiguration.NumberOfRequestRetries, 
          retryAttempt => TimeSpan.FromSeconds(this.policyConfiguration.TimeInSecondsBetweenRetries), 
          onRetry: (exception, retryCount, context) => 
          { 
           Log.Error($"[{context.PolicyKey}] Retry {retryCount} due to {exception.Message}."); 
           throw exception; 
          })       
         .WithPolicyKey(nameof(PolicyType.WaitAndRetryAsync)); 

       default: 
        throw new ArgumentOutOfRangeException(nameof(policyType), policyType, null); 
      } 
     } 

使用:

var policyWaintAndRetryForExeption = this.policyFactory.CreatePolicyForException<ElasticsearchClientException>(PolicyType.WaitAndRetryAsync); 

var policyWaintAndRetryForResult = this.policyFactory.CreatePolicyForResult<HttpResponseMessage>(PolicyType.WaitAndRetryAsync); 

PolicyWrap<HttpResponseMessage> policyWrap = policyWaintAndRetryForExeption.WrapAsync(policyWaintAndRetryForResult); 
+0

Exception-handlingとResult-handlingを同じPollyポリシーに組み合わせることは可能です。例外と結果を処理するために2つの別々のポリシーを作成する必要はありません(一般的に)。 (私はこの具体的な例を確認するために、このマシンにNESTをインストールしていません) –

+0

Pollyの以下の単体テストは、同じポリシーで例外と結果の両方を処理できることを示しています。https://github.com/ App-vNext/Polly/blob/64b21c82d79c2b441be566d06f2dfb5bcfddd338/src/Polly.SharedSpecs/Retry/RetryTResultMixedResultExceptionSpecs.cs#L31-L80 –

+1

ここで、例外と結果の2つの別々のポリシーをどのように組み合わせるかを示す回答を掲載しました。これが役に立つと願っています。 –

1

例外と結果を処理するための2つのポリシーを個別に定義する必要はありません。

public Policy<TResult> CreatePolicyForResultAndException<TResult, TException>(PolicyType policyType) 
    where TResult : HttpResponseMessage 
    where TException: Exception 
{ 
    switch (policyType) 
    { 
     case PolicyType.WaitAndRetryAsync: 

      var httpStatusCodesWorthRetrying = new List<string>(this.policyConfiguration.HttpStatusCodesToRetrying.Split(',')); 

      return Policy.HandleResult<TResult>(r => httpStatusCodesWorthRetrying.Select(int.Parse).ToList().Contains((int)r.StatusCode)) 
       .Or<TException>() 
       .WaitAndRetryAsync(
        this.policyConfiguration.NumberOfRequestRetries, 
        retryAttempt => TimeSpan.FromSeconds(this.policyConfiguration.TimeInSecondsBetweenRetries), 
        onRetry: (outcome, retryCount, context) => 
         { 
          Log.Error($"[{context.PolicyKey}] Retry {retryCount} due to {outcome.Result ?? outcome.Exception.Message}."); 
          if (outcome.Exception != null) throw outcome.Exception; // [*] if desired - see note after code sample 
         })  
       .WithPolicyKey(nameof(PolicyType.WaitAndRetryAsync));      

     default: 
      throw new ArgumentOutOfRangeException(nameof(policyType), policyType, null); 
    } 
} 

[*]上記のサンプルコードでこの行はoriginal answerからonRetry内の例外のスローを保持:this answerに二つの別々のポリシーは、このよう組み合わせることができます。しかし、ポリシーが再起された例外を処理しないため、onRetry内で例外を再発行することは珍しいことです。 onRetryの範囲内でスローすると、ポリシーはさらに試行せずに終了します。

+0

私はこのようなものを試しましたが、私はこのメッセージを受け取ります: 'System.Threading.Tasks.Task >'を暗黙的に 'System.Threading'に変換できません。 Tasks.Task ' –

+0

var PolicyForResultAndException = this.policyFactory。CreatePolicyForResultAndException (PolicyType.WaitAndRetryAsync); - 1)* request.PageSize) .Size(リクエスト()=> this.client.SearchAsync (S => S .From((request.PageNumber: –

+0

のvar searchResponseは= PolicyForResultAndException.ExecuteAsync( アクションを待ちます。 PageSize) .INDEX(GetIndexName(request.TenantId)) .Query(Q => TQ)、CancellationToken.None)、 contextData:新Polly.Context( "SearchProductSearchAsync")) .ConfigureAwait(偽); –

関連する問題