diff --git a/build.gradle b/build.gradle index 1747233..3cf7e2a 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,7 @@ test { def jvmTargetVersion = "11" compileKotlin { kotlinOptions.jvmTarget = jvmTargetVersion + kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn" } compileJava { targetCompatibility = jvmTargetVersion diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt b/src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt index 76788ba..24d4ddd 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt @@ -38,6 +38,8 @@ import kotlin.collections.ArrayList import kotlin.collections.HashMap import kotlin.collections.HashSet import kotlin.concurrent.scheduleAtFixedRate +import kotlin.time.Duration +import kotlin.time.ExperimentalTime class CacheController { @@ -247,30 +249,39 @@ class CacheController { * update the CourseList every 24h, the Timetables every 3h and the Mensa Menu every hour * doesn't account the change between winter and summer time! */ + @OptIn(ExperimentalTime::class) private fun scheduledUpdates() { val currentTime = System.currentTimeMillis() - val initDelay24h = (86400000 - ((currentTime + 3600000) % 86400000)) + 60000 - val initDelay3h = (10800000 - ((currentTime + 3600000) % 10800000)) + 60000 - val initDelay1h = (3600000 - ((currentTime + 3600000) % 3600000)) + 60000 + + val duration24h = Duration.hours(24).inWholeMilliseconds + val duration3h = Duration.hours(3).inWholeMilliseconds + val duration1h = Duration.hours(1).inWholeMilliseconds + val duration1m = Duration.minutes(1).inWholeMilliseconds + + // Calculate the initial delay to make the update time independent of the start time + fun calcInitDelay(period: Long) = (period - ((currentTime + duration1h) % period)) + duration1m + val initDelay24h = calcInitDelay(duration24h) + val initDelay3h = calcInitDelay(duration3h) + val initDelay1h = calcInitDelay(duration1h) // update courseList every 24 hours (time in ms) - Timer().scheduleAtFixedRate(initDelay24h, 86400000) { + Timer().scheduleAtFixedRate(initDelay24h, duration24h) { asyncUpdateCourseList() } // update all already existing timetables every 3 hours (time in ms) - Timer().scheduleAtFixedRate(initDelay3h, 10800000) { + Timer().scheduleAtFixedRate(initDelay3h, duration3h) { asyncUpdateTimetables() } // update courses every hour (time in ms) - Timer().scheduleAtFixedRate(initDelay1h, 3600000) { + Timer().scheduleAtFixedRate(initDelay1h, duration1h) { asyncUpdateMensa() } // post to status.mosad.xyz every hour, if an API key is present if (StartupController.cachetAPIKey != "0") { - Timer().scheduleAtFixedRate(initDelay1h, 3600000) { + Timer().scheduleAtFixedRate(initDelay1h, duration1h) { CachetAPIController.postTotalRequests() } }