private void HandleNewEndpoint(MonitorableEndpoint endpoint) { if (_tasks.TryAdd(endpoint, CreateTaskFor(endpoint))) { _onNewTask.Set(); } }
private async Task MonitorEndpointAsync(MonitorableEndpoint endpoint, CancellationToken cancellationToken) { int errorCounter = 0; await _timeCoordinator.Delay(GetRandomizedDelay(), cancellationToken); while (!cancellationToken.IsCancellationRequested && !endpoint.IsDisposed) { try { var delay = _timeCoordinator.Delay(_settings.HealthCheckInterval, cancellationToken); await Task.WhenAll(endpoint.CheckHealth(_sampler, cancellationToken), delay); errorCounter = 0; } catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { } catch (AggregateException e) when (cancellationToken.IsCancellationRequested && e.Flatten().InnerExceptions.All(ex => ex is OperationCanceledException)) { } catch (Exception e) { errorCounter = Math.Min(errorCounter + 1, MaxErrorDelayInSeconds); Logger.ErrorFormat("Monitoring error ({0} occurrence): {1}", errorCounter, e.ToString()); await _timeCoordinator.Delay(TimeSpan.FromSeconds(errorCounter), cancellationToken); } } }
private async Task MonitorEndpointAsync(MonitorableEndpoint endpoint, CancellationToken cancellationToken) { int errorCounter = 0; await _timeCoordinator.Delay(GetRandomizedDelay(), cancellationToken); while (!cancellationToken.IsCancellationRequested && !endpoint.IsDisposed) { try { var delay = _timeCoordinator.Delay(_settings.HealthCheckInterval, cancellationToken); await Task.WhenAll(endpoint.CheckHealth(_sampler, cancellationToken), delay); errorCounter = 0; } catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested) { } catch (AggregateException e) when(cancellationToken.IsCancellationRequested && e.Flatten().InnerExceptions.All(ex => ex is OperationCanceledException)) { } catch (Exception e) { errorCounter = Math.Min(errorCounter + 1, MaxErrorDelayInSeconds); Logger.ErrorFormat("Monitoring error ({0} occurrence): {1}", errorCounter, e.ToString()); await _timeCoordinator.Delay(TimeSpan.FromSeconds(errorCounter), cancellationToken); } } }
private async Task <MonitorableEndpoint> CreateTaskFor(MonitorableEndpoint endpoint) { await _timeCoordinator.Delay(GetRandomizedDelay(), _cancellation.Token); while (!_cancellation.IsCancellationRequested && !endpoint.IsDisposed) { var delay = _timeCoordinator.Delay(_settings.HealthCheckInterval, _cancellation.Token); await Task.WhenAll(endpoint.CheckHealth(_sampler, _cancellation.Token), delay); } return(endpoint); }
private void HandleNewEndpoint(MonitorableEndpoint endpoint) { _executor.TryRegisterTaskFor(endpoint, MonitorEndpointAsync); }