-
Notifications
You must be signed in to change notification settings - Fork 26
feat : Add health endpoint #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a25739d
feat : health check endpoint
teamssUTXO fce8124
feat : add live depedencies health checks besides startup checks
teamssUTXO 3f974b1
feat : add health pages
teamssUTXO 6ad6c0a
refactor : health page
teamssUTXO f100113
fix coderabbit issue
teamssUTXO 3605f18
fix : review
teamssUTXO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using Npgsql; | ||
|
|
||
| namespace PluginBuilder.Services; | ||
|
|
||
| public class HealthService : IHealthCheck | ||
| { | ||
| public HealthService(DBConnectionFactory dbConnectionFactory, AzureStorageClient azureStorageClient, ProcessRunner processRunner, IHostApplicationLifetime lifetime) | ||
| { | ||
| DbConnectionFactory = dbConnectionFactory; | ||
| AzureStorageClient = azureStorageClient; | ||
| ProcessRunner = processRunner; | ||
|
|
||
| _lifetime = lifetime; | ||
| } | ||
|
|
||
| private readonly IHostApplicationLifetime _lifetime; | ||
|
|
||
| private DBConnectionFactory DbConnectionFactory { get; } | ||
| private AzureStorageClient AzureStorageClient { get; } | ||
| private ProcessRunner ProcessRunner { get; } | ||
|
|
||
| public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| if (!_lifetime.ApplicationStarted.IsCancellationRequested) | ||
| return HealthCheckResult.Unhealthy("Startup incomplete"); | ||
|
|
||
| var dbTask = IsDatabaseHealthy(cancellationToken); | ||
| var dockerTask = IsDockerHealthy(cancellationToken); | ||
| var azureTask = AzureStorageClient.IsDefaultContainerAccessible(cancellationToken); | ||
|
|
||
| await Task.WhenAll(dbTask, dockerTask, azureTask); | ||
|
|
||
| var isHealthy = dbTask.Result && dockerTask.Result && azureTask.Result; | ||
|
|
||
| return isHealthy | ||
| ? HealthCheckResult.Healthy() | ||
| : HealthCheckResult.Unhealthy("Critical dependency unavailable"); | ||
| } | ||
|
|
||
| private async Task<bool> IsDatabaseHealthy(CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| await using var conn = await DbConnectionFactory.Open(cancellationToken); | ||
| await using var cmd = new NpgsqlCommand("SELECT 1", conn); | ||
| var result = await cmd.ExecuteScalarAsync(cancellationToken); | ||
| return result is 1; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private async Task<bool> IsDockerHealthy(CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| var code = await ProcessRunner.RunAsync(new ProcessSpec | ||
| { | ||
| Executable = "docker", | ||
| Arguments = ["info", "--format", "{{ .ServerVersion }}"], | ||
| OutputCapture = new OutputCapture(), | ||
| ErrorCapture = new OutputCapture() | ||
| }, cancellationToken); | ||
| return code == 0; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| namespace PluginBuilder.ViewModels.Home; | ||
|
|
||
| public class HealthCheckViewModel | ||
| { | ||
| public string Healthy { get; set; } = string.Empty; | ||
|
|
||
| public string Description { get; set; } = string.Empty; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.