mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-24 11:08:50 +00:00
Compare commits
17 Commits
774d9ecada
...
7aa60e73f1
Author | SHA1 | Date | |
---|---|---|---|
|
7aa60e73f1 | ||
4afebd8b7e | |||
c76ca1798e | |||
58917c6458 | |||
5ef6134d9f | |||
b9237e13c9 | |||
17971ed8fb | |||
505108d2dd | |||
094284562d | |||
5ac1bc26e1 | |||
d82107b8b8 | |||
6b6b7a6fe0 | |||
aff50f0aec | |||
e0f9db9c60 | |||
e4b3a5059a | |||
ca5794726f | |||
72ef9317f9 |
5
.github/workflows/publishing_packages.yml
vendored
5
.github/workflows/publishing_packages.yml
vendored
@ -14,11 +14,6 @@ jobs:
|
||||
sed -i -e "s/^version=\([0-9\.]*\)/version=\1-branch_$branch-build${{ github.run_number }}/" gradle.properties
|
||||
- name: prebuild
|
||||
run: ./gradlew clean build
|
||||
- name: Publish to Gitea
|
||||
continue-on-error: true
|
||||
run: ./gradlew publishAllPublicationsToGiteaRepository
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
- name: Publish package
|
||||
continue-on-error: true
|
||||
run: ./gradlew --no-parallel publishAllPublicationsToGithubPackagesRepository
|
||||
|
16
CHANGELOG.md
16
CHANGELOG.md
@ -1,5 +1,21 @@
|
||||
# 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
|
||||
|
||||
* `Version`:
|
||||
|
@ -9,7 +9,7 @@ android.useAndroidX=true
|
||||
android.enableJetifier=false
|
||||
|
||||
|
||||
kotlin_version=2.0.20
|
||||
kotlin_version=2.0.21
|
||||
kotlin_coroutines_version=1.9.0
|
||||
kotlin_serialization_version=1.7.3
|
||||
|
||||
@ -32,9 +32,9 @@ junit_version=4.12
|
||||
test_ext_junit_version=1.1.3
|
||||
espresso_core=3.4.0
|
||||
|
||||
androidx_work_version=2.9.1
|
||||
androidx_work_version=2.10.0
|
||||
|
||||
## Common
|
||||
|
||||
version=2.5.0
|
||||
android_code_version=42
|
||||
version=2.6.1
|
||||
android_code_version=45
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
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
|
||||
|
@ -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.
|
||||
*
|
||||
* 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!!! 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 {
|
||||
val time = when (this) {
|
||||
is KronSchedulerTz -> nextOrNowWithOffset()
|
||||
else -> nextOrNow().local
|
||||
else -> nextTimeZoned() ?: DateTimeTz.nowLocal()
|
||||
}
|
||||
delay((time - DateTimeTz.nowLocal()).millisecondsLong)
|
||||
return block(time)
|
||||
|
@ -1,5 +1,6 @@
|
||||
package dev.inmo.krontab
|
||||
|
||||
import dev.inmo.krontab.internal.*
|
||||
import dev.inmo.krontab.internal.CronDateTimeScheduler
|
||||
import dev.inmo.krontab.internal.CronDateTimeSchedulerTz
|
||||
import dev.inmo.krontab.internal.createKronScheduler
|
||||
@ -94,27 +95,37 @@ value class KrontabConfig(
|
||||
var dayOfWeekParsed: Array<Byte>? = null
|
||||
var yearParsed: Array<Int>? = null
|
||||
var millisecondsParsed: Array<Short>? = null
|
||||
val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = template.split(" ").also {
|
||||
listOfNotNull(
|
||||
it.getOrNull(5),
|
||||
it.getOrNull(6),
|
||||
it.getOrNull(7),
|
||||
it.getOrNull(8)
|
||||
).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 (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = template
|
||||
.split(" ")
|
||||
.filter { checkIncomingPart(it) } // filter garbage from string
|
||||
.let {
|
||||
if (it.size < 5) { // reconstruction in case of insufficient arguments; 5 is amount of required arguments out of latest also code
|
||||
it + (it.size until 5).map { "*" }
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
.also {
|
||||
listOfNotNull(
|
||||
it.getOrNull(5),
|
||||
it.getOrNull(6),
|
||||
it.getOrNull(7),
|
||||
it.getOrNull(8)
|
||||
).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 minutesParsed = parseMinutes(minutesSource)
|
||||
|
@ -37,6 +37,103 @@ private fun <T> createSimpleScheduler(from: String, dataRange: IntRange, dataCon
|
||||
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 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() }
|
||||
|
@ -2,6 +2,7 @@ package dev.inmo.krontab.utils
|
||||
|
||||
import korlibs.time.*
|
||||
import dev.inmo.krontab.KronSchedulerTz
|
||||
import dev.inmo.krontab.KrontabTemplate
|
||||
import dev.inmo.krontab.buildSchedule
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.takeWhile
|
||||
@ -11,23 +12,52 @@ import kotlin.test.*
|
||||
@ExperimentalCoroutinesApi
|
||||
@FlowPreview
|
||||
class StringParseTest {
|
||||
@Test
|
||||
fun testThatFlowIsCorrectlyWorkEverySecondBuiltOnString() {
|
||||
val kronScheduler = buildSchedule("*/1 * * * *")
|
||||
private fun makeSimpleEverySecondTest(template: KrontabTemplate) {
|
||||
val kronScheduler = buildSchedule(template)
|
||||
|
||||
val flow = kronScheduler.asFlowWithoutDelays()
|
||||
|
||||
runTest {
|
||||
val mustBeCollected = 10
|
||||
var collected = 0
|
||||
var previousTime: DateTime? = null
|
||||
flow.takeWhile {
|
||||
collected < mustBeCollected
|
||||
}.collect {
|
||||
previousTime ?.let { previousTime ->
|
||||
assertEquals(previousTime + 1.seconds, it)
|
||||
}
|
||||
previousTime = it
|
||||
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
|
||||
fun testThatFlowIsCorrectlyWorkEverySecondWhenMillisIsHalfOfSecondBuiltOnString() {
|
||||
val kronScheduler = buildSchedule("*/1 * * * * 500ms")
|
||||
@ -37,9 +67,14 @@ class StringParseTest {
|
||||
runTest {
|
||||
val mustBeCollected = 10
|
||||
var collected = 0
|
||||
var previousTime: DateTime? = null
|
||||
flow.takeWhile {
|
||||
collected < mustBeCollected
|
||||
}.collect {
|
||||
previousTime ?.let { previousTime ->
|
||||
assertEquals(previousTime.copy(milliseconds = 500) + 1.seconds, it)
|
||||
}
|
||||
previousTime = it
|
||||
collected++
|
||||
}
|
||||
assertEquals(mustBeCollected, collected)
|
||||
|
Loading…
Reference in New Issue
Block a user