elsa-core/src/modules/Elsa.Workflows.Runtime/Extensions/HealthCheckExtensions.cs

49 lines
1.9 KiB
C#
Raw Normal View History

using Elsa.Workflows.Runtime.HealthChecks;
2026-05-20 22:09:46 +00:00
using Elsa.Workflows.Runtime.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Elsa.Extensions;
/// <summary>
/// Adds Elsa workflow runtime readiness checks to ASP.NET Core health checks.
/// </summary>
public static class HealthCheckExtensions
{
2026-05-20 22:09:46 +00:00
/// <summary>
/// Tag applied to Elsa readiness checks.
/// </summary>
public const string ReadinessTag = "readiness";
private static readonly string[] ReadinessTags = ["elsa", ReadinessTag];
/// <summary>
/// Adds conservative Elsa-specific readiness probes for the workflow runtime and its core stores.
/// </summary>
/// <param name="builder">The health checks builder.</param>
/// <param name="includePersistence">Whether to probe workflow management/runtime stores.</param>
/// <param name="includeDistributedLocks">Whether to probe the configured distributed lock provider.</param>
2026-05-20 22:09:46 +00:00
/// <param name="configureOptions">Configures Elsa readiness probe behavior.</param>
public static IHealthChecksBuilder AddElsaReadinessChecks(
this IHealthChecksBuilder builder,
bool includePersistence = true,
2026-05-20 22:09:46 +00:00
bool includeDistributedLocks = false,
Action<ElsaReadinessHealthCheckOptions>? configureOptions = null)
{
var optionsBuilder = builder.Services.AddOptions<ElsaReadinessHealthCheckOptions>();
2026-05-20 22:09:46 +00:00
if (configureOptions != null)
optionsBuilder.Configure(configureOptions);
2026-05-20 22:09:46 +00:00
builder.AddCheck<ElsaRuntimeHealthCheck>("elsa-runtime", tags: ReadinessTags);
if (includePersistence)
builder.AddCheck<ElsaWorkflowPersistenceHealthCheck>("elsa-workflow-persistence", tags: ReadinessTags);
if (includeDistributedLocks)
builder.AddCheck<ElsaDistributedLockHealthCheck>("elsa-distributed-locks", tags: ReadinessTags);
return builder;
}
}