diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt index 08e5367..8a152a7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt @@ -4,7 +4,6 @@ import com.github.insanusmokrassar.krontab.utils.* import com.github.insanusmokrassar.krontab.utils.clamp import com.github.insanusmokrassar.krontab.utils.dayOfMonthRange import com.github.insanusmokrassar.krontab.utils.monthRange -import com.soywiz.klock.* /** * [month] 0-11 @@ -13,7 +12,7 @@ import com.soywiz.klock.* * [minutes] 0-59 * [seconds] 0-59 */ -data class CronDateTime( +internal data class CronDateTime( val month: Byte? = null, val dayOfMonth: Byte? = null, val hours: Byte? = null, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt index 1c07221..0c13ca2 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt @@ -8,7 +8,7 @@ private val anyCronDateTime by lazy { CronDateTime() } -data class CronDateTimeScheduler( +internal data class CronDateTimeScheduler( internal val cronDateTimes: List ) @@ -27,7 +27,7 @@ internal fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now() seconds?.let { val left = it - current.seconds - current += DateTimeSpan(minutes = if (left < 0) 1 else 0, seconds = left) + current += DateTimeSpan(minutes = if (left <= 0) 1 else 0, seconds = left) } minutes?.let { diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt index 214eacb..a9584fe 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/Scheduler.kt @@ -1,12 +1,21 @@ package com.github.insanusmokrassar.krontab -suspend fun doWhile(scheduleConfig: String, block: suspend () -> Boolean) { +suspend fun executeInfinity(scheduleConfig: String, block: suspend () -> Unit) { + val scheduler = CronDateTimeScheduler(parse(scheduleConfig)) + + scheduler.doInLoop { + block() + true + } +} + +suspend fun executeWhile(scheduleConfig: String, block: suspend () -> Boolean) { val scheduler = CronDateTimeScheduler(parse(scheduleConfig)) scheduler.doInLoop(block) } -suspend fun executeOnce(scheduleConfig: String, block: suspend () -> Boolean) { +suspend fun executeOnce(scheduleConfig: String, block: suspend () -> Unit) { val scheduler = CronDateTimeScheduler(parse(scheduleConfig)) scheduler.doInLoop { diff --git a/src/jvm8Main/kotlin/com/github/insanusmokrassar/krontab/Test.kt b/src/jvm8Main/kotlin/com/github/insanusmokrassar/krontab/Test.kt new file mode 100644 index 0000000..813cb69 --- /dev/null +++ b/src/jvm8Main/kotlin/com/github/insanusmokrassar/krontab/Test.kt @@ -0,0 +1,14 @@ +package com.github.insanusmokrassar.krontab + +import com.soywiz.klock.DateTime +import kotlinx.coroutines.runBlocking + +fun main() { + runBlocking { + var i = 0 + executeInfinity("/10 /25 * * *") { + println(DateTime.now()) + i++ + } + } +}