diff --git a/Makefile b/Makefile index 3571d30..c71ff1f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # =====CONFIG===== SRC_DIR := src -SOURCES != find '$(SRC_DIR)' -\( -iname '*.v' -or -iname '*.h' -or -iname '*.c' -\) +SOURCES != find '$(SRC_DIR)' -iname '*.v' V_PATH ?= v V := $(V_PATH) -showcc -gc boehm -W -d use_openssl -skip-unused diff --git a/src/build/queue.v b/src/build/queue.v index abd4ec6..122180e 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,7 +168,10 @@ pub fn (mut q BuildJobQueue) pop(arch string) ?BuildJob { job = q.queues[arch].pop()? if !job.single { - q.reschedule(job, arch) + // 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 {} } return job @@ -195,7 +198,8 @@ pub fn (mut q BuildJobQueue) pop_n(arch string, n int) []BuildJob { job = q.queues[arch].pop() or { break } if !job.single { - q.reschedule(job, arch) + // TODO idem + q.reschedule(job, arch) or {} } out << job diff --git a/src/console/schedule/schedule.v b/src/console/schedule/schedule.v index ceabf24..40b300f 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 59d1bf0..ed0306f 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); } -void ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref) { +int 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 @@ -84,6 +84,11 @@ void ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref) while (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) { month_index++; + + // TODO find out if this can happen + if (month_index == 2 * ce->month_count) { + return 1; + } } } @@ -94,14 +99,16 @@ void ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref) } else { out->year = ref->year; } + + return 0; } -void ce_next_from_now(cron_simple_time *out, cron_expression *ce) { +int ce_next_from_now(cron_simple_time *out, cron_expression *ce) { time_t t = time(NULL); struct tm gm; gmtime_r(&t, &gm); - cron_simple_time ref = { + struct cron_simple_time ref = { .year = gm.tm_year, // tm_mon goes from 0 to 11 .month = gm.tm_mon + 1, @@ -110,5 +117,5 @@ void ce_next_from_now(cron_simple_time *out, cron_expression *ce) { .minute = gm.tm_min }; - ce_next(out, ce, &ref); + return ce_next(out, ce, &ref); } diff --git a/src/cron/c/expression.h b/src/cron/c/expression.h index c599e4e..c9441f6 100644 --- a/src/cron/c/expression.h +++ b/src/cron/c/expression.h @@ -1,6 +1,3 @@ -#ifndef VIETER_CRON -#define VIETER_CRON - #include #include #include @@ -10,8 +7,7 @@ typedef enum cron_parse_error { cron_parse_ok = 0, cron_parse_invalid_expression = 1, cron_parse_invalid_number = 2, - cron_parse_out_of_range = 3, - cron_parse_too_many_parts = 4 + cron_parse_out_of_range = 3 } cron_parse_error; typedef struct cron_expression { @@ -37,10 +33,8 @@ cron_expression *ce_init(); void cron_ce_free(cron_expression *ce); -void cron_ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref); +int cron_ce_next(cron_simple_time *out, cron_expression *ce, cron_simple_time *ref); -void cron_ce_next_from_now(cron_simple_time *out, cron_expression *ce); +int 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); - -#endif diff --git a/src/cron/c/parse.c b/src/cron/c/parse.c index f54b818..a577c6d 100644 --- a/src/cron/c/parse.c +++ b/src/cron/c/parse.c @@ -1,28 +1,21 @@ #include "expression.h" -const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - // Allowed value ranges for the minute, hour, day and month field const uint8_t min[4] = {0, 0, 1, 1}; const uint8_t max[4] = {59, 23, 31, 12}; -const uint8_t min_parts = 2; -const uint8_t max_parts = 4; - -// Convert a string into a uint8_t value by parsing it using atoi and checking +// Convert a string a uint8_t value by parsing it using atoi and checking // whether it's contained within the given range #define SAFE_ATOI(v,s,min,max) \ int _##v = atoi(s); \ if ((_##v) == 0 && strcmp((s), "0") != 0) { \ return cron_parse_invalid_number; \ } \ - if (((_##v) < (min)) || ((_##v) > (max))) { \ + if (v < (min) || v > (max)) { \ return cron_parse_out_of_range; \ } \ v = (uint8_t) (_##v); -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) - /** * Given a range expression, produce a bit field defining what numbers in the * min-max range the expression represents. Bit 0 (starting from the @@ -39,7 +32,7 @@ const uint8_t max_parts = 4; * - a/c * - a-b/c */ -cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) { +enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) { // The * expression means "every possible value" if (s[0] == '*') { // A '*' is only valid on its own @@ -105,7 +98,7 @@ cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max * min-max range the part represents. A part consists of one or more range * expressions, separated by commas. */ -cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) { +enum cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) { *out = 0; char *next; @@ -182,30 +175,28 @@ enum cron_parse_error ce_parse_expression(cron_expression *out, char *s) { s = strdup(s); char *orig_s = s; - enum cron_parse_error res = cron_parse_ok; - uint64_t bfs[max_parts]; - - // First we divide the input string into its parts, divided by spaces. - // Each part is delimited by a NULL byte. uint8_t part_count = 0; - char *parts[max_parts]; + char *next; + enum cron_parse_error res = cron_parse_ok; + uint64_t bfs[4]; // Skip leading spaces - size_t offset = 0; - - while (s[offset] == ' ') { - offset++; + while (s[0] == ' ') { + s++; } - s += offset; - - while (part_count < max_parts && ((next = strchr(s, ' ')) != NULL)) { + while (part_count < 4 && ((next = strchr(s, ' ')) != NULL)) { next[0] = '\0'; - parts[part_count] = s; + res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]); + + if (res != cron_parse_ok) { + goto end; + } + + size_t offset = 1; // Skip multiple spaces - offset = 1; while (next[offset] == ' ') { offset++; } @@ -214,87 +205,34 @@ enum cron_parse_error ce_parse_expression(cron_expression *out, char *s) { part_count++; } - // The loop exited because we already have 4 parts, yet there's still at - // least one more part that follows. - if (next != NULL) { - res = cron_parse_too_many_parts; - } else if (s[0] != '\0') { - // There's one more excessive trailing part - if (part_count == max_parts) { - res = cron_parse_too_many_parts; + // Parse final trailing part + if (part_count < 4 && s[0] != '\0') { + res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]); + + if (res != cron_parse_ok) { goto end; } - parts[part_count] = s; part_count++; } - - // We now parse the parts in reverse. This is because the month part - // determines the maximum value of the day part. - - uint64_t bit_field = 0; - - // Months - if (part_count >= 4) { - res = ce_parse_part(&bit_field, parts[3], min[3], max[3]); - - if (res != cron_parse_ok) { - goto end; - } - - out->month_count = bf_to_nums(&out->months, bit_field, min[3], max[3]); - } - // If months aren't provided, they're replaced with a * - else { - out->month_count = bf_to_nums(&out->months, ~0, min[3], max[3]); - } - - // Determine what the largest allowed day value is, given the months - uint8_t max_day_value = 0; - - for (uint8_t i = 0; i < out->month_count; i++) { - max_day_value = MAX(max_day_value, month_days[out->months[i] - 1]); - } - - // Days - if (part_count >= 3) { - bit_field = 0; - - res = ce_parse_part(&bit_field, parts[2], min[2], max_day_value); - - if (res != cron_parse_ok) { - goto end; - } - - out->day_count = bf_to_nums(&out->days, bit_field, min[2], max_day_value); - } - // If days aren't provided, they're replaced with a * - else { - out->day_count = bf_to_nums(&out->days, ~0, min[2], max_day_value); - } - - // Hours - bit_field = 0; - - res = ce_parse_part(&bit_field, parts[1], min[1], max[1]); - - if (res != cron_parse_ok) { + // At least two parts need to be provided + if (part_count < 2) { + res = cron_parse_invalid_expression; goto end; } - out->hour_count = bf_to_nums(&out->hours, bit_field, min[1], max[1]); - - // Minutes - bit_field = 0; - - res = ce_parse_part(&bit_field, parts[0], min[0], max[0]); - - if (res != cron_parse_ok) { - goto end; + // Ensure there's always 4 parts, as expressions can have between 2 and 4 parts + while (part_count < 4) { + // Expression is augmented with '*' expressions + bfs[part_count] = ~0; + part_count++; } - out->minute_count = bf_to_nums(&out->minutes, bit_field, min[0], max[0]); + out->minute_count = bf_to_nums(&out->minutes, bfs[0], min[0], max[0]); + out->hour_count = bf_to_nums(&out->hours, bfs[1], min[1], max[1]); + out->day_count = bf_to_nums(&out->days, bfs[2], min[2], max[2]); + out->month_count = bf_to_nums(&out->months, bfs[3], min[3], max[3]); end: // s is cloned diff --git a/src/cron/expression.c.v b/src/cron/expression.c.v index 7551e6f..08af25c 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) +fn C.ce_next(out &C.cron_simple_time, ce &C.cron_expression, ref &C.cron_simple_time) int -fn C.ce_next_from_now(out &C.cron_simple_time, ce &C.cron_expression) +fn C.ce_next_from_now(out &C.cron_simple_time, ce &C.cron_expression) int 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 c0cab8d..0429b93 100644 --- a/src/cron/expression.v +++ b/src/cron/expression.v @@ -2,23 +2,22 @@ module cron import time -[unsafe] -pub fn (ce &Expression) free() { - C.ce_free(ce) -} - pub fn parse_expression(exp string) !&Expression { out := C.ce_init() res := C.ce_parse_expression(out, exp.str) if res != 0 { - return error(res.str()) + return error('yuhh') } return out } -pub fn (ce &Expression) next(ref time.Time) time.Time { +pub fn (ce &Expression) free() { + C.ce_free(ce) +} + +pub fn (ce &Expression) next(ref time.Time) !time.Time { st := SimpleTime{ year: ref.year month: ref.month @@ -28,7 +27,11 @@ pub fn (ce &Expression) next(ref time.Time) time.Time { } out := SimpleTime{} - C.ce_next(&out, ce, &st) + res := C.ce_next(&out, ce, &st) + + if res != 0 { + return error('yuhh') + } return time.new_time(time.Time{ year: out.year @@ -39,9 +42,13 @@ 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{} - C.ce_next_from_now(&out, ce) + res := C.ce_next_from_now(&out, ce) + + if res != 0 { + return error('yuhh') + } return time.new_time(time.Time{ year: out.year @@ -54,13 +61,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 7d1516d..e1a9849 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 @@ -22,7 +22,7 @@ fn test_next_simple() ! { // util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')! // Overlap to next day - mut exp := '0 3 ' + mut exp := '0 3 ' util_test_time(exp, '2002-01-01 03:00:00', '2002-01-02 03:00:00')! util_test_time(exp, '2002-01-01 04:00:00', '2002-01-02 03:00:00')! diff --git a/src/server/log_removal.v b/src/server/log_removal.v index 7f1cfb5..98cba93 100644 --- a/src/server/log_removal.v +++ b/src/server/log_removal.v @@ -9,7 +9,11 @@ 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') @@ -47,7 +51,12 @@ 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() + 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) + } + time.sleep(next_time - time.now()) } }