2019-10-10 08:29:01 +00:00
|
|
|
# krontab
|
|
|
|
|
2019-11-19 13:23:35 +00:00
|
|
|
[ ![Download](https://api.bintray.com/packages/insanusmokrassar/InsanusMokrassar/krontab/images/download.svg) ](https://bintray.com/insanusmokrassar/InsanusMokrassar/krontab/_latestVersion)
|
2019-10-15 06:49:41 +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.
|
|
|
|
|
|
|
|
| Table of content |
|
|
|
|
|---|
|
|
|
|
| [ How to use ](#how-to-use) |
|
|
|
|
| [ How to use: Including in project ](#including-in-project) |
|
|
|
|
| [ How to use: Config from string ](#config-from-string) |
|
|
|
|
| [ How to use: Config via builder (DSL preview) ](#config-via-builder) |
|
2020-01-13 04:34:35 +00:00
|
|
|
| [ How to use: KronScheduler as a Flow ](#KronScheduler-as-a-Flow) |
|
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 */;
|
|
|
|
|
|
|
|
kronScheuler.doWhile {
|
|
|
|
// 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
|
2019-11-19 13:23:35 +00:00
|
|
|
implementation "com.insanusmokrassar:krontab:$krontab_version"
|
2019-10-15 07:07:37 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Next version is the latest currently for the library:
|
|
|
|
|
|
|
|
[ ![Download](https://api.bintray.com/packages/insanusmokrassar/InsanusMokrassar/krontab/images/download.svg) ](https://bintray.com/insanusmokrassar/InsanusMokrassar/krontab/_latestVersion)
|
|
|
|
|
|
|
|
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:
|
|
|
|
```
|
|
|
|
/-------- Seconds
|
|
|
|
| /------ Minutes
|
|
|
|
| | /---- Hours
|
|
|
|
| | | /-- Days of months
|
|
|
|
| | | | / Months
|
|
|
|
| | | | |
|
|
|
|
* * * * *
|
|
|
|
```
|
|
|
|
|
|
|
|
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). In fact, developers will use something
|
|
|
|
like:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
doWhile("/5 * * * *") {
|
|
|
|
println("Called")
|
|
|
|
true // true - repeat on next time
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Or more version:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
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:
|
|
|
|
|
|
|
|
```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
|
|
|
|
|
|
|
### 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()
|
|
|
|
```
|
|
|
|
|
|
|
|
So, in this case any operations related to flow are available and it is expected tt th will work correctly. For example,
|
|
|
|
it is possible to use this flow with `takeWhile`:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
flow.takeWhile {
|
|
|
|
condition()
|
|
|
|
}.collect {
|
|
|
|
action()
|
|
|
|
}
|
|
|
|
```
|