From de3ad4bbabd385a7a0e061eb68264bf47e3a7e5b Mon Sep 17 00:00:00 2001 From: Richa Bansal <57157506+rbans96@users.noreply.github.com> Date: Thu, 8 Jul 2021 11:22:49 -0700 Subject: [PATCH] Service to startup without schema initialized (#866) * Enable service to respond to schema apis * Update indexDataStore instance * Catch the particular exception --- .../BackgroundServiceHealthCheck.cs | 20 +++++++++++++------ .../Store/SqlIndexDataStoreFactory.cs | 8 +++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.Health.Dicom.Core/Features/HealthCheck/BackgroundServiceHealthCheck.cs b/src/Microsoft.Health.Dicom.Core/Features/HealthCheck/BackgroundServiceHealthCheck.cs index f840e4db96..0130a546dc 100644 --- a/src/Microsoft.Health.Dicom.Core/Features/HealthCheck/BackgroundServiceHealthCheck.cs +++ b/src/Microsoft.Health.Dicom.Core/Features/HealthCheck/BackgroundServiceHealthCheck.cs @@ -11,6 +11,7 @@ using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; using Microsoft.Health.Dicom.Core.Configs; +using Microsoft.Health.Dicom.Core.Exceptions; using Microsoft.Health.Dicom.Core.Features.Store; namespace Microsoft.Health.Dicom.Core.Features.HealthCheck @@ -41,13 +42,20 @@ public BackgroundServiceHealthCheck( public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { - Task oldestWaitingToBeDeleated = _backgroundServiceHealthCheckCache.GetOrAddOldestTimeAsync(_indexDataStore.GetOldestDeletedAsync, cancellationToken); - Task numReachedMaxedRetry = _backgroundServiceHealthCheckCache.GetOrAddNumExhaustedDeletionAttemptsAsync( - t => _indexDataStore.RetrieveNumExhaustedDeletedInstanceAttemptsAsync(_deletedInstanceCleanupConfiguration.MaxRetries, t), - cancellationToken); + try + { + Task oldestWaitingToBeDeleated = _backgroundServiceHealthCheckCache.GetOrAddOldestTimeAsync(_indexDataStore.GetOldestDeletedAsync, cancellationToken); + Task numReachedMaxedRetry = _backgroundServiceHealthCheckCache.GetOrAddNumExhaustedDeletionAttemptsAsync( + t => _indexDataStore.RetrieveNumExhaustedDeletedInstanceAttemptsAsync(_deletedInstanceCleanupConfiguration.MaxRetries, t), + cancellationToken); - _telemetryClient.GetMetric("Oldest-Requested-Deletion").TrackValue((await oldestWaitingToBeDeleated).ToUnixTimeSeconds()); - _telemetryClient.GetMetric("Count-Deletions-Max-Retry").TrackValue(await numReachedMaxedRetry); + _telemetryClient.GetMetric("Oldest-Requested-Deletion").TrackValue((await oldestWaitingToBeDeleated).ToUnixTimeSeconds()); + _telemetryClient.GetMetric("Count-Deletions-Max-Retry").TrackValue(await numReachedMaxedRetry); + } + catch (DataStoreException e) // This is expected when service is starting up without schema initialization + { + return HealthCheckResult.Unhealthy("Unhealthy service." + e.Message); + } return HealthCheckResult.Healthy("Successfully computed values for background service."); } diff --git a/src/Microsoft.Health.Dicom.SqlServer/Features/Store/SqlIndexDataStoreFactory.cs b/src/Microsoft.Health.Dicom.SqlServer/Features/Store/SqlIndexDataStoreFactory.cs index 71467911db..6015f3cfba 100644 --- a/src/Microsoft.Health.Dicom.SqlServer/Features/Store/SqlIndexDataStoreFactory.cs +++ b/src/Microsoft.Health.Dicom.SqlServer/Features/Store/SqlIndexDataStoreFactory.cs @@ -26,7 +26,13 @@ public SqlIndexDataStoreFactory(SchemaInformation schemaInformation, IEnumerable public IIndexDataStore GetInstance() { - return _indexDataStores.First(store => (int)store.Version == _schemaInformation.Current.Value); + // if the service is starting without schema initialized + if (_schemaInformation.Current == null) + { + return _indexDataStores.First(store => (int)store.Version == _schemaInformation.MinimumSupportedVersion); + } + + return _indexDataStores.FirstOrDefault(store => (int)store.Version == _schemaInformation.Current); } } }