| | | 1 | | using CShells.Features; |
| | | 2 | | using Elsa.Hosting.Management.Contracts; |
| | | 3 | | using Elsa.Hosting.Management.HostedServices; |
| | | 4 | | using Elsa.Hosting.Management.Options; |
| | | 5 | | using Elsa.Hosting.Management.Services; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Hosting.Management.ShellFeatures; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Installs and configures the clustering feature. |
| | | 13 | | /// </summary> |
| | | 14 | | [ShellFeature( |
| | | 15 | | DisplayName = "Clustering", |
| | | 16 | | Description = "Provides clustering and heartbeat capabilities for distributed Elsa deployments")] |
| | | 17 | | [UsedImplicitly] |
| | | 18 | | public class ClusteringFeature : IShellFeature |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// A factory that instantiates an <see cref="IApplicationInstanceNameProvider"/>. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// Defaults to <see cref="ConfiguredApplicationInstanceNameProvider"/>, which honors |
| | | 25 | | /// <see cref="ApplicationInstanceOptions"/> for a stable instance name and falls back to a random |
| | | 26 | | /// name when none is configured (preserving the previous default behaviour). |
| | | 27 | | /// </remarks> |
| | 2 | 28 | | public Func<IServiceProvider, IApplicationInstanceNameProvider> InstanceNameProvider { get; set; } = sp => |
| | 1 | 29 | | { |
| | 1 | 30 | | return ActivatorUtilities.CreateInstance<ConfiguredApplicationInstanceNameProvider>(sp); |
| | 1 | 31 | | }; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Represents the options for heartbeat feature. |
| | | 35 | | /// </summary> |
| | 2 | 36 | | public Action<HeartbeatOptions> HeartbeatOptions { get; set; } = _ => { }; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Configures how the application instance name is determined. Set a stable name (for example from |
| | | 40 | | /// the pod name) to avoid accumulating orphaned per-instance transport entities across restarts. |
| | | 41 | | /// </summary> |
| | 3 | 42 | | public Action<ApplicationInstanceOptions> ApplicationInstanceOptions { get; set; } = _ => { }; |
| | | 43 | | |
| | | 44 | | public void ConfigureServices(IServiceCollection services) |
| | | 45 | | { |
| | 1 | 46 | | services.Configure(HeartbeatOptions) |
| | 1 | 47 | | .Configure(ApplicationInstanceOptions) |
| | 1 | 48 | | .AddSingleton(InstanceNameProvider) |
| | 1 | 49 | | .AddSingleton<RandomIntIdentityGenerator>() |
| | 1 | 50 | | .AddHostedService<InstanceHeartbeatService>() |
| | 1 | 51 | | .AddHostedService<InstanceHeartbeatMonitorService>(); |
| | 1 | 52 | | } |
| | | 53 | | } |