shyiko / skedule

A human-friendly alternative to cron. Designed after GAE's schedule for Kotlin and/or Java 8+.
84 stars 7 forks source link

Recurring execution of Schedule's #7

Open LarsArtmann opened 3 years ago

LarsArtmann commented 3 years ago

I think a method like this would be quite helpful:

fun Schedule.schedule(executor: ThreadPoolExecutor, code: () -> Unit) {
    val now = ZonedDateTime.now()
    val next = this.next(
        now
            .minusSeconds(now.second.toLong())
            .minusNanos(now.nano.toLong())
    )
    val delay = next.toEpochSecond() - now.toEpochSecond()
    executor.schedule(
        {
            this.schedule(code)
            code()
        },
        delay,
        TimeUnit.SECONDS
    )
}

because the example in the README.md is not recurring

Zomis commented 3 years ago

I don't like the idea of mixing in ThreadPoolExecutor (which is a Java class), you could use a Kotlin coroutine instead