public async Task <ImageDetectorState> CheckStateAsync() { // Don't retry anything if our detector is broken. if (State == ImageDetectorState.Broken) { return(State); } // Don't do anything unless we're past our imposed delay if (DateTime.Now < mStateLastChecked + mStateCheckDelay) { return(State); } Log($"Checking state for {this.GetType()}"); mStateLastChecked = DateTime.Now; try { mState = await CheckStateAsyncCore(); } catch (Exception ex) { Log($"Exception occured while detecting state", LogLevel.Error, ex); mState = ImageDetectorState.Broken; } if (State == ImageDetectorState.Good) { mStateCheckDelay = TimeSpan.Zero; } else { var currentDelay = Math.Max(mStateCheckDelay.TotalSeconds, InitialDelay.TotalSeconds); var newDelay = Math.Min(currentDelay * 2, MaxStateCheckDelay.TotalSeconds); mStateCheckDelay = TimeSpan.FromSeconds(newDelay); } if (State == ImageDetectorState.Good) { Log($"State of {this.GetType()} is good"); } else { Log($"Bad state for {this.GetType()}: {State}", LogLevel.Warning); } return(State); }
/// <summary> /// Create the exception /// </summary> /// <param name="state">The state the detector has gone into</param> public ImageDetectionException(ImageDetectorState state) : base($"Detector in bad state ({state})") { mState = state; }