Skip to content

Commit

Permalink
fixed sleep time of aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-NS committed Dec 18, 2023
1 parent 5e5a180 commit b9b8d6b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions CentralHub.Api/Services/LocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ public class LocalizationService(
IRoomRepository roomRepository)
: IScopedProcessingService
{
private static readonly TimeSpan SleepTime = TimeSpan.FromMinutes(5);
private static readonly TimeSpan SleepTime = TimeSpan.FromMinutes(1);

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);
}
}
}

Expand Down

0 comments on commit b9b8d6b

Please sign in to comment.