refactor(cron): make next function infallible

Jef Roosens 2023-01-14 18:51:01 +01:00
parent 243002f282
commit 8c0315dea6
8 changed files with 22 additions and 45 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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())
}
}