Library for using Crontab-like syntax in scheduling of some Kotlin Coroutines tasks to do from time to time
Go to file
2021-03-13 21:34:56 +06:00
.github Update publishing_packages.yml 2021-03-13 21:34:56 +06:00
gradle/wrapper update gradle wrapper 2021-03-13 20:23:15 +06:00
src fixed issues 2021-01-03 12:45:38 +06:00
_config.yml Add '_config.yml' 2020-02-07 11:21:05 +00:00
.gitignore updates 2019-10-08 23:42:50 +06:00
.travis.yml version 0.2.2 2020-03-22 18:35:06 +06:00
build.gradle fix tests issue 2021-01-02 22:56:35 +06:00
changelog_parser.sh add changelog and github release tools 2020-10-08 15:26:07 +06:00
CHANGELOG.md update dependencies 2021-03-13 20:22:55 +06:00
dokka.gradle update module name in dokka 2020-12-07 14:24:41 +06:00
github_release.gradle fix in github_release 2020-10-08 18:17:46 +06:00
gradle.properties update dependencies 2021-03-13 20:22:55 +06:00
gradlew init 2019-05-21 18:14:09 +08:00
gradlew.bat init 2019-05-21 18:14:09 +08:00
LICENSE Initial commit 2019-05-21 11:35:07 +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
README.md add years, KrontabWrapper, new function to create scheduler from template and update readme 2021-01-02 21:35:08 +06:00
settings.gradle 0.2.0 2020-01-08 14:28:57 +06:00

krontab

Download Maven Central Build Status

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: Including in project
How to use: Config from string
How to use: Config via builder (DSL preview)
How to use: KronScheduler as a Flow

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:

Download

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
* * * * * *

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.

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()
}