| | | 1 | | using Elsa.Workflows.Runtime.HealthChecks; |
| | | 2 | | using Elsa.Workflows.Runtime.Options; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Adds Elsa workflow runtime readiness checks to ASP.NET Core health checks. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class HealthCheckExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Tag applied to Elsa health checks. |
| | | 15 | | /// </summary> |
| | | 16 | | public const string ElsaTag = "elsa"; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Tag applied to Elsa readiness checks. |
| | | 20 | | /// </summary> |
| | | 21 | | public const string ReadinessTag = "elsa-readiness"; |
| | | 22 | | |
| | 2 | 23 | | private static readonly string[] ReadinessTags = [ElsaTag, ReadinessTag]; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Adds conservative Elsa-specific readiness probes for the workflow runtime and its core stores. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="builder">The health checks builder.</param> |
| | | 29 | | /// <param name="includePersistence">Whether to probe workflow management/runtime stores.</param> |
| | | 30 | | /// <param name="includeDistributedLocks">Whether to probe the configured distributed lock provider.</param> |
| | | 31 | | /// <param name="configureOptions">Configures Elsa readiness probe behavior.</param> |
| | | 32 | | public static IHealthChecksBuilder AddElsaReadinessChecks( |
| | | 33 | | this IHealthChecksBuilder builder, |
| | | 34 | | bool includePersistence = true, |
| | | 35 | | bool includeDistributedLocks = false, |
| | | 36 | | Action<ElsaReadinessHealthCheckOptions>? configureOptions = null) |
| | | 37 | | { |
| | 7 | 38 | | var optionsBuilder = builder.Services.AddOptions<ElsaReadinessHealthCheckOptions>(); |
| | | 39 | | |
| | 7 | 40 | | if (configureOptions != null) |
| | 2 | 41 | | optionsBuilder.Configure(configureOptions); |
| | | 42 | | |
| | 7 | 43 | | optionsBuilder |
| | 5 | 44 | | .Validate(x => x.DistributedLockAcquisitionTimeout > TimeSpan.Zero, "Distributed lock acquisition timeout mu |
| | 7 | 45 | | .ValidateOnStart(); |
| | | 46 | | |
| | 7 | 47 | | builder.AddCheck<ElsaRuntimeHealthCheck>("elsa-runtime", tags: ReadinessTags); |
| | | 48 | | |
| | 7 | 49 | | if (includePersistence) |
| | 3 | 50 | | builder.AddCheck<ElsaWorkflowPersistenceHealthCheck>("elsa-workflow-persistence", tags: ReadinessTags); |
| | | 51 | | |
| | 7 | 52 | | if (includeDistributedLocks) |
| | 6 | 53 | | builder.AddCheck<ElsaDistributedLockHealthCheck>("elsa-distributed-locks", tags: ReadinessTags); |
| | | 54 | | |
| | 7 | 55 | | return builder; |
| | | 56 | | } |
| | | 57 | | } |