refactor(cron): make next function infallible
parent
243002f282
commit
8c0315dea6
|
@ -92,7 +92,7 @@ pub fn (mut q BuildJobQueue) insert(input InsertConfig) ! {
|
||||||
q.default_schedule
|
q.default_schedule
|
||||||
}
|
}
|
||||||
|
|
||||||
job.timestamp = ce.next_from_now()!
|
job.timestamp = ce.next_from_now()
|
||||||
job.ce = ce
|
job.ce = ce
|
||||||
} else {
|
} else {
|
||||||
job.timestamp = time.now()
|
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
|
// 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
|
// to its respective queue. This function is called by the pop functions
|
||||||
// *after* having pop'ed the job.
|
// *after* having pop'ed the job.
|
||||||
fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) ! {
|
fn (mut q BuildJobQueue) reschedule(job BuildJob, arch string) {
|
||||||
new_timestamp := job.ce.next_from_now()!
|
new_timestamp := job.ce.next_from_now()
|
||||||
|
|
||||||
new_job := BuildJob{
|
new_job := BuildJob{
|
||||||
...job
|
...job
|
||||||
|
@ -168,10 +168,7 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob {
|
||||||
job = q.queues[arch].pop()?
|
job = q.queues[arch].pop()?
|
||||||
|
|
||||||
if !job.single {
|
if !job.single {
|
||||||
// TODO how do we handle this properly? Is it even possible for a
|
q.reschedule(job, arch)
|
||||||
// cron expression to not return a next time if it's already been
|
|
||||||
// used before?
|
|
||||||
q.reschedule(job, arch) or {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return job
|
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 }
|
job = q.queues[arch].pop() or { break }
|
||||||
|
|
||||||
if !job.single {
|
if !job.single {
|
||||||
// TODO idem
|
q.reschedule(job, arch)
|
||||||
q.reschedule(job, arch) or {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out << job
|
out << job
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub fn cmd() cli.Command {
|
||||||
ce := cron.parse_expression(cmd.args.join(' '))!
|
ce := cron.parse_expression(cmd.args.join(' '))!
|
||||||
count := cmd.flags.get_int('count')!
|
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)
|
println(t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ void ce_free(cron_expression *ce) {
|
||||||
free(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
|
// 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
|
// the length of their respective array in the CronExpression object, that
|
||||||
// means we've looped back around. This means that the "bigger" value has
|
// 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 {
|
} else {
|
||||||
out->year = ref->year;
|
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);
|
time_t t = time(NULL);
|
||||||
struct tm gm;
|
struct tm gm;
|
||||||
gmtime_r(&t, &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
|
.minute = gm.tm_min
|
||||||
};
|
};
|
||||||
|
|
||||||
return ce_next(out, ce, &ref);
|
ce_next(out, ce, &ref);
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,9 +37,9 @@ cron_expression *ce_init();
|
||||||
|
|
||||||
void cron_ce_free(cron_expression *ce);
|
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);
|
enum cron_parse_error cron_ce_parse_expression(cron_expression *out, char *s);
|
||||||
|
|
||||||
|
|
|
@ -32,8 +32,8 @@ fn C.ce_init() &C.cron_expression
|
||||||
|
|
||||||
fn C.ce_free(ce &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
|
fn C.ce_parse_expression(out &C.cron_expression, s &char) int
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub fn parse_expression(exp string) !&Expression {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ce &Expression) next(ref time.Time) !time.Time {
|
pub fn (ce &Expression) next(ref time.Time) time.Time {
|
||||||
st := SimpleTime{
|
st := SimpleTime{
|
||||||
year: ref.year
|
year: ref.year
|
||||||
month: ref.month
|
month: ref.month
|
||||||
|
@ -28,11 +28,7 @@ pub fn (ce &Expression) next(ref time.Time) !time.Time {
|
||||||
}
|
}
|
||||||
|
|
||||||
out := SimpleTime{}
|
out := SimpleTime{}
|
||||||
res := C.ce_next(&out, ce, &st)
|
C.ce_next(&out, ce, &st)
|
||||||
|
|
||||||
if res != 0 {
|
|
||||||
return error('yuhh')
|
|
||||||
}
|
|
||||||
|
|
||||||
return time.new_time(time.Time{
|
return time.new_time(time.Time{
|
||||||
year: out.year
|
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{}
|
out := SimpleTime{}
|
||||||
res := C.ce_next_from_now(&out, ce)
|
C.ce_next_from_now(&out, ce)
|
||||||
|
|
||||||
if res != 0 {
|
|
||||||
return error('yuhh')
|
|
||||||
}
|
|
||||||
|
|
||||||
return time.new_time(time.Time{
|
return time.new_time(time.Time{
|
||||||
year: out.year
|
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
|
// next_n returns the n next occurences of the expression, given a starting
|
||||||
// time.
|
// 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}
|
mut times := []time.Time{cap: n}
|
||||||
|
|
||||||
times << ce.next(ref)!
|
times << ce.next(ref)
|
||||||
|
|
||||||
for i in 1 .. n {
|
for i in 1 .. n {
|
||||||
times << ce.next(times[i - 1])!
|
times << ce.next(times[i - 1])
|
||||||
}
|
}
|
||||||
|
|
||||||
return times
|
return times
|
||||||
|
|
|
@ -8,7 +8,7 @@ fn util_test_time(exp string, t1_str string, t2_str string) ! {
|
||||||
t1 := parse(t1_str)!
|
t1 := parse(t1_str)!
|
||||||
t2 := parse(t2_str)!
|
t2 := parse(t2_str)!
|
||||||
|
|
||||||
t3 := ce.next(t1)!
|
t3 := ce.next(t1)
|
||||||
|
|
||||||
assert t2.year == t3.year
|
assert t2.year == t3.year
|
||||||
assert t2.month == t3.month
|
assert t2.month == t3.month
|
||||||
|
|
|
@ -9,11 +9,7 @@ const fallback_log_removal_frequency = 24 * time.hour
|
||||||
|
|
||||||
// log_removal_daemon removes old build logs every `log_removal_frequency`.
|
// log_removal_daemon removes old build logs every `log_removal_frequency`.
|
||||||
fn (mut app App) log_removal_daemon(schedule cron.Expression) {
|
fn (mut app App) log_removal_daemon(schedule cron.Expression) {
|
||||||
mut start_time := time.Time{}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
start_time = time.now()
|
|
||||||
|
|
||||||
mut too_old_timestamp := time.now().add_days(-app.conf.max_log_age)
|
mut too_old_timestamp := time.now().add_days(-app.conf.max_log_age)
|
||||||
|
|
||||||
app.linfo('Cleaning logs before $too_old_timestamp')
|
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)')
|
app.linfo('Cleaned $counter logs ($failed failed)')
|
||||||
|
|
||||||
// Sleep until the next cycle
|
// Sleep until the next cycle
|
||||||
next_time := schedule.next_from_now() or {
|
next_time := schedule.next_from_now()
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.sleep(next_time - time.now())
|
time.sleep(next_time - time.now())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue