From eb0f78a9e4b35ce54b40787bcdcb9525e903be6a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 5 Oct 2019 11:14:35 +0600 Subject: [PATCH] add base public functions for scheduling --- .../krontab/CronDateTimeScheduler.kt | 34 +++++++++++++- .../insanusmokrassar/krontab/Scheduler.kt | 45 +++++-------------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt index f6cf1f1..1c07221 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.krontab import com.soywiz.klock.DateTime +import com.soywiz.klock.DateTimeSpan import kotlinx.coroutines.delay private val anyCronDateTime by lazy { @@ -15,8 +16,39 @@ internal fun CronDateTimeScheduler.next(relatively: DateTime = DateTime.now()): return cronDateTimes.map { it.toNearDateTime(relatively) }.min() ?: anyCronDateTime.toNearDateTime(relatively) } -suspend fun CronDateTimeScheduler.doInLoop(block: () -> Boolean) { +internal suspend fun CronDateTimeScheduler.doInLoop(block: suspend () -> Boolean) { do { delay(next().unixMillisLong - DateTime.now().unixMillisLong) } while (block()) } + +internal 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/Scheduler.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt index ab709a1..214eacb 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt @@ -1,37 +1,16 @@ package com.github.insanusmokrassar.krontab -import com.soywiz.klock.DateTime -import com.soywiz.klock.DateTimeSpan +suspend fun doWhile(scheduleConfig: String, block: suspend () -> Boolean) { + val scheduler = CronDateTimeScheduler(parse(scheduleConfig)) - - -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 + scheduler.doInLoop(block) +} + +suspend fun executeOnce(scheduleConfig: String, block: suspend () -> Boolean) { + val scheduler = CronDateTimeScheduler(parse(scheduleConfig)) + + scheduler.doInLoop { + block() + false + } }