2019-10-10 08:29:01 +00:00
|
|
|
# krontab
|
|
|
|
|
2023-02-19 10:35:31 +00:00
|
|
|
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/krontab/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/krontab)
|
|
|
|
|
|
|
|
[![Telegram Chat](https://img.shields.io/badge/Telegram%20Chat-0288D1?style=for-the-badge&logo=telegram&logoColor=white)](https://inmodev_chat.t.me)
|
|
|
|
|
|
|
|
![JVM](https://img.shields.io/badge/JVM-red?style=for-the-badge&logo=openjdk&logoColor=white)
|
2022-06-15 09:18:41 +00:00
|
|
|
![Android](https://img.shields.io/badge/Android-green?style=for-the-badge&logo=android&logoColor=white)
|
|
|
|
![Js](https://img.shields.io/badge/JavaScript-323330?style=for-the-badge&logo=javascript&logoColor=F7DF1E)
|
2023-04-03 10:18:18 +00:00
|
|
|
![Linux x64](https://img.shields.io/badge/Linux%20x64-white?style=for-the-badge&logo=linux&logoColor=black)
|
2022-06-15 09:18:41 +00:00
|
|
|
|
2022-07-22 11:08:09 +00:00
|
|
|
[![KDocs](https://img.shields.io/badge/KDocs-323330?style=for-the-badge&logo=Kotlin&logoColor=7F52FF)](https://insanusmokrassar.github.io/krontab/)
|
2023-06-10 12:16:53 +00:00
|
|
|
[![Tutorials](https://img.shields.io/badge/Tutorials-0288D1?style=for-the-badge&logo=mkdocs&logoColor=white)](https://docs.inmo.dev/krontab/index.html)
|
2023-02-19 10:35:31 +00:00
|
|
|
|
2019-10-15 07:07:37 +00:00
|
|
|
Library was created to give oppotunity to launch some things from time to time according to some schedule in
|
|
|
|
runtime of applications.
|
|
|
|
|
2019-10-15 06:49:41 +00:00
|
|
|
## 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:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
val kronScheduler = /* creating of KronScheduler instance */;
|
|
|
|
|
2023-03-16 12:16:35 +00:00
|
|
|
kronScheduler.doWhile {
|
2019-10-15 06:49:41 +00:00
|
|
|
// some action
|
|
|
|
true // true - repeat on next time
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-10-15 07:07:37 +00:00
|
|
|
### Including in project
|
|
|
|
|
2019-11-19 13:23:35 +00:00
|
|
|
If you want to include `krontab` in your project, just add next line to your
|
|
|
|
dependencies part:
|
2019-10-15 07:07:37 +00:00
|
|
|
|
|
|
|
```groovy
|
2020-11-21 09:00:30 +00:00
|
|
|
implementation "dev.inmo:krontab:$krontab_version"
|
2019-10-15 07:07:37 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Next version is the latest currently for the library:
|
|
|
|
|
2021-07-28 05:29:31 +00:00
|
|
|
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/krontab/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/krontab)
|
2019-10-15 07:07:37 +00:00
|
|
|
|
|
|
|
For old version of Gradle, instead of `implementation` word developers must use `compile`.
|
|
|
|
|
2019-10-15 06:49:41 +00:00
|
|
|
### 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:
|
2021-04-22 06:32:45 +00:00
|
|
|
|
2019-10-15 06:49:41 +00:00
|
|
|
```
|
2021-04-22 06:32:45 +00:00
|
|
|
/--------------- Seconds
|
|
|
|
| /------------- Minutes
|
|
|
|
| | /----------- Hours
|
|
|
|
| | | /--------- Days of months
|
|
|
|
| | | | /------- Months
|
|
|
|
| | | | | /----- (optional) Year
|
|
|
|
| | | | | | /--- (optional) Timezone offset
|
|
|
|
| | | | | | | / (optional) Week days
|
|
|
|
* * * * * * 0o *w
|
2019-10-15 06:49:41 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
It is different with original `crontab` syntax for the reason, that expected that in practice developers
|
2021-01-02 15:35:08 +00:00
|
|
|
will use seconds and minutes with more probability than months (for example) or even years. In fact, developers will use
|
|
|
|
something like:
|
2019-10-15 06:49:41 +00:00
|
|
|
|
|
|
|
```kotlin
|
|
|
|
doWhile("/5 * * * *") {
|
|
|
|
println("Called")
|
|
|
|
true // true - repeat on next time
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-02 15:35:08 +00:00
|
|
|
An other version:
|
2019-10-15 06:49:41 +00:00
|
|
|
|
|
|
|
```kotlin
|
|
|
|
doInfinity("/5 * * * *") {
|
|
|
|
println("Called")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Both of examples will print `Called` message every five seconds.
|
|
|
|
|
|
|
|
### Config via builder
|
|
|
|
|
2021-01-02 15:35:08 +00:00
|
|
|
Also, this library currently supports DSL for creating the same goals:
|
2019-10-15 06:49:41 +00:00
|
|
|
|
|
|
|
```kotlin
|
|
|
|
val kronScheduler = buildSchedule {
|
|
|
|
seconds {
|
|
|
|
from (0) every 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kronScheduler.doWhile {
|
|
|
|
println("Called")
|
|
|
|
true // true - repeat on next time
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Or
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
val kronScheduler = buildSchedule {
|
|
|
|
seconds {
|
|
|
|
0 every 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kronScheduler.doWhile {
|
|
|
|
println("Called")
|
|
|
|
true // true - repeat on next time
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Or
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
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.
|
2020-01-13 04:34:35 +00:00
|
|
|
|
2021-09-26 07:05:59 +00:00
|
|
|
### 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:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
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`:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
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.
|
|
|
|
|
2020-01-13 04:34:35 +00:00
|
|
|
### KronScheduler as a Flow
|
|
|
|
|
|
|
|
Any `KronScheduler`can e converted to a `Flow<DateTime` using extension `asFlow`:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
val kronScheduler = buildSchedule {
|
|
|
|
seconds {
|
|
|
|
0 every 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val flow = kronScheduler.asFlow()
|
|
|
|
```
|
|
|
|
|
2020-01-14 08:53:36 +00:00
|
|
|
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`:
|
2020-01-13 04:34:35 +00:00
|
|
|
|
|
|
|
```kotlin
|
|
|
|
flow.takeWhile {
|
|
|
|
condition()
|
|
|
|
}.collect {
|
|
|
|
action()
|
|
|
|
}
|
|
|
|
```
|
2021-04-22 06:32:45 +00:00
|
|
|
|
2021-04-22 06:44:22 +00:00
|
|
|
### 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
|
2021-09-26 07:05:59 +00:00
|
|
|
scheduler is not instance of `KronSchedulerTz` it will work like you passed just `DateTime`.
|
2021-04-22 06:44:22 +00:00
|
|
|
|
|
|
|
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`
|
|
|
|
|
2021-04-22 06:32:45 +00:00
|
|
|
### 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`
|