From 8c0315dea679fe52daa23d429310a7db645fbde8 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 14 Jan 2023 18:51:01 +0100 Subject: [PATCH] refactor(cron): make next function infallible --- src/build/queue.v | 14 +++++--------- src/console/schedule/schedule.v | 2 +- src/cron/c/expression.c | 8 +++----- src/cron/c/expression.h | 4 ++-- src/cron/expression.c.v | 4 ++-- src/cron/expression.v | 22 +++++++--------------- src/cron/expression_test.v | 2 +- src/server/log_removal.v | 11 +---------- 8 files changed, 22 insertions(+), 45 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index 122180e..abd4ec6 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -92,7 +92,7 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { q.default_schedule } - job.timestamp = ce.next_from_now()! + job.timestamp = ce.next_from_now() job.ce = ce } else { job.timestamp = time.now() @@ -105,8 +105,8 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! { // reschedule the given job by calculating the next timestamp and re-adding it // to its respective queue. This function is called by the pop functions // *after* having pop'ed the job. -fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) ! { - new_timestamp := job.ce.next_from_now()! +fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) { + new_timestamp := job.ce.next_from_now() new_job := BuildJob{ ...job @@ -168,10 +168,7 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { job = q.queues[arch].pop()? if !job.single { - // TODO how do we handle this properly? Is it even possible for a - // cron expression to not return a next time if it's already been - // used before? - q.reschedule(job, arch) or {} + q.reschedule(job, arch) } return job @@ -198,8 +195,7 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { job = q.queues[arch].pop() or { break } if !job.single { - // TODO idem - q.reschedule(job, arch) or {} + q.reschedule(job, arch) } out << job diff --git a/src/console/schedule/schedule.v b/src/console/schedule/schedule.v index 40b300f..ceabf24 100644 --- a/src/console/schedule/schedule.v +++ b/src/console/schedule/schedule.v @@ -22,7 +22,7 @@ pub fn cmd() cli.Command { ce := cron.parse_expression(cmd.args.join(' '))! count := cmd.flags.get_int('count')! - for t in ce.next_n(time.now(), count)! { + for t in ce.next_n(time.now(), count) { println(t) } } diff --git a/src/cron/c/expression.c b/src/cron/c/expression.c index 0051e49..59d1bf0 100644 --- a/src/cron/c/expression.c +++ b/src/cron/c/expression.c @@ -15,7 +15,7 @@ void ce_free(cron_expression *ce) { free(ce); } -int ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref) { +void ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref) { // For all of these values, the rule is the following: if their value is // the length of their respective array in the CronExpression object, that // means we've looped back around. This means that the "bigger" value has @@ -94,11 +94,9 @@ int ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref) { } else { out->year = ref->year; } - - return 0; } -int ce_next_from_now(cron_simple_time *out, cron_expression *ce) { +void ce_next_from_now(cron_simple_time *out, cron_expression *ce) { time_t t = time(NULL); struct tm gm; gmtime_r(&t, &gm); @@ -112,5 +110,5 @@ int ce_next_from_now(cron_simple_time *out, cron_expression *ce) { .minute = gm.tm_min }; - return ce_next(out, ce, &ref); + ce_next(out, ce, &ref); } diff --git a/src/cron/c/expression.h b/src/cron/c/expression.h index 1c86436..c599e4e 100644 --- a/src/cron/c/expression.h +++ b/src/cron/c/expression.h @@ -37,9 +37,9 @@ cron_expression *ce_init(); void cron_ce_free(cron_expression *ce); -int cron_ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref); +void cron_ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref); -int cron_ce_next_from_now(cron_simple_time *out, cron_expression *ce); +void cron_ce_next_from_now(cron_simple_time *out, cron_expression *ce); enum cron_parse_error cron_ce_parse_expression(cron_expression *out, char *s); diff --git a/src/cron/expression.c.v b/src/cron/expression.c.v index 08af25c..7551e6f 100644 --- a/src/cron/expression.c.v +++ b/src/cron/expression.c.v @@ -32,8 +32,8 @@ fn C.ce_init() &C.cron_expression fn C.ce_free(ce &C.cron_expression) -fn C.ce_next(out &C.cron_simple_time, ce &C.cron_expression, ref &C.cron_simple_time) int +fn C.ce_next(out &C.cron_simple_time, ce &C.cron_expression, ref &C.cron_simple_time) -fn C.ce_next_from_now(out &C.cron_simple_time, ce &C.cron_expression) int +fn C.ce_next_from_now(out &C.cron_simple_time, ce &C.cron_expression) fn C.ce_parse_expression(out &C.cron_expression, s &char) int diff --git a/src/cron/expression.v b/src/cron/expression.v index 23e1354..c0cab8d 100644 --- a/src/cron/expression.v +++ b/src/cron/expression.v @@ -18,7 +18,7 @@ pub fn parse_expression(exp string) !&Expression { return out } -pub fn (ce &Expression) next(ref time.Time) !time.Time { +pub fn (ce &Expression) next(ref time.Time) time.Time { st := SimpleTime{ year: ref.year month: ref.month @@ -28,11 +28,7 @@ pub fn (ce &Expression) next(ref time.Time) !time.Time { } out := SimpleTime{} - res := C.ce_next(&out, ce, &st) - - if res != 0 { - return error('yuhh') - } + C.ce_next(&out, ce, &st) return time.new_time(time.Time{ year: out.year @@ -43,13 +39,9 @@ pub fn (ce &Expression) next(ref time.Time) !time.Time { }) } -pub fn (ce &Expression) next_from_now() !time.Time { +pub fn (ce &Expression) next_from_now() time.Time { out := SimpleTime{} - res := C.ce_next_from_now(&out, ce) - - if res != 0 { - return error('yuhh') - } + C.ce_next_from_now(&out, ce) return time.new_time(time.Time{ year: out.year @@ -62,13 +54,13 @@ pub fn (ce &Expression) next_from_now() !time.Time { // next_n returns the n next occurences of the expression, given a starting // time. -pub fn (ce &Expression) next_n(ref time.Time, n int) ![]time.Time { +pub fn (ce &Expression) next_n(ref time.Time, n int) []time.Time { mut times := []time.Time{cap: n} - times << ce.next(ref)! + times << ce.next(ref) for i in 1 .. n { - times << ce.next(times[i - 1])! + times << ce.next(times[i - 1]) } return times diff --git a/src/cron/expression_test.v b/src/cron/expression_test.v index d6ec002..7d1516d 100644 --- a/src/cron/expression_test.v +++ b/src/cron/expression_test.v @@ -8,7 +8,7 @@ fn util_test_time(exp string, t1_str string, t2_str string) ! { t1 := parse(t1_str)! t2 := parse(t2_str)! - t3 := ce.next(t1)! + t3 := ce.next(t1) assert t2.year == t3.year assert t2.month == t3.month diff --git a/src/server/log_removal.v b/src/server/log_removal.v index 98cba93..7f1cfb5 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -9,11 +9,7 @@ const fallback_log_removal_frequency = 24 * time.hour // log_removal_daemon removes old build logs every `log_removal_frequency`. fn (mut app App) log_removal_daemon(schedule cron.Expression) { - mut start_time := time.Time{} - for { - start_time = time.now() - mut too_old_timestamp := time.now().add_days(-app.conf.max_log_age) app.linfo('Cleaning logs before $too_old_timestamp') @@ -51,12 +47,7 @@ fn (mut app App) log_removal_daemon(schedule cron.Expression) { app.linfo('Cleaned $counter logs ($failed failed)') // Sleep until the next cycle - next_time := schedule.next_from_now() or { - app.lerror("Log removal daemon couldn't calculate next time: $err.msg(); fallback to $server.fallback_log_removal_frequency") - - start_time.add(server.fallback_log_removal_frequency) - } - + next_time := schedule.next_from_now() time.sleep(next_time - time.now()) } }