krontab/src/commonMain/kotlin/com/insanusmokrassar/krontab/StringParser.kt

72 lines
2.0 KiB
Kotlin
Raw Normal View History

package com.insanusmokrassar.krontab
2019-10-05 05:09:22 +00:00
import com.insanusmokrassar.krontab.internal.*
2019-10-05 05:09:22 +00:00
2020-08-22 15:42:47 +00:00
/**
* @see createSimpleScheduler
* @see buildSchedule
*/
typealias KrontabTemplate = String
2019-10-10 10:43:52 +00:00
/**
* Parse [incoming] string and adapt according to next format: "* * * * *" where order of things:
*
2020-06-03 14:41:54 +00:00
* * seconds
* * minutes
* * hours
* * dayOfMonth
* * month
2019-10-10 10:43:52 +00:00
*
* And each one have next format:
*
2020-07-24 08:18:18 +00:00
* `{number}[,{number},...]` or `*`
2019-10-10 10:43:52 +00:00
*
2020-06-03 14:41:54 +00:00
* and {number} here is one of
2019-10-10 10:43:52 +00:00
*
2020-06-03 14:41:54 +00:00
* * {int}-{int}
* * {int}/{int}
* * */{int}
* * {int}
2020-10-10 15:12:32 +00:00
* * F
* * L
2019-10-10 10:43:52 +00:00
*
2020-06-03 14:41:54 +00:00
* Additional info about ranges can be found in follow accordance:
*
* * Seconds ranges can be found in [secondsRange]
* * Minutes ranges can be found in [minutesRange]
* * Hours ranges can be found in [hoursRange]
* * Days of month ranges can be found in [dayOfMonthRange]
* * Months ranges can be found in [monthRange]
*
* Examples:
*
* * "0/5 * * * *" for every five seconds triggering
2020-10-10 15:12:32 +00:00
* * "0/5,L * * * *" for every five seconds triggering and on 59 second
2020-06-03 14:41:54 +00:00
* * "0/15 30 * * *" for every 15th seconds in a half of each hour
2020-10-10 15:12:32 +00:00
* * "1 2 3 F,4,L 5" for triggering in near first second of second minute of third hour of fourth day of may
*
* @see com.insanusmokrassar.krontab.internal.createKronScheduler
2019-10-10 10:43:52 +00:00
*/
2020-08-22 15:42:47 +00:00
fun createSimpleScheduler(incoming: KrontabTemplate): KronScheduler {
2019-10-05 10:40:51 +00:00
val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = incoming.split(" ")
val secondsParsed = parseSeconds(secondsSource)
val minutesParsed = parseMinutes(minutesSource)
val hoursParsed = parseHours(hoursSource)
val dayOfMonthParsed = parseDaysOfMonth(dayOfMonthSource)
val monthParsed = parseMonths(monthSource)
return createKronScheduler(
secondsParsed, minutesParsed, hoursParsed, dayOfMonthParsed, monthParsed
)
}
/**
2020-07-24 08:18:18 +00:00
* Shortcut for [createSimpleScheduler]
*/
2020-08-22 15:42:47 +00:00
fun buildSchedule(incoming: KrontabTemplate): KronScheduler = createSimpleScheduler(incoming)
/**
* Shortcut for [buildSchedule]
*/
fun KrontabTemplate.toSchedule(): KronScheduler = buildSchedule(this)