@@ -59,7 +59,7 @@ public NodeBackupHostedService(IHostApplicationLifetime hostApplicationLifetime,
59
59
// Configure timer
60
60
_logger . LogDebug ( "Configuring NodeBackup Timer" ) ;
61
61
_timer = new System . Timers . Timer ( _nodeBackupConfiguration . Backup . Interval * 1000 ) ;
62
- _timer . Elapsed += TriggerNodeBackup ;
62
+ _timer . Elapsed += OnBackupTimerElapsed ;
63
63
64
64
// Configure output
65
65
_logger . LogDebug ( "Configuring Output path" ) ;
@@ -89,9 +89,12 @@ public NodeBackupHostedService(IHostApplicationLifetime hostApplicationLifetime,
89
89
hostApplicationLifetime . StopApplication ( ) ;
90
90
91
91
// Start embedded Prometheus MetricsServer
92
- if ( ! _nodeBackupConfiguration . Prometheus . EnableMetricsServer ) return ;
92
+ if ( ! _nodeBackupConfiguration . Prometheus . MetricsServer . Enabled ) return ;
93
93
_logger . LogInformation ( "Starting metrics server" ) ;
94
- _metricServer = new MetricServer ( hostname : "localhost" , port : 5001 , useHttps : false ) ;
94
+ _metricServer = new MetricServer (
95
+ hostname : "localhost" ,
96
+ port : _nodeBackupConfiguration . Prometheus . MetricsServer . Port ,
97
+ useHttps : _nodeBackupConfiguration . Prometheus . MetricsServer . UseHttps ) ;
95
98
_metricServer . Start ( ) ;
96
99
}
97
100
@@ -222,82 +225,95 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
222
225
_logger . LogDebug ( "Waiting 5 seconds before starting countdown" ) ;
223
226
await Task . Delay ( 5000 , cancellationToken ) ;
224
227
225
- await TriggerExecutionTimer ( cancellationToken ) ;
228
+ await ExecutionLoopAsync ( cancellationToken ) ;
226
229
227
230
_logger . LogInformation ( "Stopping backup timer" ) ;
228
231
_timer . Stop ( ) ;
229
232
}
230
233
231
- private async Task TriggerExecutionTimer ( CancellationToken cancellationToken )
234
+ private async Task ExecutionLoopAsync ( CancellationToken cancellationToken )
232
235
{
233
236
while ( ! cancellationToken . IsCancellationRequested )
234
237
{
235
- var nextIntervalCountdown = CalculateNextTargetSeconds ( ) ;
238
+ var nextIntervalSeconds = CalculateNextIntervalSeconds ( ) ;
236
239
237
240
if ( ! _timer . Enabled )
238
241
{
239
- if ( nextIntervalCountdown == 300 )
242
+ if ( nextIntervalSeconds == _nodeBackupConfiguration . Backup . Interval )
240
243
{
241
244
_logger . LogInformation ( "Starting backup timer" ) ;
242
245
_timer . Enabled = true ;
243
246
await ExecuteBackupProcedure ( DateTime . Now ) ;
244
247
}
245
248
else
246
249
{
247
- _logger . LogInformation ( "Counting down to backup timer start: {0}" ,
248
- nextIntervalCountdown . ToString ( ) ) ;
250
+ _logger . LogInformation ( "Counting down to backup timer start: {0} at {1} - {2} seconds" ,
251
+ TimeSpan . FromSeconds ( nextIntervalSeconds ) . ToString ( @"hh\:mm\:ss" ) ,
252
+ TimeSpan . FromSeconds ( DateTime . Now . Ticks / TimeSpan . TicksPerSecond + nextIntervalSeconds ) . ToString ( @"hh\:mm\:ss" ) ,
253
+ nextIntervalSeconds . ToString ( ) ) ;
249
254
}
250
255
}
251
256
else
252
257
{
253
- _logger . LogDebug ( "Counting down to next backup: {0}" , nextIntervalCountdown . ToString ( ) ) ;
258
+ _logger . LogInformation ( "Counting down to next backup: {0} at {1} - {2} seconds" ,
259
+ TimeSpan . FromSeconds ( nextIntervalSeconds ) . ToString ( @"hh\:mm\:ss" ) ,
260
+ TimeSpan . FromSeconds ( DateTime . Now . Ticks / TimeSpan . TicksPerSecond + nextIntervalSeconds ) . ToString ( @"hh\:mm\:ss" ) ,
261
+ nextIntervalSeconds . ToString ( ) ) ;
254
262
}
263
+
255
264
await Task . Delay ( 1000 , cancellationToken ) ;
256
265
}
257
266
}
258
267
259
- private long CalculateNextTargetSeconds ( )
268
+ private long CalculateNextIntervalSeconds ( )
260
269
{
261
- long nextTargetSeconds ;
262
-
263
- if ( DateTime . Now . Ticks / TimeSpan . TicksPerSecond < _nodeBackupConfiguration . Backup . Start . Ticks / TimeSpan . TicksPerSecond )
264
- {
265
- _logger . LogDebug ( "Current time is before target start time" ) ;
266
- nextTargetSeconds = ( ( _nodeBackupConfiguration . Backup . Start . Ticks - DateTime . Now . Ticks ) /
267
- TimeSpan . TicksPerSecond ) ;
268
-
269
- if ( nextTargetSeconds > _nodeBackupConfiguration . Backup . Interval )
270
- {
271
- nextTargetSeconds -= _nodeBackupConfiguration . Backup . Interval ;
270
+ return
271
+ DateTime . Now . Ticks / TimeSpan . TicksPerSecond < _nodeBackupConfiguration . Backup . Start . Ticks / TimeSpan . TicksPerSecond
272
+ ? CalculateIntervalSecondsFromNowToBackupStart ( ) : CalculateIntervalSecondsFromBackupStartToNow ( ) ;
273
+ }
272
274
273
- _logger . LogDebug (
274
- "Seconds to next interval : {0}" ,
275
- nextTargetSeconds . ToString ( ) ) ;
276
- }
275
+ private long CalculateIntervalSecondsFromBackupStartToNow ( )
276
+ {
277
+ _logger . LogDebug ( "Current time is later than target start time" ) ;
278
+ var nextTargetSeconds = ( ( DateTime . Now . Ticks - _nodeBackupConfiguration . Backup . Start . Ticks ) /
279
+ TimeSpan . TicksPerSecond ) ;
277
280
278
- nextTargetSeconds ++ ;
279
- }
281
+ if ( nextTargetSeconds < 0 )
282
+ nextTargetSeconds = _nodeBackupConfiguration . Backup . Interval - nextTargetSeconds ;
280
283
else
281
284
{
282
- _logger . LogDebug ( "Current time is later than target start time" ) ;
283
- nextTargetSeconds = ( ( DateTime . Now . Ticks - _nodeBackupConfiguration . Backup . Start . Ticks ) /
284
- TimeSpan . TicksPerSecond ) ;
285
+ nextTargetSeconds = _nodeBackupConfiguration . Backup . Interval -
286
+ nextTargetSeconds % _nodeBackupConfiguration . Backup . Interval ;
287
+ }
288
+
289
+ _logger . LogDebug (
290
+ "Seconds to next interval : {0}" ,
291
+ nextTargetSeconds . ToString ( ) ) ;
292
+
293
+ return nextTargetSeconds ;
294
+ }
295
+
296
+ private long CalculateIntervalSecondsFromNowToBackupStart ( )
297
+ {
298
+ _logger . LogDebug ( "Current time is before target start time" ) ;
299
+ var nextTargetSeconds = ( _nodeBackupConfiguration . Backup . Start . Ticks - DateTime . Now . Ticks ) /
300
+ TimeSpan . TicksPerSecond ;
301
+
302
+ if ( nextTargetSeconds > _nodeBackupConfiguration . Backup . Interval )
303
+ {
304
+ nextTargetSeconds -= _nodeBackupConfiguration . Backup . Interval ;
285
305
286
- if ( nextTargetSeconds < 0 )
287
- nextTargetSeconds = 300 - nextTargetSeconds ;
288
- else
289
- {
290
- nextTargetSeconds = _nodeBackupConfiguration . Backup . Interval - nextTargetSeconds % _nodeBackupConfiguration . Backup . Interval ;
291
- }
292
306
_logger . LogDebug (
293
307
"Seconds to next interval : {0}" ,
294
308
nextTargetSeconds . ToString ( ) ) ;
295
309
}
296
-
310
+
311
+ nextTargetSeconds ++ ;
312
+
297
313
return nextTargetSeconds ;
298
314
}
299
315
300
- private async void TriggerNodeBackup ( Object source , ElapsedEventArgs e )
316
+ private async void OnBackupTimerElapsed ( Object source , ElapsedEventArgs e )
301
317
{
302
318
_logger . LogInformation ( "Backup was triggered at {0:HH:mm:ss}" ,
303
319
e . SignalTime ) ;
0 commit comments