Make the update scheduling more readable (hopefully)
This commit is contained in:
parent
460d1ee131
commit
f9cc9b5e14
|
@ -33,6 +33,7 @@ test {
|
||||||
def jvmTargetVersion = "11"
|
def jvmTargetVersion = "11"
|
||||||
compileKotlin {
|
compileKotlin {
|
||||||
kotlinOptions.jvmTarget = jvmTargetVersion
|
kotlinOptions.jvmTarget = jvmTargetVersion
|
||||||
|
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
|
||||||
}
|
}
|
||||||
compileJava {
|
compileJava {
|
||||||
targetCompatibility = jvmTargetVersion
|
targetCompatibility = jvmTargetVersion
|
||||||
|
|
|
@ -38,6 +38,8 @@ import kotlin.collections.ArrayList
|
||||||
import kotlin.collections.HashMap
|
import kotlin.collections.HashMap
|
||||||
import kotlin.collections.HashSet
|
import kotlin.collections.HashSet
|
||||||
import kotlin.concurrent.scheduleAtFixedRate
|
import kotlin.concurrent.scheduleAtFixedRate
|
||||||
|
import kotlin.time.Duration
|
||||||
|
import kotlin.time.ExperimentalTime
|
||||||
|
|
||||||
class CacheController {
|
class CacheController {
|
||||||
|
|
||||||
|
@ -247,30 +249,39 @@ class CacheController {
|
||||||
* update the CourseList every 24h, the Timetables every 3h and the Mensa Menu every hour
|
* 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!
|
* doesn't account the change between winter and summer time!
|
||||||
*/
|
*/
|
||||||
|
@OptIn(ExperimentalTime::class)
|
||||||
private fun scheduledUpdates() {
|
private fun scheduledUpdates() {
|
||||||
val currentTime = System.currentTimeMillis()
|
val currentTime = System.currentTimeMillis()
|
||||||
val initDelay24h = (86400000 - ((currentTime + 3600000) % 86400000)) + 60000
|
|
||||||
val initDelay3h = (10800000 - ((currentTime + 3600000) % 10800000)) + 60000
|
val duration24h = Duration.hours(24).inWholeMilliseconds
|
||||||
val initDelay1h = (3600000 - ((currentTime + 3600000) % 3600000)) + 60000
|
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)
|
// update courseList every 24 hours (time in ms)
|
||||||
Timer().scheduleAtFixedRate(initDelay24h, 86400000) {
|
Timer().scheduleAtFixedRate(initDelay24h, duration24h) {
|
||||||
asyncUpdateCourseList()
|
asyncUpdateCourseList()
|
||||||
}
|
}
|
||||||
|
|
||||||
// update all already existing timetables every 3 hours (time in ms)
|
// update all already existing timetables every 3 hours (time in ms)
|
||||||
Timer().scheduleAtFixedRate(initDelay3h, 10800000) {
|
Timer().scheduleAtFixedRate(initDelay3h, duration3h) {
|
||||||
asyncUpdateTimetables()
|
asyncUpdateTimetables()
|
||||||
}
|
}
|
||||||
|
|
||||||
// update courses every hour (time in ms)
|
// update courses every hour (time in ms)
|
||||||
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
|
Timer().scheduleAtFixedRate(initDelay1h, duration1h) {
|
||||||
asyncUpdateMensa()
|
asyncUpdateMensa()
|
||||||
}
|
}
|
||||||
|
|
||||||
// post to status.mosad.xyz every hour, if an API key is present
|
// post to status.mosad.xyz every hour, if an API key is present
|
||||||
if (StartupController.cachetAPIKey != "0") {
|
if (StartupController.cachetAPIKey != "0") {
|
||||||
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
|
Timer().scheduleAtFixedRate(initDelay1h, duration1h) {
|
||||||
CachetAPIController.postTotalRequests()
|
CachetAPIController.postTotalRequests()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue