private async Task ServiceStatusPingAsync(TimerAwaitable timer) { using (timer) { timer.Start(); while (await timer) { try { // Check if last send time is longer than default keep-alive ticks and then send ping if (Stopwatch.GetTimestamp() - Interlocked.Read(ref _lastSendTimestamp) > DefaultGetServiceStatusTicks) { await WriteAsync(RuntimeServicePingMessage.GetStatusPingMessage(true)); Interlocked.Exchange(ref _lastSendTimestamp, Stopwatch.GetTimestamp()); Log.SentServiceStatusPing(Logger); } } catch (Exception e) { Log.FailedSendingServiceStatusPing(Logger, e); } } } }
private TimerAwaitable Init() { Log.StartingPingTimer(_logger, _pingName, _intervalTime); _lastSendTimestamp = Stopwatch.GetTimestamp(); var timer = new TimerAwaitable(_dueTime, _intervalTime); return(timer); }
public bool Start() { if (Interlocked.Increment(ref _counter) == 1) { _timer = Init(); _timer.Start(); _ = PingAsync(_timer); return(true); } return(false); }
public CustomizedPingTimer(ILogger logger, string pingName, Func <Task> writePing, TimeSpan dueTime, TimeSpan intervalTime) { _logger = logger; _pingName = pingName; _writePing = writePing; _dueTime = dueTime; _intervalTime = intervalTime; _defaultPingTicks = intervalTime.Seconds * Stopwatch.Frequency; _timer = Init(); }
private TimerAwaitable StartServiceStatusPingTimer() { Log.StartingServiceStatusPingTimer(Logger, DefaultGetServiceStatusInterval); _lastSendTimestamp = Stopwatch.GetTimestamp(); var timer = new TimerAwaitable(DefaultGetServiceStatusInterval, DefaultGetServiceStatusInterval); _ = ServiceStatusPingAsync(timer); return(timer); }
public RestHealthCheckService(RestClientFactory clientFactory, IServiceEndpointManager serviceEndpointManager, ILogger <RestHealthCheckService> logger, string hubName, IOptions <HealthCheckOption> options) { var checkOptions = options.Value; _enable = serviceEndpointManager.Endpoints.Count > 1 || checkOptions.EnabledForSingleEndpoint; if (_enable) { _clientFactory = clientFactory; _serviceEndpointManager = serviceEndpointManager; _logger = logger; _hubName = hubName; _checkInterval = checkOptions.CheckInterval.GetValueOrDefault(_checkInterval); _retryInterval = checkOptions.RetryInterval.GetValueOrDefault(_retryInterval); _timer = new TimerAwaitable(_checkInterval, _checkInterval); } }
private async Task PingAsync(TimerAwaitable timer) { while (await timer) { try { // Check if last send time is longer than default keep-alive ticks and then send ping if (Stopwatch.GetTimestamp() - Interlocked.Read(ref _lastSendTimestamp) > _defaultPingTicks) { await _writePing.Invoke(); Interlocked.Exchange(ref _lastSendTimestamp, Stopwatch.GetTimestamp()); Log.SentPing(_logger, _pingName); } } catch (Exception e) { Log.FailedSendingPing(_logger, _pingName, e); } } }
public ObjectWithTimerAwaitable(TaskCompletionSource tcs) { _tcs = tcs; _timer = new TimerAwaitable(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(1)); _timer.Start(); }
public WeakServiceConnectionContainer(IServiceConnectionFactory serviceConnectionFactory, int fixedConnectionCount, HubServiceEndpoint endpoint, ILogger logger) : base(serviceConnectionFactory, fixedConnectionCount, endpoint, logger: logger) { _timer = StartServiceStatusPingTimer(); }