krontab/src/commonMain/kotlin/dev/inmo/krontab/KronScheduler.kt

27 lines
1013 B
Kotlin
Raw Normal View History

2020-11-21 08:58:19 +00:00
package dev.inmo.krontab
2019-10-10 10:43:52 +00:00
import korlibs.time.DateTime
2019-10-10 10:43:52 +00:00
2020-06-03 15:07:10 +00:00
/**
* This interface was created for abstraction of [next] operation. Currently, there is only
2020-11-21 08:58:19 +00:00
* [dev.inmo.krontab.internal.CronDateTimeScheduler] realisation of this interface inside of this library,
2020-06-03 15:07:10 +00:00
* but you it is possible to create your own realisation of this interface for scheduling, for example, depending of
* users activity or something like this
*
2020-11-21 08:58:19 +00:00
* @see dev.inmo.krontab.internal.CronDateTimeScheduler
2020-06-03 15:07:10 +00:00
*/
2019-10-10 10:43:52 +00:00
interface KronScheduler {
2021-04-10 09:29:55 +00:00
2020-06-03 15:07:10 +00:00
/**
* @return Next [DateTime] when some action must be triggered according to settings of this instance
*
2020-11-21 08:58:19 +00:00
* @see dev.inmo.krontab.internal.CronDateTimeScheduler.next
2020-06-03 15:07:10 +00:00
*/
suspend fun next(relatively: DateTime = DateTime.now()): DateTime?
2019-10-10 10:43:52 +00:00
}
2021-01-02 15:55:06 +00:00
suspend fun KronScheduler.nextOrRelative(relatively: DateTime = DateTime.now()): DateTime = next(relatively) ?: getAnyNext(relatively)
suspend fun KronScheduler.nextOrNow(): DateTime = DateTime.now().let {
next(it) ?: getAnyNext(it)
}