Merge pull request #2 from InsanusMokrassar/0.2.4

0.2.4
This commit is contained in:
InsanusMokrassar 2020-07-26 00:12:15 +06:00 committed by GitHub
commit 5ccebc5033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 40 deletions

View File

@ -7,6 +7,13 @@
* Coroutines `1.3.2` -> `1.3.3` * Coroutines `1.3.2` -> `1.3.3`
* Klock `1.7.3` -> `1.8.6` * Klock `1.7.3` -> `1.8.6`
### 0.2.4
* Updates in libraries:
* Klock `1.11.3` -> `1.11.14`
* Coroutines `1.3.7` -> `1.3.8`
* Ranges support were included. Now it is possible to correctly use syntax `0-5` in strings schedules
### 0.2.3 ### 0.2.3
* Updates in libraries: * Updates in libraries:

View File

@ -18,7 +18,7 @@ plugins {
id "org.jetbrains.dokka" version "$dokka_version" id "org.jetbrains.dokka" version "$dokka_version"
} }
project.version = "0.2.3" project.version = "0.2.4"
project.group = "com.insanusmokrassar" project.group = "com.insanusmokrassar"
apply from: "publish.gradle" apply from: "publish.gradle"

View File

@ -1,11 +1,11 @@
kotlin.code.style=official kotlin.code.style=official
kotlin_version=1.3.72 kotlin_version=1.3.72
kotlin_coroutines_version=1.3.7 kotlin_coroutines_version=1.3.8
dokka_version=0.10.1 dokka_version=0.10.1
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4
klockVersion=1.11.3 klockVersion=1.11.14
kotlin.incremental.multiplatform=true kotlin.incremental.multiplatform=true

View File

@ -1,6 +1,6 @@
# Tue May 21 17:58:54 HKT 2019 # Fri Jul 24 23:52:00 +06 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip

View File

@ -7,7 +7,7 @@ import kotlinx.coroutines.delay
/** /**
* Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation. * Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
* *
* WARNING!!! If you want to launch it in parallel, you must do this explicit. * WARNING!!! If you want to launch it in parallel, you must do this explicitly.
*/ */
suspend inline fun <T> KronScheduler.doOnce(noinline block: suspend () -> T): T { suspend inline fun <T> KronScheduler.doOnce(noinline block: suspend () -> T): T {
delay((next() - DateTime.now()).millisecondsLong) delay((next() - DateTime.now()).millisecondsLong)
@ -15,12 +15,13 @@ suspend inline fun <T> KronScheduler.doOnce(noinline block: suspend () -> T): T
} }
/** /**
* Will [createSimpleScheduler] using [scheduleConfig] and call [doOnce] on it * Will [buildSchedule] using [scheduleConfig] and call [doOnce] on it
* @see buildSchedule
*/ */
suspend inline fun <T> doOnce( suspend inline fun <T> doOnce(
scheduleConfig: String, scheduleConfig: String,
noinline block: suspend () -> T noinline block: suspend () -> T
) = createSimpleScheduler(scheduleConfig).doOnce(block) ) = buildSchedule(scheduleConfig).doOnce(block)
/** /**
* Will execute [block] while it will return true as a result of its calculation * Will execute [block] while it will return true as a result of its calculation
@ -30,12 +31,14 @@ suspend inline fun KronScheduler.doWhile(noinline block: suspend () -> Boolean)
} }
/** /**
* Will [createSimpleScheduler] using [scheduleConfig] and call [doWhile] with [block] * Will [buildSchedule] using [scheduleConfig] and call [doWhile] with [block]
*
* @see buildSchedule
*/ */
suspend inline fun doWhile( suspend inline fun doWhile(
scheduleConfig: String, scheduleConfig: String,
noinline block: suspend () -> Boolean noinline block: suspend () -> Boolean
) = createSimpleScheduler(scheduleConfig).doWhile(block) ) = buildSchedule(scheduleConfig).doWhile(block)
/** /**
* Will execute [block] without any checking of result * Will execute [block] without any checking of result
@ -45,9 +48,11 @@ suspend inline fun KronScheduler.doInfinity(noinline block: suspend () -> Unit)
true true
} }
/** /**
* Will [createSimpleScheduler] using [scheduleConfig] and call [doInfinity] with [block] * Will [buildSchedule] using [scheduleConfig] and call [doInfinity] with [block]
*
* @see buildSchedule
*/ */
suspend inline fun doInfinity( suspend inline fun doInfinity(
scheduleConfig: String, scheduleConfig: String,
noinline block: suspend () -> Unit noinline block: suspend () -> Unit
) = createSimpleScheduler(scheduleConfig).doInfinity(block) ) = buildSchedule(scheduleConfig).doInfinity(block)

