Actually possibly kinda decent cron next func

cron-docs
Jef Roosens 2022-04-12 21:16:09 +02:00
parent e6033f9ab4
commit 1116fee3fc
Signed by: Jef Roosens
GPG Key ID: 2619CD13516CF9C4
3 changed files with 24 additions and 41 deletions

View File

@ -1,9 +1,7 @@
module cron module cron
import git import git
import datatypes
import time import time
import rand
struct ScheduledBuild { struct ScheduledBuild {
repo git.GitRepo repo git.GitRepo
@ -15,12 +13,8 @@ fn (r1 ScheduledBuild) < (r2 ScheduledBuild) bool {
} }
pub fn cron(conf Config) ? { pub fn cron(conf Config) ? {
mut queue := datatypes.MinHeap<time.Time>{}
ce := parse_expression('0 3') ? ce := parse_expression('0 3') ?
t := time.parse('2002-01-01 00:00:00') ? t := time.parse('2002-01-01 00:00:00') ?
println(t)
t2 := ce.next(t) ? t2 := ce.next(t) ?
println(t2) println(t2)
} }

View File

@ -38,17 +38,17 @@ pub fn (ce &CronExpression) next(ref time.Time) ?time.Time {
month_index++ month_index++
} }
if month_index < ce.months.len { if month_index < ce.months.len && sref.month == ce.months[month_index] {
for day_index < ce.days.len && sref.day > ce.days[day_index] { for day_index < ce.days.len && sref.day > ce.days[day_index] {
day_index++ day_index++
} }
if day_index < ce.days.len { if day_index < ce.days.len && ce.days[day_index] == sref.day {
for hour_index < ce.hours.len && sref.hour > ce.hours[hour_index] { for hour_index < ce.hours.len && sref.hour > ce.hours[hour_index] {
hour_index++ hour_index++
} }
if hour_index < ce.hours.len { if hour_index < ce.hours.len && ce.hours[hour_index] == sref.hour {
// Minute is the only value where we explicitely make sure we // Minute is the only value where we explicitely make sure we
// can't match sref's value exactly. This is to ensure we only // can't match sref's value exactly. This is to ensure we only
// return values in the future. // return values in the future.

View File

@ -2,38 +2,27 @@ module cron
import time { parse } import time { parse }
fn util_test_time(exp string, t1_str string, t2_str string) ? {
ce := parse_expression(exp) ?
t1 := parse(t1_str) ?
t2 := parse(t2_str) ?
t3 := ce.next(t1) ?
assert t2.year == t3.year
assert t2.month == t3.month
assert t2.day == t3.day
assert t2.hour == t3.hour
assert t2.minute == t3.minute
}
fn test_next_simple() ? { fn test_next_simple() ? {
ce := parse_expression('0 3') ? // Very simple
t := parse('2002-01-01 00:00:00') ? util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00') ?
t2 := ce.next(t) ?
assert t2.year == 2002 // Overlap to next day
assert t2.month == 1 util_test_time('0 3', '2002-01-01 03:00:00', '2002-01-02 03:00:00') ?
assert t2.day == 1 util_test_time('0 3', '2002-01-01 04:00:00', '2002-01-02 03:00:00') ?
assert t2.hour == 3
assert t2.minute == 0 util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00') ?
}
fn test_next_identical() ? {
ce := parse_expression('0 3') ?
t := parse('2002-01-01 03:00:00') ?
t2 := ce.next(t) ?
assert t2.year == 2002
assert t2.month == 1
assert t2.day == 2
assert t2.hour == 3
assert t2.minute == 0
}
fn test_next_next_day() ? {
ce := parse_expression('0 3') ?
t := parse('2002-01-01 04:00:00') ?
t2 := ce.next(t) ?
assert t2.year == 2002
assert t2.month == 1
assert t2.day == 2
assert t2.hour == 3
assert t2.minute == 0
} }