public async Task Start() { _current = await _detector.Detect(_token); _tracker.Publish(new ShardState(ShardState.HighWaterMark, _current.CurrentMark) { Action = ShardAction.Started }); _loop = Task.Factory.StartNew(DetectChanges, TaskCreationOptions.LongRunning | TaskCreationOptions.AttachedToParent); _timer.Start(); _logger.LogInformation("Started HighWaterAgent"); }
private async Task DetectChanges() { // TODO -- need to put some retry & exception handling here. try { _current = await _detector.Detect(_token); if (_current.CurrentMark > 0) { _tracker.MarkHighWater(_current.CurrentMark); } } catch (Exception e) { _logger.LogError(e, "Failed while making the initial determination of the high water mark"); } await Task.Delay(_settings.FastPollingTime, _token); while (!_token.IsCancellationRequested) { HighWaterStatistics statistics = null; try { statistics = await _detector.Detect(_token); } catch (Exception e) { _logger.LogError(e, "Failed while trying to detect high water statistics"); await Task.Delay(_settings.SlowPollingTime, _token); continue; } var status = statistics.InterpretStatus(_current); switch (status) { case HighWaterStatus.Changed: await markProgress(statistics, _settings.FastPollingTime); break; case HighWaterStatus.CaughtUp: await markProgress(statistics, _settings.SlowPollingTime); break; case HighWaterStatus.Stale: var safeHarborTime = _current.Timestamp.Add(_settings.StaleSequenceThreshold); var delayTime = safeHarborTime.Subtract(statistics.Timestamp); if (delayTime.TotalSeconds > 0) { await Task.Delay(delayTime, _token); } statistics = await _detector.DetectInSafeZone(safeHarborTime, _token); await markProgress(statistics, _settings.FastPollingTime); break; } } _logger.LogInformation("HighWaterAgent has detected a cancellation and has stopped polling"); }