private static async void SendReadToElastic(IExecutionSegment transaction, ElasticDocument message) { var span = transaction.StartSpan("Send message to Elastic", "Elastic"); try { var indexSpan = span.StartSpan("Index to Elastic", ApiConstants.TypeDb); try { var res = await Policy .Handle <ElasticsearchClientException>() .WaitAndRetryAsync(5, i => TimeSpan.FromSeconds(3)) .ExecuteAsync(() => ElasticClient.IndexAsync(message, idx => idx.Index(Environment.GetEnvironmentVariable("Elastic_Index_Name")))); } catch (Exception e) { ElasticClient.Index(message, idx => idx.Index($"proof-reads-{Today}")); indexSpan.CaptureException(e, "Index to Elastic"); } finally { indexSpan.End(); } } catch (Exception e) { span.CaptureException(e, "SendDataToElastic"); } finally { span.End(); } }
protected PolicyResult RetryAction(Action action, int timeoutInSeconds = 30, int retryIntervalInSeconds = 1) { Guard.NotNull(action, nameof(action), "Requires disposing function to be retried"); Guard.NotLessThan(timeoutInSeconds, 0, nameof(timeoutInSeconds), "Requires a timeout (in sec) greater than zero"); Guard.NotLessThan(retryIntervalInSeconds, 0, nameof(retryIntervalInSeconds), "Requires a retry interval (in sec) greater than zero"); return(Policy.Timeout(TimeSpan.FromSeconds(timeoutInSeconds)) .Wrap(Policy.Handle <Exception>() .WaitAndRetryForever(index => TimeSpan.FromSeconds(retryIntervalInSeconds))) .ExecuteAndCapture(action)); }
public virtual async Task <BulkResponse> BulkAsync(IBulkRequest request, string callerName = "") { var timer = Stopwatch.StartNew(); var policy = Policy .Handle <TooManyRequestsException>() .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt) * 10)); var result = await policy.Execute(() => BulkData(request)); SendLog(null, null, timer.ElapsedMilliseconds, $"Elasticsearch.BulkAsync.{callerName}"); return(result); }
public ISearchResponse <T> Search <T>(Func <SearchDescriptor <T>, ISearchRequest> selector, [CallerMemberName] string callerName = "") where T : class { var timer = Stopwatch.StartNew(); var policy = Policy .Handle <FailedToPingSpecifiedNodeException>() .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt) * 10)); var result = policy.Execute(() => SearchData(selector)); ValidateResponse(result); SendLog(result.ApiCall, result.Took, timer.ElapsedMilliseconds, $"Elasticsearch.Search.{callerName}"); return(result); }