Library for using Crontab-like syntax in scheduling of some Kotlin Coroutines tasks to do from time to time
Go to file
InsanusMokrassar 93d38f8945
Update CHANGELOG.md
2022-10-02 22:26:24 +06:00
.github workflows fix 2022-02-27 20:47:10 +06:00
gradle/wrapper Update gradle-wrapper.properties 2022-10-02 22:21:23 +06:00
kotlin-js-store update yarn.lock 2022-07-22 17:03:20 +06:00
src Update NearDateTimeCalculator.kt 2022-06-15 14:46:12 +06:00
.gitignore updates 2019-10-08 23:42:50 +06:00
CHANGELOG.md Update CHANGELOG.md 2022-10-02 22:26:24 +06:00
LICENSE Initial commit 2019-05-21 11:35:07 +08:00
README.md change kdocs link 2022-07-22 17:09:00 +06:00
_config.yml Add '_config.yml' 2020-02-07 11:21:05 +00:00
build.gradle revert android sdk version 2022-02-27 20:37:48 +06:00
changelog_parser.sh add changelog and github release tools 2020-10-08 15:26:07 +06:00
dokka.gradle update kdocs script 2021-08-07 16:25:35 +06:00
github_release.gradle Update github_release.gradle 2022-07-02 18:38:03 +06:00
gradle.properties Update gradle.properties 2022-10-02 22:22:04 +06:00
gradlew init 2019-05-21 18:14:09 +08:00
gradlew.bat init 2019-05-21 18:14:09 +08:00
mpp_config.kpsb update publishing scripts 2021-03-13 20:30:02 +06:00
publish.gradle update publishing scripts 2021-03-13 20:30:02 +06:00
settings.gradle 0.2.0 2020-01-08 14:28:57 +06:00

README.md

krontab

Maven Central Build Status

JVM Android Js

KDocs Tutorials

Library was created to give oppotunity to launch some things from time to time according to some schedule in runtime of applications.

How to use

There are several ways to configure and use this library:

  • From some string
  • From builder

Anyway, to start some action from time to time you will need to use one of extensions/functions:

val kronScheduler = /* creating of KronScheduler instance */;

kronScheuler.doWhile {
    // some action
    true // true - repeat on next time
}

Including in project

If you want to include krontab in your project, just add next line to your dependencies part:

implementation "dev.inmo:krontab:$krontab_version"

Next version is the latest currently for the library:

Maven Central

For old version of Gradle, instead of implementation word developers must use compile.

Config from string

Developers can use more simple way to configure repeat times is string. String configuring like a crontab, but with a little bit different meanings:

/--------------- Seconds
| /------------- Minutes
| | /----------- Hours
| | | /--------- Days of months
| | | | /------- Months
| | | | | /----- (optional) Year
| | | | | | /--- (optional) Timezone offset
| | | | | | |  / (optional) Week days
* * * * * * 0o *w

It is different with original crontab syntax for the reason, that expected that in practice developers will use seconds and minutes with more probability than months (for example) or even years. In fact, developers will use something like:

doWhile("/5 * * * *") {
    println("Called")
    true // true - repeat on next time
}

An other version:

doInfinity("/5 * * * *") {
    println("Called")
}

Both of examples will print Called message every five seconds.

Config via builder

Also, this library currently supports DSL for creating the same goals:

val kronScheduler = buildSchedule {
    seconds {
        from (0) every 5
    }
}
kronScheduler.doWhile {
    println("Called")
    true // true - repeat on next time
}

Or

val kronScheduler = buildSchedule {
    seconds {
        0 every 5
    }
}
kronScheduler.doWhile {
    println("Called")
    true // true - repeat on next time
}

Or

val kronScheduler = buildSchedule {
    seconds {
        0 every 5
    }
}
kronScheduler.doInfinity {
    println("Called")
}

All of these examples will do the same things: print Called message every five seconds.

do* functions

With regular doOnce/doWhile/doInfinity there are two types of their variations: local and timezoned. Local variations (doOnceLocal/doWhileLocal/doInfinityLocal) will pass DateTime as an argument into the block:

doInfinityLocal("/5 * * * *") {
    println(it) // will print current date time
}

Timezoned variations (doOnceTz/doWhileTz/doInfinityTz) will do the same thing but pass as an argument DateTimeTz:

doInfinityTz("/5 * * * * 0o") {
    println(it) // will print current date time in UTC
}

It is useful in cases when you need to get the time of calling and avoid extra calls to system time.

Helpful table for

No args Local DateTime Local DateTimeTz with offset of KronScheduler
Call only near time doOnce doOnceLocal doOnceTz
Call while condition is true doWhile doWhileLocal doWhileTz
Work infinity* doInfinity doInfinityLocal doInfinityTz

*Here there is an important notice, that Work infinity is not exactly infinity. Actually, that means that do while coroutine is alive and in fact executing will be stopped when coroutine became cancelled.

KronScheduler as a Flow

Any KronSchedulercan e converted to a Flow<DateTime using extension asFlow:

val kronScheduler = buildSchedule {
    seconds {
        0 every 1
    }
}

val flow = kronScheduler.asFlow()

So, in this case any operations related to flow are available and it is expected that they will work correctly. For example, it is possible to use this flow with takeWhile:

flow.takeWhile {
    condition()
}.collect {
    action()
}

Offsets

Offsets in this library works via passing parameter ending with o in any place after month config. Currently there is only one format supported for offsets: minutes of offsets. To use time zones you will need to call next method with DateTimeTz argument or nextTimeZoned method with any KronScheduler instance, but in case if this scheduler is not instance of KronSchedulerTz it will work like you passed just DateTime.

Besides, in case you wish to use time zones explicitly, you will need to get KronSchedulerTz. It is possible by:

  • Using createSimpleScheduler/buildSchedule/KrontabTemplate#toSchedule/KrontabTemplate#toKronScheduler methods with passing defaultOffset parameter
  • Using SchedulerBuilder#build/createSimpleScheduler/buildSchedule/KrontabTemplate#toSchedule/KrontabTemplate#toKronScheduler methods with casting to KronSchedulerTz in case you are pretty sure that it is timezoned KronScheduler
  • Creating your own implementation of KronSchedulerTz

Note about week days

Unlike original CRON, here week days:

  • Works as AND: cron date time will search first day which will pass requirement according all parameters including week days
  • You may use any related to numbers syntax with week days: 0-3w, 0,1,2,3w, etc.
  • Week days (like years and offsets) are optional and can be placed anywhere after month