From 4fcc70aac0bb8a39b60e6e286ca4a205fef8a31f Mon Sep 17 00:00:00 2001 From: Casper-NS Date: Mon, 18 Dec 2023 13:07:37 +0100 Subject: [PATCH] fixed sleep time of aggregation --- CentralHub.Api/Services/LocalizationService.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CentralHub.Api/Services/LocalizationService.cs b/CentralHub.Api/Services/LocalizationService.cs index 675f37f..06beea5 100644 --- a/CentralHub.Api/Services/LocalizationService.cs +++ b/CentralHub.Api/Services/LocalizationService.cs @@ -18,8 +18,24 @@ public async Task DoWorkAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { + var now = DateTime.UtcNow; + logger.LogInformation("Starting measurement aggregation {Time}", now); await AggregateMeasurementsAsync(stoppingToken); - await Task.Delay(SleepTime, stoppingToken); + var after = DateTime.UtcNow; + logger.LogInformation("Ending aggregation {Time}", after); + + var aggregationTime = after - now; + logger.LogInformation("Aggregation took: {Time} s", aggregationTime.TotalSeconds); + + if (aggregationTime <= SleepTime) + { + logger.LogInformation("Aggregation finished sleeping {Time} until next aggregation", SleepTime - aggregationTime); + await Task.Delay(SleepTime - aggregationTime, stoppingToken); + } + else + { + logger.LogWarning("Aggregation of measurements took {Time}, which is longer than what has been allocated.", aggregationTime); + } } }