View File

@ -13,7 +13,7 @@ import com.insanusmokrassar.krontab.internal.*
* *
* And each one have next format: * And each one have next format:
* *
* `{number},{number},...` * `{number}[,{number},...]` or `*`
* *
* and {number} here is one of * and {number} here is one of
* *
@ -53,6 +53,6 @@ fun createSimpleScheduler(incoming: String): KronScheduler {
} }
/** /**
* * Shortcut for [createSimpleScheduler]
*/ */
fun buildSchedule(incoming: String): KronScheduler = createSimpleScheduler(incoming) fun buildSchedule(incoming: String): KronScheduler = createSimpleScheduler(incoming)

View File

@ -81,6 +81,16 @@ sealed class TimeBuilder (
*/ */
@Suppress("unused") @Suppress("unused")
infix fun upTo(endIncluding: Int): Array<Int> = this from 0 upTo endIncluding infix fun upTo(endIncluding: Int): Array<Int> = this from 0 upTo endIncluding
/**
* Will fill up this timeline from [this] up to [endIncluding]
*/
@Suppress("MemberVisibilityCanBePrivate")
infix operator fun Int.rangeTo(endIncluding: Int) = upTo(endIncluding)
/**
* Shortcut for "[from] 0 [rangeTo] [endIncluding]"
*/
@Suppress("MemberVisibilityCanBePrivate")
infix operator fun rangeTo(endIncluding: Int) = (this from 0) rangeTo endIncluding
internal fun build() = result ?.map { it.toByte() } ?.toTypedArray() internal fun build() = result ?.map { it.toByte() } ?.toTypedArray()
} }

View File

@ -28,30 +28,6 @@ internal data class CronDateTime(
} }
internal val klockDayOfMonth = dayOfMonth ?.plus(1) internal val klockDayOfMonth = dayOfMonth ?.plus(1)
companion object {
/**
* Using [clamp] extension for checking every parameter to be ensure that they are all correct
* @param month 0-11
* @param dayOfMonth 0-31
* @param hours 0-23
* @param minutes 0-59
* @param seconds 0-59
*/
fun create(
month: Int? = null,
dayOfMonth: Int? = null,
hours: Int? = null,
minutes: Int? = null,
seconds: Int? = null
) = CronDateTime(
month ?.clamp(monthRange) ?.toByte(),
dayOfMonth ?.clamp(dayOfMonthRange) ?.toByte(),
hours ?.clamp(hoursRange) ?.toByte(),
minutes ?.clamp(minutesRange) ?.toByte(),
seconds ?.clamp(secondsRange) ?.toByte()
)
}
} }
/** /**

View File

@ -7,6 +7,10 @@ private fun createSimpleScheduler(from: String, dataRange: IntRange): Array<Byte
val results = things.flatMap { val results = things.flatMap {
when { when {
it.contains("-") -> {
val splitted = it.split("-")
(splitted.first().toInt().clamp(dataRange) .. splitted[1].toInt().clamp(dataRange)).toList()
}
it.contains("/") -> { it.contains("/") -> {
val (start, step) = it.split("/") val (start, step) = it.split("/")
val startNum = (if (start.isEmpty() || start == "*") { val startNum = (if (start.isEmpty() || start == "*") {

View File

@ -1,12 +1,12 @@
package com.insanusmokrassar.krontab.utils package com.insanusmokrassar.krontab.utils
import com.insanusmokrassar.krontab.buildSchedule import com.insanusmokrassar.krontab.buildSchedule
import com.insanusmokrassar.krontab.builder.buildSchedule
import com.insanusmokrassar.krontab.createSimpleScheduler
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.flow.takeWhile
import kotlin.math.max
import kotlin.math.min
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -60,4 +60,23 @@ class StringParseTest {
} }
} }
} }
@Test
fun testThatFlowIsCorrectlyWorkEverySeveralSecondsRangeBuiltOnString() {
val rangesEnds = listOf(0 to 5, 30 to 35)
val kronScheduler = buildSchedule("${rangesEnds.joinToString(",") { "${it.first}-${it.second}" }} * * * *")
val flow = kronScheduler.asFlow()
runTest {
val ranges = rangesEnds.map { it.first .. it.second }.flatten().toMutableList()
val expectedCollects = rangesEnds.sumBy { it.second - it.first + 1 }
var collected = 0
flow.takeWhile { ranges.isNotEmpty() }.collect {
ranges.remove(it.seconds)
collected++
}
assertEquals(expectedCollects, collected)
}
}
} }