@@ -10,43 +10,34 @@ namespace OpenShock.LiveControlGateway;
1010/// </summary>
1111public sealed class LcgKeepAlive : IHostedService
1212{
13- private readonly IServiceProvider _serviceProvider ;
13+ private readonly IRedisConnectionProvider _redisConnectionProvider ;
1414 private readonly IWebHostEnvironment _env ;
1515 private readonly LcgOptions _options ;
1616 private readonly ILogger < LcgKeepAlive > _logger ;
17- private readonly System . Timers . Timer _timer ;
1817
1918 private uint _errorsInRow ;
2019
21- // ReSharper disable once InconsistentNaming
2220 private static readonly TimeSpan KeepAliveKeyTTL = TimeSpan . FromSeconds ( 35 ) ; // 35 seconds
2321 private static readonly TimeSpan KeepAliveInterval = TimeSpan . FromSeconds ( 15 ) ; // 15 seconds
2422
2523 /// <summary>
2624 /// DI Constructor
2725 /// </summary>
28- /// <param name="serviceProvider "></param>
26+ /// <param name="redisConnectionProvider "></param>
2927 /// <param name="env"></param>
3028 /// <param name="options"></param>
3129 /// <param name="logger"></param>
32- public LcgKeepAlive ( IServiceProvider serviceProvider , IWebHostEnvironment env , LcgOptions options , ILogger < LcgKeepAlive > logger )
30+ public LcgKeepAlive ( IRedisConnectionProvider redisConnectionProvider , IWebHostEnvironment env , LcgOptions options , ILogger < LcgKeepAlive > logger )
3331 {
34- _serviceProvider = serviceProvider ;
32+ _redisConnectionProvider = redisConnectionProvider ;
3533 _env = env ;
3634 _options = options ;
3735 _logger = logger ;
38-
39- _timer = new System . Timers . Timer ( KeepAliveInterval )
40- {
41- AutoReset = true
42- } ;
43- _timer . Elapsed += OnTimerElapsed ;
4436 }
4537
4638 private async Task SelfOnline ( )
4739 {
48- var redisConnectionProvider = _serviceProvider . GetRequiredService < IRedisConnectionProvider > ( ) ;
49- var lcgNodes = redisConnectionProvider . RedisCollection < LcgNode > ( false ) ;
40+ var lcgNodes = _redisConnectionProvider . RedisCollection < LcgNode > ( false ) ;
5041
5142 var online = await lcgNodes . FindByIdAsync ( _options . Fqdn ) ;
5243 if ( online is null )
@@ -63,7 +54,7 @@ await lcgNodes.InsertAsync(new LcgNode
6354
6455 if ( online . Country != _options . CountryCode )
6556 {
66- var changeTracker = redisConnectionProvider . RedisCollection < LcgNode > ( ) ;
57+ var changeTracker = _redisConnectionProvider . RedisCollection < LcgNode > ( ) ;
6758 var tracked = await changeTracker . FindByIdAsync ( _options . Fqdn ) ;
6859 if ( tracked is not null )
6960 {
@@ -76,41 +67,49 @@ await lcgNodes.InsertAsync(new LcgNode
7667 "Could not save changed firmware version to redis, device was not found in change tracker, this shouldn't be possible but it somehow was?" ) ;
7768 }
7869
79- await redisConnectionProvider . Connection . ExecuteAsync ( "EXPIRE" ,
80- $ "{ typeof ( LcgNode ) . FullName } :{ _options . Fqdn } ", KeepAliveKeyTTL ) ;
70+ await _redisConnectionProvider . Connection . ExecuteAsync ( "EXPIRE" ,
71+ $ "{ typeof ( LcgNode ) . FullName } :{ _options . Fqdn } ", ( int ) KeepAliveKeyTTL . TotalSeconds ) ;
8172 }
8273
83- private async void OnTimerElapsed ( object ? sender , System . Timers . ElapsedEventArgs e )
74+ private async Task Loop ( )
8475 {
85- try
86- {
87- _logger . LogDebug ( "Sending keep alive..." ) ;
88- await SelfOnline ( ) ;
89- _errorsInRow = 0 ;
90- }
91- catch ( Exception ex )
76+ while ( true )
9277 {
93- ++ _errorsInRow ;
94- _logger . LogError ( ex , "Error sending gateway keep alive {Attempt}" , _errorsInRow ) ;
95- if ( _errorsInRow >= 10 )
78+ try
9679 {
97- _logger . LogCritical ( "Too many errors in a row sending keep alive, terminating process" ) ;
98- Environment . Exit ( 1001 ) ;
80+ _logger . LogDebug ( "Sending keep alive..." ) ;
81+ await SelfOnline ( ) ;
82+ _logger . LogDebug ( "Sent keep alive!" ) ;
83+ _errorsInRow = 0 ;
84+ await Task . Delay ( KeepAliveInterval ) ;
85+ }
86+ catch ( Exception e )
87+ {
88+ ++ _errorsInRow ;
89+ _logger . LogError ( e , "Error sending gateway keep alive {Attempt}" , _errorsInRow ) ;
90+ if ( _errorsInRow >= 10 )
91+ {
92+ _logger . LogCritical ( "Too many errors in a row sending keep alive, terminating process" ) ;
93+ Environment . Exit ( 1001 ) ;
94+ }
95+
96+ await Task . Delay ( KeepAliveInterval ) ;
9997 }
10098 }
99+ // ReSharper disable once FunctionNeverReturns
101100 }
102101
103102 /// <inheritdoc />
104103 public Task StartAsync ( CancellationToken cancellationToken )
105104 {
106- _timer . Start ( ) ;
105+ OsTask . Run ( Loop ) ;
106+
107107 return Task . CompletedTask ;
108108 }
109109
110110 /// <inheritdoc />
111111 public Task StopAsync ( CancellationToken cancellationToken )
112112 {
113- _timer . Stop ( ) ;
114113 return Task . CompletedTask ;
115114 }
116115}
0 commit comments