Browse Source

Make the update scheduling more readable (hopefully)

pull/17/head
Hannes Braun 9 months ago
parent
commit
f9cc9b5e14
Signed by: hannesbraun
GPG Key ID: 7B6557E1DFD685BE
  1. 1
      build.gradle
  2. 25
      src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt

1
build.gradle

@ -33,6 +33,7 @@ test {
def jvmTargetVersion = "11"
compileKotlin {
kotlinOptions.jvmTarget = jvmTargetVersion
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
compileJava {
targetCompatibility = jvmTargetVersion

25
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()
}
}

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close