Skip to content

Commit

Permalink
[ODS-6041] HealthCheck exception thrown (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
simpat-jesus authored Oct 16, 2023
1 parent 4110d4d commit 559b047
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,47 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using EdFi.Ods.Common.Configuration;
using EdFi.Ods.Common.Configuration.Sections;
using EdFi.Ods.Common.Constants;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace EdFi.Ods.Api.Extensions
{
public static class HealthCheckServiceExtensions
{
public static IServiceCollection AddHealthCheck(this IServiceCollection services, string connectionString, bool isSqlServer)
public static IServiceCollection AddHealthCheck(this IServiceCollection services, IConfigurationRoot configuration, ApiSettings apiSettings)
{
var hcBuilder = services.AddHealthChecks();
if (isSqlServer)
Dictionary<string, string> connectionStrings;

if (apiSettings.IsFeatureEnabled(ApiFeature.MultiTenancy.GetConfigKeyName()))
{
hcBuilder.AddSqlServer(connectionString);
connectionStrings = configuration.Get<TenantsSection>().Tenants.ToDictionary(x => x.Key, x => x.Value.ConnectionStrings["EdFi_Admin"]);
}
else
{
hcBuilder.AddNpgSql(connectionString);
connectionStrings = new() {
{ "SingleTenant", configuration.GetConnectionString("EdFi_Admin") }
};
}

var isSqlServer = "SQLServer".Equals(apiSettings.Engine, StringComparison.OrdinalIgnoreCase);
var hcBuilder = services.AddHealthChecks();

foreach (var connectionString in connectionStrings)
{
if (isSqlServer)
{
hcBuilder.AddSqlServer(connectionString.Value, name: connectionString.Key);
}
else
{
hcBuilder.AddNpgSql(connectionString.Value, name: connectionString.Key);
}
}

return services;
Expand Down
4 changes: 1 addition & 3 deletions Application/EdFi.Ods.Api/Startup/OdsStartupBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,10 @@ public void ConfigureServices(IServiceCollection services)
});
}

services.AddHealthCheck(Configuration.GetConnectionString("EdFi_Admin"), IsSqlServer(_apiSettings.Engine));
services.AddHealthCheck(Configuration, _apiSettings);
services.AddScheduledJobs();
}

private static bool IsSqlServer(string databaseEngine) => "SQLServer".Equals(databaseEngine, StringComparison.OrdinalIgnoreCase);

// This method gets called by the runtime. Use this method to add services to the container.

// ConfigureContainer is where you can register things directly
Expand Down

0 comments on commit 559b047

Please sign in to comment.