2020-11-21 08:58:19 +00:00
|
|
|
package dev.inmo.krontab
|
2019-10-05 10:34:59 +00:00
|
|
|
|
2019-10-10 10:43:52 +00:00
|
|
|
import com.soywiz.klock.DateTime
|
2021-09-25 13:28:49 +00:00
|
|
|
import com.soywiz.klock.DateTimeTz
|
2019-10-10 10:43:52 +00:00
|
|
|
import kotlinx.coroutines.delay
|
2021-09-26 05:49:01 +00:00
|
|
|
import kotlinx.coroutines.isActive
|
|
|
|
import kotlin.coroutines.coroutineContext
|
2019-10-10 10:43:52 +00:00
|
|
|
|
2022-04-29 15:57:10 +00:00
|
|
|
|
2021-09-25 13:28:49 +00:00
|
|
|
/**
|
|
|
|
* Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
|
|
|
|
*
|
|
|
|
* WARNING!!! If you want to launch it in parallel, you must do this explicitly.
|
|
|
|
*
|
|
|
|
* WARNING!!! In case if [KronScheduler.next] of [this] instance will return null, [block] will be called immediately
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun <T> KronScheduler.doOnce(noinline block: suspend (DateTime) -> T): T {
|
2021-09-26 07:05:59 +00:00
|
|
|
val time = nextOrNow().also {
|
|
|
|
delay((it - DateTime.now()).millisecondsLong)
|
2021-09-25 13:28:49 +00:00
|
|
|
}
|
2021-09-26 07:05:59 +00:00
|
|
|
return block(time)
|
2021-09-25 13:28:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 15:57:10 +00:00
|
|
|
/**
|
|
|
|
* Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
|
|
|
|
*
|
|
|
|
* WARNING!!! If you want to launch it in parallel, you must do this explicitly.
|
|
|
|
*
|
|
|
|
* WARNING!!! In case if [KronScheduler.next] of [this] instance will return null, [block] will be called immediately
|
|
|
|
*/
|
|
|
|
@Deprecated("Replaceable", ReplaceWith("doOnce", "dev.inmo.krontab.doOnce"))
|
|
|
|
suspend inline fun <T> KronScheduler.doOnceLocal(noinline block: suspend (DateTime) -> T): T = doOnce(block)
|
|
|
|
|
2020-06-03 15:53:55 +00:00
|
|
|
/**
|
|
|
|
* Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
|
|
|
|
*
|
2020-07-24 09:23:44 +00:00
|
|
|
* WARNING!!! If you want to launch it in parallel, you must do this explicitly.
|
2021-01-02 15:35:08 +00:00
|
|
|
*
|
2021-09-26 07:05:59 +00:00
|
|
|
* WARNING!!! In case if [KronScheduler.next] of [this] instance will return null, [block] will be called immediatelly
|
2020-06-03 15:53:55 +00:00
|
|
|
*/
|
2021-09-26 07:05:59 +00:00
|
|
|
suspend inline fun <T> KronScheduler.doOnceTz(noinline block: suspend (DateTimeTz) -> T): T {
|
|
|
|
val time = when (this) {
|
|
|
|
is KronSchedulerTz -> nextOrNowWithOffset()
|
|
|
|
else -> nextOrNow().local
|
2021-01-02 15:35:08 +00:00
|
|
|
}
|
2021-09-26 07:05:59 +00:00
|
|
|
delay((time - DateTimeTz.nowLocal()).millisecondsLong)
|
|
|
|
return block(time)
|
2020-06-03 15:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-29 15:57:10 +00:00
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doOnce] on it
|
2021-09-26 07:05:59 +00:00
|
|
|
* @see buildSchedule
|
|
|
|
*/
|
|
|
|
suspend inline fun <T> doOnce(
|
|
|
|
scheduleConfig: String,
|
|
|
|
noinline block: suspend (DateTime) -> T
|
2022-04-29 15:57:10 +00:00
|
|
|
) = buildSchedule(scheduleConfig).doOnce(block)
|
2021-09-26 07:05:59 +00:00
|
|
|
|
|
|
|
/**
|
2022-04-29 15:57:10 +00:00
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doOnce] on it
|
2021-09-26 07:05:59 +00:00
|
|
|
* @see buildSchedule
|
|
|
|
*/
|
|
|
|
suspend inline fun <T> doOnceTz(
|
|
|
|
scheduleConfig: String,
|
|
|
|
noinline block: suspend (DateTimeTz) -> T
|
|
|
|
) = buildSchedule(scheduleConfig).doOnceTz(block)
|
|
|
|
|
2020-06-03 15:53:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will execute [block] while it will return true as a result of its calculation
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun KronScheduler.doWhile(noinline block: suspend (DateTime) -> Boolean) {
|
2021-09-25 13:28:49 +00:00
|
|
|
do {
|
|
|
|
delay(1L)
|
2022-04-29 15:57:10 +00:00
|
|
|
} while (doOnce(block))
|
2021-09-25 13:28:49 +00:00
|
|
|
}
|
2022-04-29 15:57:10 +00:00
|
|
|
/**
|
|
|
|
* Will execute [block] while it will return true as a result of its calculation
|
|
|
|
*/
|
|
|
|
@Deprecated("Replaceable", ReplaceWith("doWhile", "dev.inmo.krontab.doWhile"))
|
|
|
|
suspend inline fun KronScheduler.doWhileLocal(noinline block: suspend (DateTime) -> Boolean) = doWhile(block)
|
2021-09-25 13:28:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will execute [block] while it will return true as a result of its calculation
|
|
|
|
*/
|
2021-09-26 07:05:59 +00:00
|
|
|
suspend inline fun KronScheduler.doWhileTz(noinline block: suspend (DateTimeTz) -> Boolean) {
|
2021-09-25 13:28:49 +00:00
|
|
|
do {
|
|
|
|
delay(1L)
|
2021-09-26 07:05:59 +00:00
|
|
|
} while (doOnceTz(block))
|
2019-10-10 10:43:52 +00:00
|
|
|
}
|
2020-06-03 15:53:55 +00:00
|
|
|
|
2021-09-26 07:05:59 +00:00
|
|
|
/**
|
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doWhile] with [block]
|
|
|
|
*
|
|
|
|
* @see buildSchedule
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun doWhile(
|
2021-09-26 07:05:59 +00:00
|
|
|
scheduleConfig: String,
|
|
|
|
noinline block: suspend (DateTime) -> Boolean
|
2022-04-29 15:57:10 +00:00
|
|
|
) = buildSchedule(scheduleConfig).doWhile(block)
|
2021-09-26 07:05:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doWhile] with [block]
|
|
|
|
*
|
|
|
|
* @see buildSchedule
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
@Deprecated("Replaceable", ReplaceWith("doWhile", "dev.inmo.krontab.doWhile"))
|
|
|
|
suspend inline fun doWhileLocal(
|
2021-09-26 07:05:59 +00:00
|
|
|
scheduleConfig: String,
|
2022-04-29 15:57:10 +00:00
|
|
|
noinline block: suspend (DateTime) -> Boolean
|
|
|
|
) = doWhile(scheduleConfig, block)
|
2021-09-26 07:05:59 +00:00
|
|
|
|
2020-06-03 15:53:55 +00:00
|
|
|
/**
|
2020-07-24 09:23:44 +00:00
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doWhile] with [block]
|
|
|
|
*
|
|
|
|
* @see buildSchedule
|
2020-06-03 15:53:55 +00:00
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun doWhileTz(
|
2019-10-10 10:43:52 +00:00
|
|
|
scheduleConfig: String,
|
2022-04-29 15:57:10 +00:00
|
|
|
noinline block: suspend (DateTimeTz) -> Boolean
|
|
|
|
) = buildSchedule(scheduleConfig).doWhileTz(block)
|
2021-09-26 07:05:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will execute [block] without any checking of result
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun KronScheduler.doInfinity(noinline block: suspend (DateTime) -> Unit) = doWhile {
|
2021-09-26 07:05:59 +00:00
|
|
|
block(it)
|
|
|
|
coroutineContext.isActive
|
2021-09-25 13:28:49 +00:00
|
|
|
}
|
2020-06-03 15:53:55 +00:00
|
|
|
/**
|
|
|
|
* Will execute [block] without any checking of result
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
@Deprecated("Replaceable", ReplaceWith("doInfinity", "dev.inmo.krontab.doInfinity"))
|
|
|
|
suspend inline fun KronScheduler.doInfinityLocal(noinline block: suspend (DateTime) -> Unit) = doInfinity(block)
|
2021-09-25 13:28:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will execute [block] without any checking of result
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun KronScheduler.doInfinityTz(noinline block: suspend (DateTimeTz) -> Unit) = doWhileTz {
|
|
|
|
block(it)
|
2021-09-26 05:49:01 +00:00
|
|
|
coroutineContext.isActive
|
2021-09-25 13:28:49 +00:00
|
|
|
}
|
2021-09-26 07:05:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doInfinity] with [block]
|
|
|
|
*
|
|
|
|
* @see buildSchedule
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun doInfinity(
|
2021-09-26 07:05:59 +00:00
|
|
|
scheduleConfig: String,
|
|
|
|
noinline block: suspend (DateTime) -> Unit
|
2022-04-29 15:57:10 +00:00
|
|
|
) = buildSchedule(scheduleConfig).doInfinity(block)
|
2021-09-26 07:05:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doInfinity] with [block]
|
|
|
|
*
|
|
|
|
* @see buildSchedule
|
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
@Deprecated("Replaceable", ReplaceWith("doInfinity", "dev.inmo.krontab.doInfinity"))
|
|
|
|
suspend inline fun doInfinityLocal(
|
2021-09-26 07:05:59 +00:00
|
|
|
scheduleConfig: String,
|
2022-04-29 15:57:10 +00:00
|
|
|
noinline block: suspend (DateTime) -> Unit
|
|
|
|
) = doInfinity(scheduleConfig, block)
|
2021-09-26 07:05:59 +00:00
|
|
|
|
2020-06-03 15:53:55 +00:00
|
|
|
/**
|
2020-07-24 09:23:44 +00:00
|
|
|
* Will [buildSchedule] using [scheduleConfig] and call [doInfinity] with [block]
|
|
|
|
*
|
|
|
|
* @see buildSchedule
|
2020-06-03 15:53:55 +00:00
|
|
|
*/
|
2022-04-29 15:57:10 +00:00
|
|
|
suspend inline fun doInfinityTz(
|
2019-10-08 16:15:00 +00:00
|
|
|
scheduleConfig: String,
|
2022-04-29 15:57:10 +00:00
|
|
|
noinline block: suspend (DateTimeTz) -> Unit
|
|
|
|
) = buildSchedule(scheduleConfig).doInfinityTz(block)
|