mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-23 02:28:52 +00:00
fill documentation and optimize SchedulerBuilder#build and createSimpleScheduler functions
This commit is contained in:
parent
408349be04
commit
6077384a17
@ -35,6 +35,8 @@ import com.insanusmokrassar.krontab.internal.*
|
|||||||
* * "0/5 * * * *" for every five seconds triggering
|
* * "0/5 * * * *" for every five seconds triggering
|
||||||
* * "0/15 30 * * *" for every 15th seconds in a half of each hour
|
* * "0/15 30 * * *" for every 15th seconds in a half of each hour
|
||||||
* * "1 2 3 4 5" for triggering in near first second of second minute of third hour of fourth day of may
|
* * "1 2 3 4 5" for triggering in near first second of second minute of third hour of fourth day of may
|
||||||
|
*
|
||||||
|
* @see com.insanusmokrassar.krontab.internal.createKronScheduler
|
||||||
*/
|
*/
|
||||||
fun createSimpleScheduler(incoming: String): KronScheduler {
|
fun createSimpleScheduler(incoming: String): KronScheduler {
|
||||||
val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = incoming.split(" ")
|
val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = incoming.split(" ")
|
||||||
@ -45,27 +47,7 @@ fun createSimpleScheduler(incoming: String): KronScheduler {
|
|||||||
val dayOfMonthParsed = parseDaysOfMonth(dayOfMonthSource)
|
val dayOfMonthParsed = parseDaysOfMonth(dayOfMonthSource)
|
||||||
val monthParsed = parseMonths(monthSource)
|
val monthParsed = parseMonths(monthSource)
|
||||||
|
|
||||||
val resultCronDateTimes = mutableListOf(CronDateTime())
|
return createKronScheduler(
|
||||||
|
secondsParsed, minutesParsed, hoursParsed, dayOfMonthParsed, monthParsed
|
||||||
secondsParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
)
|
||||||
previousCronDateTime.copy(seconds = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
minutesParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(minutes = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
hoursParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(hours = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
dayOfMonthParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(dayOfMonth = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
monthParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(month = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
return CronDateTimeScheduler(resultCronDateTimes.toList())
|
|
||||||
}
|
}
|
@ -1,10 +1,16 @@
|
|||||||
package com.insanusmokrassar.krontab.builder
|
package com.insanusmokrassar.krontab.builder
|
||||||
|
|
||||||
import com.insanusmokrassar.krontab.internal.CronDateTimeScheduler
|
|
||||||
import com.insanusmokrassar.krontab.KronScheduler
|
import com.insanusmokrassar.krontab.KronScheduler
|
||||||
|
import com.insanusmokrassar.krontab.internal.*
|
||||||
import com.insanusmokrassar.krontab.internal.CronDateTime
|
import com.insanusmokrassar.krontab.internal.CronDateTime
|
||||||
|
import com.insanusmokrassar.krontab.internal.CronDateTimeScheduler
|
||||||
import com.insanusmokrassar.krontab.internal.fillWith
|
import com.insanusmokrassar.krontab.internal.fillWith
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will help to create an instance of [KronScheduler]
|
||||||
|
*
|
||||||
|
* @see com.insanusmokrassar.krontab.createSimpleScheduler
|
||||||
|
*/
|
||||||
fun buildSchedule(settingsBlock: SchedulerBuilder.() -> Unit): KronScheduler {
|
fun buildSchedule(settingsBlock: SchedulerBuilder.() -> Unit): KronScheduler {
|
||||||
val builder = SchedulerBuilder()
|
val builder = SchedulerBuilder()
|
||||||
|
|
||||||
@ -36,6 +42,9 @@ class SchedulerBuilder(
|
|||||||
} ?: builderValue
|
} ?: builderValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts an seconds block
|
||||||
|
*/
|
||||||
fun seconds(block: SecondsBuilder.() -> Unit) {
|
fun seconds(block: SecondsBuilder.() -> Unit) {
|
||||||
seconds = callAndReturn(
|
seconds = callAndReturn(
|
||||||
seconds,
|
seconds,
|
||||||
@ -44,6 +53,9 @@ class SchedulerBuilder(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts an minutes block
|
||||||
|
*/
|
||||||
fun minutes(block: MinutesBuilder.() -> Unit) {
|
fun minutes(block: MinutesBuilder.() -> Unit) {
|
||||||
minutes = callAndReturn(
|
minutes = callAndReturn(
|
||||||
minutes,
|
minutes,
|
||||||
@ -52,6 +64,9 @@ class SchedulerBuilder(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts an hours block
|
||||||
|
*/
|
||||||
fun hours(block: HoursBuilder.() -> Unit) {
|
fun hours(block: HoursBuilder.() -> Unit) {
|
||||||
hours = callAndReturn(
|
hours = callAndReturn(
|
||||||
hours,
|
hours,
|
||||||
@ -60,6 +75,9 @@ class SchedulerBuilder(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts an days of month block
|
||||||
|
*/
|
||||||
fun dayOfMonth(block: DaysOfMonthBuilder.() -> Unit) {
|
fun dayOfMonth(block: DaysOfMonthBuilder.() -> Unit) {
|
||||||
dayOfMonth = callAndReturn(
|
dayOfMonth = callAndReturn(
|
||||||
dayOfMonth,
|
dayOfMonth,
|
||||||
@ -68,6 +86,9 @@ class SchedulerBuilder(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts an months block
|
||||||
|
*/
|
||||||
fun months(block: MonthsBuilder.() -> Unit) {
|
fun months(block: MonthsBuilder.() -> Unit) {
|
||||||
month = callAndReturn(
|
month = callAndReturn(
|
||||||
month,
|
month,
|
||||||
@ -76,29 +97,11 @@ class SchedulerBuilder(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun build(): KronScheduler {
|
/**
|
||||||
val resultCronDateTimes = mutableListOf(CronDateTime())
|
* @return Completely built and independent [KronScheduler]
|
||||||
|
*
|
||||||
seconds ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
* @see com.insanusmokrassar.krontab.createSimpleScheduler
|
||||||
previousCronDateTime.copy(seconds = currentTime)
|
* @see com.insanusmokrassar.krontab.internal.createKronScheduler
|
||||||
}
|
*/
|
||||||
|
fun build(): KronScheduler = createKronScheduler(seconds, minutes, hours, dayOfMonth, month)
|
||||||
minutes ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(minutes = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
hours ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(hours = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
dayOfMonth ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(dayOfMonth = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
month ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
|
||||||
previousCronDateTime.copy(month = currentTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
return CronDateTimeScheduler(resultCronDateTimes.toList())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.insanusmokrassar.krontab.internal
|
package com.insanusmokrassar.krontab.internal
|
||||||
|
|
||||||
|
import com.insanusmokrassar.krontab.KronScheduler
|
||||||
import com.insanusmokrassar.krontab.utils.clamp
|
import com.insanusmokrassar.krontab.utils.clamp
|
||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
import com.soywiz.klock.DateTimeSpan
|
import com.soywiz.klock.DateTimeSpan
|
||||||
@ -86,3 +87,38 @@ internal fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()
|
|||||||
|
|
||||||
return current
|
return current
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return [KronScheduler] (in fact [CronDateTimeScheduler]) based on incoming data
|
||||||
|
*/
|
||||||
|
internal fun createKronScheduler(
|
||||||
|
seconds: Array<Byte>? = null,
|
||||||
|
minutes: Array<Byte>? = null,
|
||||||
|
hours: Array<Byte>? = null,
|
||||||
|
dayOfMonth: Array<Byte>? = null,
|
||||||
|
month: Array<Byte>? = null
|
||||||
|
): KronScheduler {
|
||||||
|
val resultCronDateTimes = mutableListOf(CronDateTime())
|
||||||
|
|
||||||
|
seconds ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
||||||
|
previousCronDateTime.copy(seconds = currentTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
minutes ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
||||||
|
previousCronDateTime.copy(minutes = currentTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
hours ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
||||||
|
previousCronDateTime.copy(hours = currentTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
dayOfMonth ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
||||||
|
previousCronDateTime.copy(dayOfMonth = currentTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
month ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
|
||||||
|
previousCronDateTime.copy(month = currentTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
return CronDateTimeScheduler(resultCronDateTimes.toList())
|
||||||
|
}
|
||||||
|
@ -6,6 +6,16 @@ import com.soywiz.klock.DateTime
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Cron-oriented realisation of [KronScheduler]
|
* Cron-oriented realisation of [KronScheduler]
|
||||||
|
*
|
||||||
|
* @see com.insanusmokrassar.krontab.AnyTimeScheduler
|
||||||
|
* @see com.insanusmokrassar.krontab.EverySecondScheduler
|
||||||
|
* @see com.insanusmokrassar.krontab.EveryMinuteScheduler
|
||||||
|
* @see com.insanusmokrassar.krontab.EveryHourScheduler
|
||||||
|
* @see com.insanusmokrassar.krontab.EveryDayOfMonthScheduler
|
||||||
|
* @see com.insanusmokrassar.krontab.EveryMonthScheduler
|
||||||
|
*
|
||||||
|
* @see com.insanusmokrassar.krontab.builder.buildSchedule
|
||||||
|
* @see com.insanusmokrassar.krontab.builder.SchedulerBuilder
|
||||||
*/
|
*/
|
||||||
internal data class CronDateTimeScheduler internal constructor(
|
internal data class CronDateTimeScheduler internal constructor(
|
||||||
internal val cronDateTimes: List<CronDateTime>
|
internal val cronDateTimes: List<CronDateTime>
|
||||||
|
Loading…
Reference in New Issue
Block a user