public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { try { // Make a non-invasive query to make sure we can reach the data store. await _testProvider.PerformTest(_container.Value, _configuration, _cosmosCollectionConfiguration); return(HealthCheckResult.Healthy("Successfully connected to the data store.")); } catch (CosmosException ex) when(ex.IsCmkClientError()) { return(HealthCheckResult.Unhealthy( "Connection to the data store was unsuccesful because the client's customer-managed key is not available.", exception: ex, new Dictionary <string, object>() { { "IsCustomerManagedKeyError", true } })); } catch (Exception ex) when(ex.IsRequestRateExceeded()) { return(HealthCheckResult.Healthy("Connection to the data store was successful, however, the rate limit has been exceeded.")); } catch (Exception ex) { _logger.LogWarning(ex, "Failed to connect to the data store."); return(HealthCheckResult.Unhealthy("Failed to connect to the data store.")); } }
public async Task <Container> InitializeCollection(CosmosClient client) { Database database = client.GetDatabase(_cosmosDataStoreConfiguration.DatabaseId); Container containerClient = database.GetContainer(_cosmosCollectionConfiguration.CollectionId); _logger.LogInformation("Finding Container: {collectionId}", _cosmosCollectionConfiguration.CollectionId); AsyncPolicy retryPolicy = _retryExceptionPolicyFactory.GetRetryPolicy(); var existingContainer = await retryPolicy.ExecuteAsync(async() => await database.TryGetContainerAsync(_cosmosCollectionConfiguration.CollectionId)); _logger.LogInformation("Creating Cosmos Container if not exits: {collectionId}", _cosmosCollectionConfiguration.CollectionId); ContainerResponse containerResponse = await retryPolicy.ExecuteAsync(async() => await database.CreateContainerIfNotExistsAsync( _cosmosCollectionConfiguration.CollectionId, $"/{KnownDocumentProperties.PartitionKey}", _cosmosCollectionConfiguration.InitialCollectionThroughput)); if (containerResponse.StatusCode == HttpStatusCode.Created || containerResponse.Resource.DefaultTimeToLive != -1) { if (_cosmosCollectionConfiguration.InitialCollectionThroughput.HasValue) { var throughputProperties = ThroughputProperties.CreateManualThroughput(_cosmosCollectionConfiguration.InitialCollectionThroughput.Value); await retryPolicy.ExecuteAsync(async() => await containerClient.ReplaceThroughputAsync(throughputProperties)); } containerResponse.Resource.DefaultTimeToLive = -1; existingContainer = await retryPolicy.ExecuteAsync(async() => await containerClient.ReplaceContainerAsync(containerResponse)); } await retryPolicy.ExecuteAsync(async() => { try { await _clientTestProvider.PerformTest(existingContainer, _cosmosDataStoreConfiguration, _cosmosCollectionConfiguration); } catch (CosmosException e) when(e.StatusCode == HttpStatusCode.TooManyRequests) { // This is the very first interaction with the collection, and we might get this exception // when it calls GetCachedContainerPropertiesAsync, which does not use our request handler. throw new RequestRateExceededException(e.RetryAfter); } }); await _upgradeManager.SetupContainerAsync(containerClient); return(existingContainer); }
public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { try { // Make a non-invasive query to make sure we can reach the data store. await _testProvider.PerformTest(_container.Value, _configuration, _cosmosCollectionConfiguration); return(HealthCheckResult.Healthy("Successfully connected to the data store.")); } catch (Exception ex) when(ex.IsRequestRateExceeded()) { return(HealthCheckResult.Healthy("Connection to the data store was successful, however, the rate limit has been exceeded.")); } catch (Exception ex) { _logger.LogWarning(ex, "Failed to connect to the data store."); return(HealthCheckResult.Unhealthy("Failed to connect to the data store.")); } }