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));
                var watcherCheckResult = await watcher.ExecuteAsync();
                var completedAt = _configuration.DateTimeProvider();
                wardenCheckResult = WardenCheckResult.Create(watcherCheckResult, startedAt, completedAt);
                var watcherResults = await ExecuteWatcherCheckAsync(watcher, watcherCheckResult, wardenCheckResult,
                    watcherConfiguration);
                results.AddRange(watcherResults);
            }
            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);
            }

            return results;
        }
Beispiel #2
0
        /// <summary>
        /// Run a single iteration (cycle) that will execute all of the watchers and theirs hooks.
        /// </summary>
        /// <param name="wardenName">Name of the Warden that will execute the iteration.</param>
        /// <param name="ordinal">Number (ordinal) of executed iteration</param>
        /// <returns>Single iteration containing its ordinal and results of all executed watcher checks</returns>
        public async Task <IWardenIteration> ExecuteAsync(string wardenName, long ordinal)
        {
            var iterationStartedAt = _configuration.DateTimeProvider();
            var results            = new List <WatcherExecutionResult>();
            var iterationTasks     = _configuration.Watchers.Select(async watcherConfiguration =>
            {
                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);
                    if (watcherCheckResult.IsValid)
                    {
                        var result = wardenCheckResult;
                        results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Success,
                                                               GetPreviousWatcherState(watcher), result));
                        await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Success,
                                                                                   () => InvokeOnFirstSuccessHooksAsync(watcherConfiguration, result),
                                                                                   executeIfLatestStateIsNotSet: false);
                        await InvokeOnSuccessHooksAsync(watcherConfiguration, wardenCheckResult);
                    }
                    else
                    {
                        var result = wardenCheckResult;
                        results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Failure,
                                                               GetPreviousWatcherState(watcher), result));
                        await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Failure,
                                                                                   () => InvokeOnFirstFailureHooksAsync(watcherConfiguration, result));
                        await InvokeOnFailureHooksAsync(watcherConfiguration, wardenCheckResult);
                    }
                }
                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);
                }
            });

            await Task.WhenAll(iterationTasks);

            await ExecuteAggregatedHooksAsync(results);

            var iterationCompletedAt = _configuration.DateTimeProvider();
            var iteration            = WardenIteration.Create(wardenName, ordinal, results.Select(x => x.WardenCheckResult),
                                                              iterationStartedAt, iterationCompletedAt);

            return(iteration);
        }