Compare commits

...

2 Commits

10 changed files with 134 additions and 93 deletions

View File

@ -1,6 +1,6 @@
# =====CONFIG===== # =====CONFIG=====
SRC_DIR := src SRC_DIR := src
SOURCES != find '$(SRC_DIR)' -iname '*.v' SOURCES != find '$(SRC_DIR)' -\( -iname '*.v' -or -iname '*.h' -or -iname '*.c' -\)
V_PATH ?= v V_PATH ?= v
V := $(V_PATH) -showcc -gc boehm -W -d use_openssl -skip-unused V := $(V_PATH) -showcc -gc boehm -W -d use_openssl -skip-unused

View File

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

View File

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

View File

@ -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
@ -84,11 +84,6 @@ int 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]) { while (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) {
month_index++; month_index++;
// TODO find out if this can happen
if (month_index == 2 * ce->month_count) {
return 1;
}
} }
} }
@ -99,16 +94,14 @@ 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);
struct cron_simple_time ref = { cron_simple_time ref = {
.year = gm.tm_year, .year = gm.tm_year,
// tm_mon goes from 0 to 11 // tm_mon goes from 0 to 11
.month = gm.tm_mon + 1, .month = gm.tm_mon + 1,
@ -117,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);
} }

View File

@ -1,3 +1,6 @@
#ifndef VIETER_CRON
#define VIETER_CRON
#include <time.h> #include <time.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -7,7 +10,8 @@ typedef enum cron_parse_error {
cron_parse_ok = 0, cron_parse_ok = 0,
cron_parse_invalid_expression = 1, cron_parse_invalid_expression = 1,
cron_parse_invalid_number = 2, cron_parse_invalid_number = 2,
cron_parse_out_of_range = 3 cron_parse_out_of_range = 3,
cron_parse_too_many_parts = 4
} cron_parse_error; } cron_parse_error;
typedef struct cron_expression { typedef struct cron_expression {
@ -33,8 +37,10 @@ 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);
#endif

View File

@ -1,21 +1,28 @@
#include "expression.h" #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 // Allowed value ranges for the minute, hour, day and month field
const uint8_t min[4] = {0, 0, 1, 1}; const uint8_t min[4] = {0, 0, 1, 1};
const uint8_t max[4] = {59, 23, 31, 12}; const uint8_t max[4] = {59, 23, 31, 12};
// Convert a string a uint8_t value by parsing it using atoi and checking 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
// whether it's contained within the given range // whether it's contained within the given range
#define SAFE_ATOI(v,s,min,max) \ #define SAFE_ATOI(v,s,min,max) \
int _##v = atoi(s); \ int _##v = atoi(s); \
if ((_##v) == 0 && strcmp((s), "0") != 0) { \ if ((_##v) == 0 && strcmp((s), "0") != 0) { \
return cron_parse_invalid_number; \ return cron_parse_invalid_number; \
} \ } \
if (v < (min) || v > (max)) { \ if (((_##v) < (min)) || ((_##v) > (max))) { \
return cron_parse_out_of_range; \ return cron_parse_out_of_range; \
} \ } \
v = (uint8_t) (_##v); 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 * Given a range expression, produce a bit field defining what numbers in the
* min-max range the expression represents. Bit 0 (starting from the * min-max range the expression represents. Bit 0 (starting from the
@ -32,7 +39,7 @@ const uint8_t max[4] = {59, 23, 31, 12};
* - a/c * - a/c
* - a-b/c * - a-b/c
*/ */
enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) { cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) {
// The * expression means "every possible value" // The * expression means "every possible value"
if (s[0] == '*') { if (s[0] == '*') {
// A '*' is only valid on its own // A '*' is only valid on its own
@ -98,7 +105,7 @@ enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_
* min-max range the part represents. A part consists of one or more range * min-max range the part represents. A part consists of one or more range
* expressions, separated by commas. * expressions, separated by commas.
*/ */
enum cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) { cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) {
*out = 0; *out = 0;
char *next; char *next;
@ -175,28 +182,30 @@ enum cron_parse_error ce_parse_expression(cron_expression *out, char *s) {
s = strdup(s); s = strdup(s);
char *orig_s = s; char *orig_s = s;
uint8_t part_count = 0;
char *next;
enum cron_parse_error res = cron_parse_ok; enum cron_parse_error res = cron_parse_ok;
uint64_t bfs[4]; 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;
// Skip leading spaces // Skip leading spaces
while (s[0] == ' ') { size_t offset = 0;
s++;
while (s[offset] == ' ') {
offset++;
} }
while (part_count < 4 && ((next = strchr(s, ' ')) != NULL)) { s += offset;
while (part_count < max_parts && ((next = strchr(s, ' ')) != NULL)) {
next[0] = '\0'; next[0] = '\0';
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]); parts[part_count] = s;
if (res != cron_parse_ok) {
goto end;
}
size_t offset = 1;
// Skip multiple spaces // Skip multiple spaces
offset = 1;
while (next[offset] == ' ') { while (next[offset] == ' ') {
offset++; offset++;
} }
@ -205,34 +214,87 @@ enum cron_parse_error ce_parse_expression(cron_expression *out, char *s) {
part_count++; part_count++;
} }
// Parse final trailing part // The loop exited because we already have 4 parts, yet there's still at
if (part_count < 4 && s[0] != '\0') { // least one more part that follows.
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]); 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;
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) { if (res != cron_parse_ok) {
goto end; goto end;
} }
part_count++; 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]);
} }
// At least two parts need to be provided // Determine what the largest allowed day value is, given the months
if (part_count < 2) { uint8_t max_day_value = 0;
res = cron_parse_invalid_expression;
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) {
goto end; goto end;
} }
// Ensure there's always 4 parts, as expressions can have between 2 and 4 parts out->hour_count = bf_to_nums(&out->hours, bit_field, min[1], max[1]);
while (part_count < 4) {
// Expression is augmented with '*' expressions // Minutes
bfs[part_count] = ~0; bit_field = 0;
part_count++;
res = ce_parse_part(&bit_field, parts[0], min[0], max[0]);
if (res != cron_parse_ok) {
goto end;
} }
out->minute_count = bf_to_nums(&out->minutes, bfs[0], min[0], max[0]); out->minute_count = bf_to_nums(&out->minutes, bit_field, 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: end:
// s is cloned // s is cloned

View File

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

View File

@ -2,22 +2,23 @@ module cron
import time import time
[unsafe]
pub fn (ce &Expression) free() {
C.ce_free(ce)
}
pub fn parse_expression(exp string) !&Expression { pub fn parse_expression(exp string) !&Expression {
out := C.ce_init() out := C.ce_init()
res := C.ce_parse_expression(out, exp.str) res := C.ce_parse_expression(out, exp.str)
if res != 0 { if res != 0 {
return error('yuhh') return error(res.str())
} }
return out return out
} }
pub fn (ce &Expression) free() { pub fn (ce &Expression) next(ref time.Time) time.Time {
C.ce_free(ce)
}
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
@ -27,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
@ -42,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
@ -61,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

View File

@ -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
@ -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')! // util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00')!
// Overlap to next day // 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 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')! util_test_time(exp, '2002-01-01 04:00:00', '2002-01-02 03:00:00')!

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`. // 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())
} }
} }