add base public functions for scheduling

This commit is contained in:
InsanusMokrassar 2019-10-05 11:14:35 +06:00
parent fa68bf72dc
commit eb0f78a9e4
2 changed files with 45 additions and 34 deletions

View File

@ -1,6 +1,7 @@
package com.github.insanusmokrassar.krontab package com.github.insanusmokrassar.krontab
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import com.soywiz.klock.DateTimeSpan
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
private val anyCronDateTime by lazy { 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) 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 { do {
delay(next().unixMillisLong - DateTime.now().unixMillisLong) delay(next().unixMillisLong - DateTime.now().unixMillisLong)
} while (block()) } 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
}

View File

@ -1,37 +1,16 @@
package com.github.insanusmokrassar.krontab package com.github.insanusmokrassar.krontab
import com.soywiz.klock.DateTime suspend fun doWhile(scheduleConfig: String, block: suspend () -> Boolean) {
import com.soywiz.klock.DateTimeSpan val scheduler = CronDateTimeScheduler(parse(scheduleConfig))
scheduler.doInLoop(block)
}
fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime {
var current = relativelyTo suspend fun executeOnce(scheduleConfig: String, block: suspend () -> Boolean) {
val scheduler = CronDateTimeScheduler(parse(scheduleConfig))
seconds?.let {
val left = it - current.seconds scheduler.doInLoop {
current += DateTimeSpan(minutes = if (left < 0) 1 else 0, seconds = left) block()
} false
}
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
} }