Beispiel #1
0
        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);
        }
Beispiel #2
0
        private async Task TryExecuteIterationsAsync()
        {
            var iterationProcessor = _configuration.IterationProcessorProvider();

            _currentIterationCancellationTokenSource = new CancellationTokenSource();
            while (CanExecuteIteration(_iterationOrdinal))
            {
                try
                {
                    _currentIterationTask = Task.Run(() => ExecuteIterationAsync(iterationProcessor),
                                                     _currentIterationCancellationTokenSource.Token);
                    await _currentIterationTask;
                    var   canExecuteNextIteration = CanExecuteIteration(_iterationOrdinal + 1);
                    if (!canExecuteNextIteration)
                    {
                        break;
                    }

                    _iterationOrdinal++;
                }
                catch (Exception exception)
                {
                    try
                    {
                        _logger.Error("There was an error while executing Warden iteration " +
                                      $"{_iterationOrdinal}.", exception);
                        _logger.Trace("Executing Warden hooks OnError.");
                        _configuration.Hooks.OnError.Execute(exception);
                        _logger.Trace("Executing Warden hooks OnErrorAsync.");
                        await _configuration.Hooks.OnErrorAsync.ExecuteAsync(exception);
                    }
                    catch (Exception onErrorException)
                    {
                        _logger.Error("There was an error while executing internal Warden error hooks " +
                                      $"for iteration {_iterationOrdinal}.", onErrorException);
                    }
                }
            }
        }
Beispiel #3
0
 /// <summary>
 ///     Fired when a process dies.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private static void ProcessStopped(object sender, EventArrivedEventArgs e)
 {
     try
     {
         var targetInstance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
         var processId      = int.Parse(targetInstance["ProcessId"].ToString());
         targetInstance.Dispose();
         e.NewEvent.Dispose();
         try
         {
             HandleStoppedProcess(processId);
             Logger?.Debug($"{processId} stopped");
         }
         catch (Exception ex)
         {
             Logger?.Error(ex.ToString());
         }
     }
     catch (Exception ex)
     {
         Logger?.Error(ex.ToString());
     }
 }