private static void HttpLog(string identity, string message, LogLevel level, Exception exception = null) { if (Env.EnterpiseService && Env.EnterpiseServiceLog) { var json = JsonConvert.SerializeObject(new { Token = Env.EnterpiseServiceToken, Identity = identity, LogInfo = new { Identity = identity, NodeId = NodeId.Id, Logged = DateTime.UtcNow, Level = level.ToString(), Message = message, Exception = exception?.ToString(), } }); var content = new StringContent(json, Encoding.UTF8, "application/json"); RetryPolicy.ExecuteAndCapture(() => { NetworkCenter.Current.Execute("log", () => { var response = HttpSender.Client.PostAsync(Env.EnterpiseServiceLogUrl, content).Result; response.EnsureSuccessStatusCode(); }); }); } }
private void Connect() { RetryPolicy <bool> policy = Policy.HandleResult <bool>(connected => !connected) .WaitAndRetry(10, r => TimeSpan.FromMilliseconds(100)); ConfigurationOptions configure = Configure(_options.HostAndPort); PolicyResult <bool> result = policy.ExecuteAndCapture(() => { try { if (_redis?.IsConnecting ?? false) { return(_redis.IsConnected); } _redis = ConnectionMultiplexer.Connect(configure); Database = _redis.GetDatabase(); return(_redis.IsConnected); } catch { return(false); } }); if (result.Outcome == OutcomeType.Successful && !result.Result) { throw new RedisConnectionException(ConnectionFailureType.InternalFailure, $"Redis connection is failed on ${_options.HostAndPort}"); } if (result.Outcome == OutcomeType.Failure) { throw result.FinalException.InnerException ?? result.FinalException; } }
public MQQueue GetResilientQueue(string queueName, int openOptions) { var policyResult = _defaultPolicy.ExecuteAndCapture(() => GetQueue(queueName ?? _queueOptions.Name, openOptions)); EnsureSuccess(policyResult); return(policyResult.Result); }
public static void Send(HttpRequestMessage httpRequestMessage) { RetryPolicy.ExecuteAndCapture(() => { NetworkCenter.Current.Execute("status", () => { HttpSender.Client.SendAsync(httpRequestMessage).Result.EnsureSuccessStatusCode(); }); }); }
public void Publish(HdMessage @event) { using (var channel = _connection.CreateChannel()) { channel.QueueDeclare(_exchange, true, false, false, null); var body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(@event)); RetryPolicy.ExecuteAndCapture <SocketException, BrokerUnreachableException>(5, TimeSpan.FromSeconds(3), () => { IBasicProperties basicProperties = channel.CreateBasicProperties(); basicProperties.Persistent = true; channel.BasicPublish(_exchange, @event.Name, true, basicProperties, body); }); } }
public void Connect() { lock (_sync) { RetryPolicy.ExecuteAndCapture <SocketException, BrokerUnreachableException>(5, TimeSpan.FromSeconds(3), () => _connection = _connectionFactory.CreateConnection()); if (IsConnected) { _connection.CallbackException += OnCallbackException; _connection.RecoverySucceeded += OnRecoverySucceeded; _connection.ConnectionShutdown += OnConnectionShutdown; Log.InfoFormat("RabbitMQ persistent connection acquired a connection {0}", _connection.Endpoint.HostName); } else { Log.Fatal("FATAL ERROR: RabbitMQ connections could not be created and opened"); } } }
private IModel GetChannel() { RetryPolicy policy = Policy .Handle <Exception>() .WaitAndRetry(5, i => TimeSpan.FromSeconds(3)); PolicyResult <IModel> result = policy.ExecuteAndCapture <IModel>(() => { if (_connection == null) { _connection = _connectionFactory.CreateConnection(); } IModel channel = _connection.CreateModel(); PrepareModel(channel); return(channel); }); return(result.Result); }
private void InsertDataToClickHouse(ClickHouseConnection clickHouseConn, List <dynamic[]> list) { if (list == null || list.Count == 0) { return; } RetryPolicy.ExecuteAndCapture(() => { using (var command = clickHouseConn.CreateCommand()) { command.CommandText = _insertClickHouseSql; command.Parameters.Add(new ClickHouseParameter { ParameterName = "bulk", Value = list }); command.ExecuteNonQuery(); } }); }
public IConnection GetConnection() { lock (_lock) { if (_connection != null) { return(_connection); } var logger = _honeyCombBuilder.Services.BuildServiceProvider().GetRequiredService <ILogger <ConnectionWithRetryFactory> >(); var policyBuilder = Policy.Handle <Exception>(); RetryPolicy retryPolicy = null; if (_options.ConnectionRetryForever || (!_options.ConnectionRetryForever && _options.ConnectionRetryCount < 0)) { retryPolicy = policyBuilder.WaitAndRetryForever((r, e, ctx) => TimeSpan.FromSeconds(3 * r), OnForeverConnectionException); } else { retryPolicy = policyBuilder.WaitAndRetry(_options.ConnectionRetryCount, r => TimeSpan.FromSeconds(3 * r), OnConnectionException); } var policyResult = retryPolicy.ExecuteAndCapture(() => _connectionFactory.CreateConnection(_options.HostNames.ToList(), _options.ConnectionName)); _connection = policyResult.Result; return(_connection); void OnForeverConnectionException(Exception ex, int r, TimeSpan ts, Context ctx) { logger.LogError(ex, "Retry [ {Retry} / {TotalRetry} ]. Error while connecting to RabbitMq with hostnames {@HostNames}. {ErrorMessage}", r, "∞", _options.HostNames, ex.Message); } void OnConnectionException(Exception ex, TimeSpan ts, int r, Context ctx) { logger.LogError(ex, "Retry [ {Retry} / {TotalRetry} ]. Error while connecting to RabbitMq with hostnames {@HostNames} {ErrorMessage}", r, _options.ConnectionRetryCount, _options.HostNames, ex.Message); } } }
private T ExecuteAndCapture <T>(Func <T> action) { var policyResult = _syncPolicy.ExecuteAndCapture(action); return(policyResult.Result); }
public static PolicyResult <string> RequestCloud(string key) { return(RetryPolicy.ExecuteAndCapture(() => Client.Cloud(key))); }