Пример #1
0
 public WardenCheckResultMinified(WardenCheckResult check)
 {
     v = check.IsValid;
     r = new WatcherCheckResultMinified(check.WatcherCheckResult);
     s = check.StartedAt.Ticks;
     c = check.CompletedAt.Ticks;
     t = check.ExecutionTime.Ticks;
     e = check.Exception != null ? new ExceptionInfoMinified(check.Exception) : null;
 }
Пример #2
0
 public WardenCheckResultDto(WardenCheckResult result)
 {
     IsValid = result.IsValid;
     WatcherCheckResult = new WatcherCheckResultDto(result.WatcherCheckResult);
     StartedAt = result.StartedAt;
     CompletedAt = result.CompletedAt;
     ExecutionTime = result.ExecutionTime;
     Exception = result.Exception == null ? null : new ExceptionDto(result.Exception);
 }
Пример #3
0
 public WardenCheckResultDto(WardenCheckResult result)
 {
     IsValid            = result.IsValid;
     WatcherCheckResult = new WatcherCheckResultDto(result.WatcherCheckResult);
     StartedAt          = result.StartedAt;
     CompletedAt        = result.CompletedAt;
     ExecutionTime      = result.ExecutionTime;
     Exception          = result.Exception == null ? null : new ExceptionDto(result.Exception);
 }
Пример #4
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);
        }
Пример #5
0
 private void ValidateCheckResult(WardenCheckResult check)
 {
     if (check.WatcherCheckResult == null)
     {
         throw new ArgumentNullException(nameof(check.WatcherCheckResult),
                                         "Watcher check result can not be null.");
     }
     if (check.WatcherCheckResult.WatcherName.Empty())
     {
         throw new ArgumentException("Watcher name can not be empty.",
                                     nameof(check.WatcherCheckResult.WatcherName));
     }
     if (check.WatcherCheckResult.WatcherType.Empty())
     {
         throw new ArgumentException("Watcher type can not be empty.",
                                     nameof(check.WatcherCheckResult.WatcherType));
     }
 }
Пример #6
0
 private void ValidateCheckResult(WardenCheckResult check)
 {
     if (check.WatcherCheckResult == null)
     {
         throw new ServiceException(OperationCodes.EmptyWatcherCheckResult,
                                    "Watcher check result can not be null.");
     }
     if (check.WatcherCheckResult.WatcherName.Empty())
     {
         throw new ServiceException(OperationCodes.EmptyWatcherName,
                                    "Watcher name can not be empty.");
     }
     if (check.WatcherCheckResult.WatcherType.Empty())
     {
         throw new ServiceException(OperationCodes.EmptyWatcherType,
                                    "Watcher type can not be empty.");
     }
 }
Пример #7
0
        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);
            }
        }
Пример #8
0
        public async Task <WardenIterationDto> SaveIterationAsync(WardenIterationDto iteration, Guid organizationId)
        {
            if (iteration == null)
            {
                return(null);
            }

            var wardenCheckResults  = (iteration.Results ?? Enumerable.Empty <WardenCheckResultDto>()).ToList();
            var watcherCheckResults = wardenCheckResults.Select(x => x.WatcherCheckResult).ToList();

            watcherCheckResults.ForEach(SetWatcherTypeFromFullNamespace);

            var organization = await _database.Organizations().GetByIdAsync(organizationId);

            if (organization == null)
            {
                throw new ServiceException($"Organization has not been found for given id: '{organizationId}'.");
            }

            var warden = organization.GetWardenByName(iteration.WardenName);

            if (warden == null && !organization.AutoRegisterNewWarden)
            {
                throw new ServiceException($"Warden with name: '{iteration.WardenName}' has not been registered.");
            }

            var updateOrganization = false;

            if (warden == null)
            {
                if (organization.Wardens.Count() >= _featureSettings.MaxWardensInOrganization)
                {
                    throw new ServiceException($"Limit of {_featureSettings.MaxWardensInOrganization} " +
                                               "wardens in organization has been reached.");
                }

                updateOrganization = true;
                organization.AddWarden(iteration.WardenName);
                warden = organization.GetWardenByName(iteration.WardenName);
            }
            else if (!warden.Enabled)
            {
                throw new ServiceException($"Warden with name: '{iteration.WardenName}' is disabled.");
            }

            var wardenIteration = new WardenIteration(warden.Name, organization,
                                                      iteration.Ordinal, iteration.StartedAt, iteration.CompletedAt, iteration.IsValid);

            foreach (var result in wardenCheckResults)
            {
                WatcherType watcherType;
                var         watcherName = result.WatcherCheckResult.WatcherName;
                if (!Enum.TryParse(result.WatcherCheckResult.WatcherType, true, out watcherType))
                {
                    watcherType = WatcherType.Custom;
                }

                var watcher = warden.GetWatcherByName(watcherName);
                if (watcher == null)
                {
                    if (warden.Watchers.Count() >= _featureSettings.MaxWatchersInWarden)
                    {
                        throw new ServiceException($"Limit of {_featureSettings.MaxWatchersInWarden} " +
                                                   "watchers in Warden has been reached.");
                    }

                    updateOrganization = true;
                    warden.AddWatcher(watcherName, watcherType);
                    watcher = warden.GetWatcherByName(watcherName);
                }

                var watcherCheckResult = result.WatcherCheckResult.IsValid
                    ? WatcherCheckResult.Valid(watcher, result.WatcherCheckResult.Description)
                    : WatcherCheckResult.Invalid(watcher, result.WatcherCheckResult.Description);

                var checkResult = result.IsValid
                    ? WardenCheckResult.Valid(watcherCheckResult, result.StartedAt, result.CompletedAt)
                    : WardenCheckResult.Invalid(watcherCheckResult, result.StartedAt, result.CompletedAt,
                                                CreatExceptionInfo(result.Exception));

                wardenIteration.AddResult(checkResult);
            }

            if (updateOrganization)
            {
                await _database.Organizations().ReplaceOneAsync(x => x.Id == organizationId, organization);
            }

            await _database.WardenIterations().InsertOneAsync(wardenIteration);

            Logger.Info($"Warden iteration: '{wardenIteration.Id}' was created " +
                        $"for organization: '{organization.Name}' with id: '{organization.Id}'.");

            return(new WardenIterationDto(wardenIteration));
        }