| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Hosting.Management.Contracts; |
| | | 3 | | using Elsa.Hosting.Management.Options; |
| | | 4 | | using Elsa.KeyValues.Contracts; |
| | | 5 | | using Elsa.KeyValues.Entities; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Hosting; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Hosting.Management.HostedServices; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Service to write heartbeat messages per running instance. |
| | | 15 | | /// </summary> |
| | | 16 | | [UsedImplicitly] |
| | | 17 | | public class InstanceHeartbeatService : IHostedService, IDisposable |
| | | 18 | | { |
| | | 19 | | private readonly IServiceProvider _serviceProvider; |
| | | 20 | | private readonly HeartbeatOptions _heartbeatOptions; |
| | | 21 | | private Timer? _timer; |
| | | 22 | | |
| | | 23 | | internal const string HeartbeatKeyPrefix = "Heartbeat_"; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Creates a new instance of the <see cref="InstanceHeartbeatService"/> |
| | | 27 | | /// </summary> |
| | 0 | 28 | | public InstanceHeartbeatService(IServiceProvider serviceProvider, IOptions<HeartbeatOptions> heartbeatOptions) |
| | | 29 | | { |
| | 0 | 30 | | _serviceProvider = serviceProvider; |
| | 0 | 31 | | _heartbeatOptions = heartbeatOptions.Value; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public Task StartAsync(CancellationToken cancellationToken) |
| | | 36 | | { |
| | 0 | 37 | | _timer = new Timer(WriteHeartbeat, null, TimeSpan.Zero, _heartbeatOptions.Interval); |
| | 0 | 38 | | return Task.CompletedTask; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public Task StopAsync(CancellationToken cancellationToken) |
| | | 43 | | { |
| | 0 | 44 | | _timer?.Change(Timeout.Infinite, Timeout.Infinite); |
| | 0 | 45 | | return Task.CompletedTask; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public void Dispose() |
| | | 50 | | { |
| | 0 | 51 | | _timer?.Dispose(); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | private void WriteHeartbeat(object? state) |
| | | 55 | | { |
| | 0 | 56 | | _ = Task.Run(async () => await WriteHeartbeatAsync()); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | private async Task WriteHeartbeatAsync() |
| | | 60 | | { |
| | 0 | 61 | | using var scope = _serviceProvider.CreateScope(); |
| | 0 | 62 | | var instanceNameProvider = scope.ServiceProvider.GetRequiredService<IApplicationInstanceNameProvider>(); |
| | 0 | 63 | | var store = scope.ServiceProvider.GetRequiredService<IKeyValueStore>(); |
| | 0 | 64 | | var systemClock = scope.ServiceProvider.GetRequiredService<ISystemClock>(); |
| | 0 | 65 | | await store.SaveAsync(new SerializedKeyValuePair |
| | 0 | 66 | | { |
| | 0 | 67 | | Id = $"{HeartbeatKeyPrefix}{instanceNameProvider.GetName()}", |
| | 0 | 68 | | SerializedValue = systemClock.UtcNow.ToString("o") |
| | 0 | 69 | | }, |
| | 0 | 70 | | default); |
| | 0 | 71 | | } |
| | | 72 | | } |