예제 #1
0
        public async Task <IWatcherCheckResult> ExecuteAsync()
        {
            try
            {
                var mongoDb = _configuration.MongoDbProvider?.Invoke() ?? await _connection.GetDatabaseAsync();

                if (mongoDb == null)
                {
                    return(MongoDbWatcherCheckResult.Create(this, false, _configuration.Database,
                                                            _configuration.ConnectionString, $"Database: '{_configuration.Database}' has not been found."));
                }
                if (string.IsNullOrWhiteSpace(_configuration.Query))
                {
                    return(MongoDbWatcherCheckResult.Create(this, true, _configuration.Database,
                                                            _configuration.ConnectionString,
                                                            $"Database: {_configuration.Database} has been sucessfully checked."));
                }

                return(await ExecuteForQueryAsync(mongoDb));
            }
            catch (MongoException exception)
            {
                return(MongoDbWatcherCheckResult.Create(this, false, _configuration.Database,
                                                        _configuration.ConnectionString, exception.Message));
            }
            catch (Exception exception)
            {
                throw new WatcherException("There was an error while trying to access the MongoDB.", exception);
            }
        }
예제 #2
0
        public async Task <IWatcherCheckResult> ExecuteAsync()
        {
            try
            {
                var database = _configuration.MongoDbProvider?.Invoke() ?? await _connection.GetDatabaseAsync();

                if (database == null)
                {
                    return(MongoDbWatcherCheckResult.Create(this, false, _configuration.Database,
                                                            _configuration.ConnectionString, $"Database: '{_configuration.Database}' has not been found."));
                }
                if (string.IsNullOrWhiteSpace(_configuration.Query))
                {
                    return(MongoDbWatcherCheckResult.Create(this, true, _configuration.Database,
                                                            _configuration.ConnectionString,
                                                            $"Database: {_configuration.Database} has been sucessfully checked."));
                }

                var queryResult = await database.QueryAsync(_configuration.CollectionName, _configuration.Query);

                var isValid = true;
                if (_configuration.EnsureThatAsync != null)
                {
                    isValid = await _configuration.EnsureThatAsync?.Invoke(queryResult);
                }

                isValid = isValid && (_configuration.EnsureThat?.Invoke(queryResult) ?? true);
                var description = $"MongoDB check has returned {(isValid ? "valid" : "invalid")} result for " +
                                  $"database: '{_configuration.Database}' and given query.";

                return(MongoDbWatcherCheckResult.Create(this, isValid, _configuration.Database,
                                                        _configuration.ConnectionString, _configuration.Query, queryResult, description));
            }
            catch (MongoException exception)
            {
                return(MongoDbWatcherCheckResult.Create(this, false, _configuration.Database,
                                                        _configuration.ConnectionString, exception.Message));
            }
            catch (Exception exception)
            {
                throw new WatcherException("There was an error while trying to access the MongoDB.", exception);
            }
        }
        public virtual async Task <bool> EnsureCreatedAsync(CancellationToken cancellationToken = new CancellationToken())
        {
            await _mongoDbConnection.GetDatabaseAsync(cancellationToken);

            return(true);
        }