Skip to content

Commit

Permalink
Hotfix for health check function (#1507)
Browse files Browse the repository at this point in the history
* Updated static class and cleaned up duplicate code

* Infrastructure health page code improvements

* Added missing dependency to let the function run
  • Loading branch information
akhavich authored Dec 13, 2024
1 parent 06d2293 commit 954cc1f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -607,53 +607,63 @@ public async Task<IntermediateHealthCheckResult> CheckWebApp(InfrastructureHealt
var errors = new List<string>();
var status = InfrastructureHealthStatus.Healthy;

await using var ctx = await dbContextFactory.CreateDbContextAsync();

var project = await ctx.Projects
.AsNoTracking()
.Include(p => p.Resources)
.FirstOrDefaultAsync(p => p.Project_Acronym_CD == request.Name);

// If the project is null, the project does not exist or there was an error retrieving it
if (project == null)
{
errors.Add("Unable to retrieve project.");
status = InfrastructureHealthStatus.Unhealthy;
}
else
try
{
var isRequested = TerraformVariableExtraction.IsResourceRequested(project, TerraformTemplate.AzureAppService);
var appServiceConfig = TerraformVariableExtraction.ExtractAppServiceConfiguration(project);
await using var ctx = await dbContextFactory.CreateDbContextAsync();

if (!isRequested)
{
status = InfrastructureHealthStatus.Undefined;
}
else if (appServiceConfig is null)
var project = await ctx.Projects
.AsNoTracking()
.Include(p => p.Resources)
.FirstOrDefaultAsync(p => p.Project_Acronym_CD == request.Name);

// If the project is null, the project does not exist or there was an error retrieving it
if (project == null)
{
errors.Add("Unable to retrieve App Service configuration from project resource.");
errors.Add("Unable to retrieve project.");
status = InfrastructureHealthStatus.Unhealthy;
}
else
{
var isProvisioned = !(string.IsNullOrEmpty(appServiceConfig.HostName) && string.IsNullOrEmpty(appServiceConfig.Id));
if (!isProvisioned)
var isRequested = TerraformVariableExtraction.IsResourceRequested(project, TerraformTemplate.AzureAppService);
var appServiceConfig = TerraformVariableExtraction.ExtractAppServiceConfiguration(project);

if (!isRequested)
{
status = InfrastructureHealthStatus.Undefined;
}
else if (appServiceConfig is null)
{
errors.Add("App has not been provisioned - it may still be processing.");
status = InfrastructureHealthStatus.NeedHealthCheckRun;
errors.Add("Unable to retrieve App Service configuration from project resource.");
status = InfrastructureHealthStatus.Unhealthy;
}
else
{
var appIsRunning = await workspaceWebAppManagementService.GetState(appServiceConfig.Id);
if (!appIsRunning)
var isProvisioned = !(string.IsNullOrEmpty(appServiceConfig.HostName) && string.IsNullOrEmpty(appServiceConfig.Id));
if (!isProvisioned)
{
errors.Add("App is provisioned but not running.");
status = InfrastructureHealthStatus.Degraded;
errors.Add("App has not been provisioned - it may still be processing.");
status = InfrastructureHealthStatus.NeedHealthCheckRun;
}
else
{
var appIsRunning = await workspaceWebAppManagementService.GetState(appServiceConfig.Id);
if (!appIsRunning)
{
errors.Add("App is provisioned but not running.");
status = InfrastructureHealthStatus.Degraded;
}
}
}
}
}

catch (Exception ex)
{
status = InfrastructureHealthStatus.Unhealthy;
errors.Add("Error while verifying web app. " + ex.GetType().ToString());
errors.Add($"Details: {ex.Message}");
}

return new(status, errors);
}

Expand Down
4 changes: 3 additions & 1 deletion ServerlessOperations/src/Datahub.Functions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Datahub.Shared.Configuration;
using Datahub.Application.Services.WebApp;
using Datahub.Infrastructure.Services.WebApp;
using Datahub.Infrastructure.Offline.Security;


var host = new HostBuilder()
Expand Down Expand Up @@ -93,8 +94,9 @@
services.AddScoped<EmailValidator>();
services.AddScoped<HealthCheckHelper>();
services.AddDatahubConfigurationFromFunctionFormat(config);


// This is necessary to satisfy a dependency in WorkspaceWebAppManagementService, but it's not actually used so Offline works fine.
services.AddScoped<IKeyVaultUserService, OfflineKeyVaultUserService>();
})
.Build();

Expand Down

0 comments on commit 954cc1f

Please sign in to comment.