From 532f86885e9f58cf3f33a6ba9a37153d9878448b Mon Sep 17 00:00:00 2001 From: alsami Date: Sat, 9 Jan 2021 14:06:49 +0100 Subject: [PATCH] fix: make sure refresh-worker executes next on full hour of day --- src/Covid19Api.Worker/DataRefreshWorker.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Covid19Api.Worker/DataRefreshWorker.cs b/src/Covid19Api.Worker/DataRefreshWorker.cs index c02f6d9..8a45ad5 100644 --- a/src/Covid19Api.Worker/DataRefreshWorker.cs +++ b/src/Covid19Api.Worker/DataRefreshWorker.cs @@ -57,11 +57,20 @@ private DateTime CalculateInitialExecutionTime() private DateTime CalculateNextRun() { var nextExecution = DateTime.UtcNow.AddHours(4); + if (nextExecution.Day != DateTime.UtcNow.Day) { nextExecution = DateTime.UtcNow.Date.AddDays(1); } + var hourDiffTillNextDay = 24 - nextExecution.Hour; + + if (hourDiffTillNextDay <= 4) + { + nextExecution = nextExecution.AddHours(hourDiffTillNextDay - 1); + } + + this.logger.LogInformation("Next refresh run {nextRun}", nextExecution.ToString("O")); return nextExecution; }