| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Workflows.Runtime; |
| | | 4 | | |
| | | 5 | | public class HeartbeatGenerator : IDisposable |
| | | 6 | | { |
| | | 7 | | private readonly Timer _timer; |
| | | 8 | | private readonly Func<Task> _heartbeatAction; |
| | | 9 | | private readonly TimeSpan _interval; |
| | | 10 | | private readonly ILogger<HeartbeatGenerator> _logger; |
| | | 11 | | |
| | 474 | 12 | | public HeartbeatGenerator(Func<Task> heartbeatAction, TimeSpan interval, ILoggerFactory loggerFactory) |
| | | 13 | | { |
| | 474 | 14 | | _heartbeatAction = heartbeatAction; |
| | 474 | 15 | | _interval = interval; |
| | 474 | 16 | | _logger = loggerFactory.CreateLogger<HeartbeatGenerator>(); |
| | 474 | 17 | | _timer = new(GenerateHeartbeatAsync, null, _interval, Timeout.InfiniteTimeSpan); |
| | 474 | 18 | | } |
| | | 19 | | |
| | | 20 | | private async void GenerateHeartbeatAsync(object? state) |
| | | 21 | | { |
| | | 22 | | try |
| | | 23 | | { |
| | 0 | 24 | | _logger.LogDebug("Generating heartbeat"); |
| | 0 | 25 | | await _heartbeatAction(); |
| | 0 | 26 | | _timer.Change(_interval, Timeout.InfiniteTimeSpan); |
| | 0 | 27 | | } |
| | 0 | 28 | | catch (Exception ex) |
| | | 29 | | { |
| | 0 | 30 | | _logger.LogError(ex, "An error occurred while generating a workflow heartbeat."); |
| | 0 | 31 | | } |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void Dispose() |
| | | 35 | | { |
| | 474 | 36 | | _timer.Dispose(); |
| | 474 | 37 | | } |
| | | 38 | | } |