private async Task <IList <WatcherExecutionResult> > TryExecuteWatcherCheckAndHooksAsync( WatcherConfiguration watcherConfiguration) { var startedAt = _configuration.DateTimeProvider(); var watcher = watcherConfiguration.Watcher; IWardenCheckResult wardenCheckResult = null; var results = new List <WatcherExecutionResult>(); try { await InvokeOnStartHooksAsync(watcherConfiguration, WatcherCheck.Create(watcher)); _logger.Info($"Executing Watcher: {watcher.Name}."); var watcherCheckResult = await watcher.ExecuteAsync(); _logger.Info($"Completed executing Watcher: {watcher.Name}. " + $"Is valid: {watcherCheckResult.IsValid}. " + $"Description: {watcherCheckResult.Description}"); var completedAt = _configuration.DateTimeProvider(); wardenCheckResult = WardenCheckResult.Create(watcherCheckResult, startedAt, completedAt); var watcherResults = await ExecuteWatcherCheckAsync(watcher, watcherCheckResult, wardenCheckResult, watcherConfiguration); results.AddRange(watcherResults); } catch (Exception exception) { _logger.Error($"There was an error while executing Watcher: {watcher.Name}.", exception); var completedAt = _configuration.DateTimeProvider(); wardenCheckResult = WardenCheckResult.Create(WatcherCheckResult.Create(watcher, false, exception.Message), startedAt, completedAt, exception); results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Error, GetPreviousWatcherState(watcher), wardenCheckResult, exception)); await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Error, () => InvokeOnFirstErrorHooksAsync(watcherConfiguration, exception)); var wardenException = new WardenException("There was an error while executing Warden " + $"caused by watcher: '{watcher.Name}'.", exception); await InvokeOnErrorHooksAsync(watcherConfiguration, wardenException); } finally { await InvokeOnCompletedHooksAsync(watcherConfiguration, wardenCheckResult); } return(results); }
private async Task TryExecuteWatcherCheckAndHooksAsync(WatcherConfiguration watcherConfiguration, IList <WatcherExecutionResult> results) { var startedAt = _configuration.DateTimeProvider(); var watcher = watcherConfiguration.Watcher; IWardenCheckResult wardenCheckResult = null; try { await InvokeOnStartHooksAsync(watcherConfiguration, WatcherCheck.Create(watcher)); var watcherCheckResult = await watcher.ExecuteAsync(); var completedAt = _configuration.DateTimeProvider(); wardenCheckResult = WardenCheckResult.Create(watcherCheckResult, startedAt, completedAt); await ExecuteWatcherCheckAsync(watcher, watcherCheckResult, wardenCheckResult, watcherConfiguration, results); } catch (Exception exception) { var completedAt = _configuration.DateTimeProvider(); wardenCheckResult = WardenCheckResult.Create(WatcherCheckResult.Create(watcher, false), startedAt, completedAt, exception); results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Error, GetPreviousWatcherState(watcher), wardenCheckResult, exception)); await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Error, () => InvokeOnFirstErrorHooksAsync(watcherConfiguration, exception)); var wardenException = new WardenException("There was an error while executing Warden " + $"caused by watcher: '{watcher.Name}'.", exception); await InvokeOnErrorHooksAsync(watcherConfiguration, wardenException); } finally { await InvokeOnCompletedHooksAsync(watcherConfiguration, wardenCheckResult); } }