From fa68bf72dce003c8179f07dabee706d7e5cba9a3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 5 Oct 2019 11:09:22 +0600 Subject: [PATCH] add base scheduling --- .../insanusmokrassar/krontab/CronDateTime.kt | 31 ----------------- .../krontab/CronDateTimeScheduler.kt | 22 ++++++++++++ .../insanusmokrassar/krontab/Scheduler.kt | 34 +++++++++++++++++++ 3 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt index fb14235..08e5367 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt @@ -46,34 +46,3 @@ data class CronDateTime( ) } } - -fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime { - var current = relativelyTo - - seconds?.let { - val left = it - current.seconds - current += DateTimeSpan(minutes = if (left < 0) 1 else 0, seconds = left) - } - - minutes?.let { - val left = it - current.minutes - current += DateTimeSpan(hours = if (left < 0) 1 else 0, minutes = left) - } - - hours?.let { - val left = it - current.hours - current += DateTimeSpan(days = if (left < 0) 1 else 0, hours = left) - } - - klockDayOfMonth ?.let { - val left = it - current.dayOfMonth - current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left) - } - - month ?.let { - val left = it - current.month0 - current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left) - } - - return current -} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt new file mode 100644 index 0000000..f6cf1f1 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt @@ -0,0 +1,22 @@ +package com.github.insanusmokrassar.krontab + +import com.soywiz.klock.DateTime +import kotlinx.coroutines.delay + +private val anyCronDateTime by lazy { + CronDateTime() +} + +data class CronDateTimeScheduler( + internal val cronDateTimes: List +) + +internal fun CronDateTimeScheduler.next(relatively: DateTime = DateTime.now()): DateTime { + return cronDateTimes.map { it.toNearDateTime(relatively) }.min() ?: anyCronDateTime.toNearDateTime(relatively) +} + +suspend fun CronDateTimeScheduler.doInLoop(block: () -> Boolean) { + do { + delay(next().unixMillisLong - DateTime.now().unixMillisLong) + } while (block()) +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt index 8a710c0..ab709a1 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt @@ -1,3 +1,37 @@ package com.github.insanusmokrassar.krontab +import com.soywiz.klock.DateTime +import com.soywiz.klock.DateTimeSpan + + +fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime { + var current = relativelyTo + + seconds?.let { + val left = it - current.seconds + current += DateTimeSpan(minutes = if (left < 0) 1 else 0, seconds = left) + } + + minutes?.let { + val left = it - current.minutes + current += DateTimeSpan(hours = if (left < 0) 1 else 0, minutes = left) + } + + hours?.let { + val left = it - current.hours + current += DateTimeSpan(days = if (left < 0) 1 else 0, hours = left) + } + + klockDayOfMonth ?.let { + val left = it - current.dayOfMonth + current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left) + } + + month ?.let { + val left = it - current.month0 + current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left) + } + + return current +}