Compare commits

...

17 Commits

Author SHA1 Message Date
renovate[bot]
7aa60e73f1
Merge 20c052124b into 4afebd8b7e 2024-11-22 01:37:11 +00:00
4afebd8b7e
Merge pull request #131 from InsanusMokrassar/2.6.1
2.6.1
2024-11-06 08:50:36 +06:00
c76ca1798e fixes in parsing of ktontab templates 2024-11-06 08:46:10 +06:00
58917c6458 start 2.6.1 2024-11-06 07:38:24 +06:00
5ef6134d9f
Merge pull request #129 from InsanusMokrassar/2.6.0
2.6.0
2024-11-03 21:52:59 +06:00
b9237e13c9 fix changelog :) 2024-11-03 17:41:13 +06:00
17971ed8fb now doOnceTz works as it must 2024-11-03 17:40:42 +06:00
505108d2dd start 2.6.0 2024-11-03 17:40:12 +06:00
094284562d
Merge pull request #128 from InsanusMokrassar/2.5.1
2.5.1
2024-11-03 13:38:07 +06:00
5ac1bc26e1 rewrite check onto fsm 2024-11-03 13:30:20 +06:00
d82107b8b8 remove gitea publishing step from workflows 2024-11-03 12:59:35 +06:00
6b6b7a6fe0 remove redundant regexes 2024-11-03 12:58:26 +06:00
aff50f0aec get back splitting by simple ' ' instead of regex in KrontabConfig 2024-11-03 12:57:32 +06:00
e0f9db9c60 fix of changelog 2024-11-03 12:56:10 +06:00
e4b3a5059a fix of #126 2024-11-03 12:54:51 +06:00
ca5794726f update dependencies 2024-11-03 12:27:43 +06:00
72ef9317f9 start 2.5.1 2024-11-03 12:23:23 +06:00
8 changed files with 188 additions and 32 deletions

View File

@ -14,11 +14,6 @@ jobs:
sed -i -e "s/^version=\([0-9\.]*\)/version=\1-branch_$branch-build${{ github.run_number }}/" gradle.properties sed -i -e "s/^version=\([0-9\.]*\)/version=\1-branch_$branch-build${{ github.run_number }}/" gradle.properties
- name: prebuild - name: prebuild
run: ./gradlew clean build run: ./gradlew clean build
- name: Publish to Gitea
continue-on-error: true
run: ./gradlew publishAllPublicationsToGiteaRepository
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
- name: Publish package - name: Publish package
continue-on-error: true continue-on-error: true
run: ./gradlew --no-parallel publishAllPublicationsToGithubPackagesRepository run: ./gradlew --no-parallel publishAllPublicationsToGithubPackagesRepository

View File

@ -1,5 +1,21 @@
# Changelog # Changelog
## 2.6.1
* Fixes in parsing of string parts
## 2.6.0
* Fix of `doOnceTz` behaviour: now it will use local time as utc time to get next waking up time
## 2.5.1
* Add cleaning up of incoming template, which must remove all malformed parts from string template (fix of [#126](https://github.com/InsanusMokrassar/krontab/issues/126))
* Add support of insufficient amount of arguments (fix of [#126](https://github.com/InsanusMokrassar/krontab/issues/126))
* `Version`:
* `Kotlin`: `2.0.21`
* `AndroidXWork`: `2.10.0`
## 2.5.0 ## 2.5.0
* `Version`: * `Version`:

View File

@ -9,7 +9,7 @@ android.useAndroidX=true
android.enableJetifier=false android.enableJetifier=false
kotlin_version=2.0.20 kotlin_version=2.0.21
kotlin_coroutines_version=1.9.0 kotlin_coroutines_version=1.9.0
kotlin_serialization_version=1.7.3 kotlin_serialization_version=1.7.3
@ -32,9 +32,9 @@ junit_version=4.12
test_ext_junit_version=1.1.3 test_ext_junit_version=1.1.3
espresso_core=3.4.0 espresso_core=3.4.0
androidx_work_version=2.9.1 androidx_work_version=2.10.0
## Common ## Common
version=2.5.0 version=2.6.1
android_code_version=42 android_code_version=45

View File

@ -3,4 +3,4 @@ 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-8.9-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip

View File

@ -35,6 +35,8 @@ suspend inline fun <T> KronScheduler.doOnceLocal(block: (DateTime) -> T): T = do
/** /**
* 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!!! This method will use local time instead of utc
*
* WARNING!!! If you want to launch it in parallel, you must do this explicitly. * WARNING!!! If you want to launch it in parallel, you must do this explicitly.
* *
* WARNING!!! In case if [KronScheduler.next] of [this] instance will return null, [block] will be called immediatelly * WARNING!!! In case if [KronScheduler.next] of [this] instance will return null, [block] will be called immediatelly
@ -42,7 +44,7 @@ suspend inline fun <T> KronScheduler.doOnceLocal(block: (DateTime) -> T): T = do
suspend inline fun <T> KronScheduler.doOnceTz(noinline block: suspend (DateTimeTz) -> T): T { suspend inline fun <T> KronScheduler.doOnceTz(noinline block: suspend (DateTimeTz) -> T): T {
val time = when (this) { val time = when (this) {
is KronSchedulerTz -> nextOrNowWithOffset() is KronSchedulerTz -> nextOrNowWithOffset()
else -> nextOrNow().local else -> nextTimeZoned() ?: DateTimeTz.nowLocal()
} }
delay((time - DateTimeTz.nowLocal()).millisecondsLong) delay((time - DateTimeTz.nowLocal()).millisecondsLong)
return block(time) return block(time)

View File

@ -1,5 +1,6 @@
package dev.inmo.krontab package dev.inmo.krontab
import dev.inmo.krontab.internal.*
import dev.inmo.krontab.internal.CronDateTimeScheduler import dev.inmo.krontab.internal.CronDateTimeScheduler
import dev.inmo.krontab.internal.CronDateTimeSchedulerTz import dev.inmo.krontab.internal.CronDateTimeSchedulerTz
import dev.inmo.krontab.internal.createKronScheduler import dev.inmo.krontab.internal.createKronScheduler
@ -94,27 +95,37 @@ value class KrontabConfig(
var dayOfWeekParsed: Array<Byte>? = null var dayOfWeekParsed: Array<Byte>? = null
var yearParsed: Array<Int>? = null var yearParsed: Array<Int>? = null
var millisecondsParsed: Array<Short>? = null var millisecondsParsed: Array<Short>? = null
val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = template.split(" ").also { val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = template
listOfNotNull( .split(" ")
it.getOrNull(5), .filter { checkIncomingPart(it) } // filter garbage from string
it.getOrNull(6), .let {
it.getOrNull(7), if (it.size < 5) { // reconstruction in case of insufficient arguments; 5 is amount of required arguments out of latest also code
it.getOrNull(8) it + (it.size until 5).map { "*" }
).forEach { } else {
val offsetFromString = parseOffset(it) it
val dayOfWeekFromString = parseWeekDay(it) }
val millisecondsFromString = parseMilliseconds(it) }
offsetParsed = offsetParsed ?: offsetFromString .also {
dayOfWeekParsed = dayOfWeekParsed ?: dayOfWeekFromString listOfNotNull(
millisecondsParsed = millisecondsParsed ?: millisecondsFromString it.getOrNull(5),
when { it.getOrNull(6),
dayOfWeekFromString != null || offsetFromString != null || millisecondsFromString != null -> return@forEach it.getOrNull(7),
yearParsed == null -> { it.getOrNull(8)
yearParsed = parseYears(it) ).forEach {
val offsetFromString = parseOffset(it)
val dayOfWeekFromString = parseWeekDay(it)
val millisecondsFromString = parseMilliseconds(it)
offsetParsed = offsetParsed ?: offsetFromString
dayOfWeekParsed = dayOfWeekParsed ?: dayOfWeekFromString
millisecondsParsed = millisecondsParsed ?: millisecondsFromString
when {
dayOfWeekFromString != null || offsetFromString != null || millisecondsFromString != null -> return@forEach
yearParsed == null -> {
yearParsed = parseYears(it)
}
} }
} }
} }
}
val secondsParsed = parseSeconds(secondsSource) val secondsParsed = parseSeconds(secondsSource)
val minutesParsed = parseMinutes(minutesSource) val minutesParsed = parseMinutes(minutesSource)

View File

@ -37,6 +37,103 @@ private fun <T> createSimpleScheduler(from: String, dataRange: IntRange, dataCon
return results.map(dataConverter) return results.map(dataConverter)
} }
/**
* FSM for parsing of incoming data. If at the end of parsing it have non-null state and string is not empty, data passed check
*
* 1.
* * "\\d" -> 2
* * "\\*" -> 4
* * "f" -> 7
* * "l" -> 7
* * "/" -> 6
* 2.
* * "\\d" -> 2
* * "/" -> 6
* * "," -> 1
* * "-" -> 3
* * "m" -> 9
* * "o" -> 10
* * "w" -> 10
* 3.
* * "l" -> 7
* * "\\d" -> 8
* 4.
* * "/" -> 6
* * "," -> 1
* 5.
* * "/" -> 6
* 6.
* * "\\d" -> 8
* * "\\*" -> 7
* 7.
* * "," -> 1
* 8.
* * "\\d" -> 8
* * "," -> 1
* 9.
* * "s" -> 10 // end of ms
* 10. Empty, end of parse
*/
private val checkIncomingPartTransitionsMap = listOf(
listOf( // 0
Regex("\\d") to 1,
Regex("\\*") to 3,
Regex("f") to 6,
Regex("l") to 6,
Regex("/") to 5,
),
listOf( // 1
Regex("\\d") to 1,
Regex("/") to 5,
Regex(",") to 0,
Regex("-") to 2,
Regex("m") to 8,
Regex("o") to 9,
Regex("w") to 9,
),
listOf( // 2
Regex("l") to 6,
Regex("\\d") to 7,
),
listOf( // 3
Regex("/") to 5,
Regex(",") to 0,
),
listOf( // 4
Regex("/") to 5,
),
listOf( // 5
Regex("\\d") to 7,
Regex("\\*") to 6,
),
listOf( // 6
Regex(",") to 0,
),
listOf( // 7
Regex("\\d") to 7,
Regex(",") to 0,
),
listOf( // 8
Regex("s") to 9, // end of ms
),
listOf(), // empty state, end of parsing
)
internal fun checkIncomingPart(part: String): Boolean {
var i = 0
var state = checkIncomingPartTransitionsMap[0]
while (i < part.length) {
val char = part[i]
val nextState = state.firstNotNullOfOrNull {
it.second.takeIf { _ -> it.first.matches("$char") }
}
if (nextState == null) return false
state = checkIncomingPartTransitionsMap[nextState]
i++
}
return part.isNotEmpty()
}
internal fun parseWeekDay(from: String?) = from ?.let { if (it.endsWith("w")) createSimpleScheduler(it.removeSuffix("w"), dayOfWeekRange, intToByteConverter) ?.toTypedArray() else null } internal fun parseWeekDay(from: String?) = from ?.let { if (it.endsWith("w")) createSimpleScheduler(it.removeSuffix("w"), dayOfWeekRange, intToByteConverter) ?.toTypedArray() else null }
internal fun parseOffset(from: String?) = from ?.let { if (it.endsWith("o")) it.removeSuffix("o").toIntOrNull() else null } internal fun parseOffset(from: String?) = from ?.let { if (it.endsWith("o")) it.removeSuffix("o").toIntOrNull() else null }
internal fun parseYears(from: String?) = from ?.let { createSimpleScheduler(from, yearRange, intToIntConverter) ?.toTypedArray() } internal fun parseYears(from: String?) = from ?.let { createSimpleScheduler(from, yearRange, intToIntConverter) ?.toTypedArray() }

View File

@ -2,6 +2,7 @@ package dev.inmo.krontab.utils
import korlibs.time.* import korlibs.time.*
import dev.inmo.krontab.KronSchedulerTz import dev.inmo.krontab.KronSchedulerTz
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.buildSchedule import dev.inmo.krontab.buildSchedule
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.flow.takeWhile
@ -11,23 +12,52 @@ import kotlin.test.*
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
@FlowPreview @FlowPreview
class StringParseTest { class StringParseTest {
@Test private fun makeSimpleEverySecondTest(template: KrontabTemplate) {
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnString() { val kronScheduler = buildSchedule(template)
val kronScheduler = buildSchedule("*/1 * * * *")
val flow = kronScheduler.asFlowWithoutDelays() val flow = kronScheduler.asFlowWithoutDelays()
runTest { runTest {
val mustBeCollected = 10 val mustBeCollected = 10
var collected = 0 var collected = 0
var previousTime: DateTime? = null
flow.takeWhile { flow.takeWhile {
collected < mustBeCollected collected < mustBeCollected
}.collect { }.collect {
previousTime ?.let { previousTime ->
assertEquals(previousTime + 1.seconds, it)
}
previousTime = it
collected++ collected++
} }
assertEquals(mustBeCollected, collected) assertEquals(mustBeCollected, collected)
} }
} }
@Test
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnString() {
makeSimpleEverySecondTest("*/1 * * * *")
}
@Test
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnStringWithWrongAmountOfSpaces() {
val templatesFirstReplacers = listOf(
"*/1",
"*",
"/1",
"f,/1",
)
templatesFirstReplacers.forEach { replacer ->
makeSimpleEverySecondTest("$replacer * * * * ")
}
}
@Test
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnStringWithGarbageInTemplate() {
makeSimpleEverySecondTest(" sdf */1 * * * oo * ")
}
@Test
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnStringWithInsufficientArgsInTemplate() {
makeSimpleEverySecondTest(" sdf */1 ")
}
@Test @Test
fun testThatFlowIsCorrectlyWorkEverySecondWhenMillisIsHalfOfSecondBuiltOnString() { fun testThatFlowIsCorrectlyWorkEverySecondWhenMillisIsHalfOfSecondBuiltOnString() {
val kronScheduler = buildSchedule("*/1 * * * * 500ms") val kronScheduler = buildSchedule("*/1 * * * * 500ms")
@ -37,9 +67,14 @@ class StringParseTest {
runTest { runTest {
val mustBeCollected = 10 val mustBeCollected = 10
var collected = 0 var collected = 0
var previousTime: DateTime? = null
flow.takeWhile { flow.takeWhile {
collected < mustBeCollected collected < mustBeCollected
}.collect { }.collect {
previousTime ?.let { previousTime ->
assertEquals(previousTime.copy(milliseconds = 500) + 1.seconds, it)
}
previousTime = it
collected++ collected++
} }
assertEquals(mustBeCollected, collected) assertEquals(mustBeCollected, collected)