feat(cron): first step of replacing cron with C implementation

Jef Roosens 2023-01-12 21:52:51 +01:00
parent 84e7e14a19
commit fec8118ff5
9 changed files with 157 additions and 305 deletions

View File

@ -81,7 +81,7 @@ fmt:
.PHONY: test
test:
$(V) test $(SRC_DIR)
$(V) -g test $(SRC_DIR)
.PHONY: clean
clean:

View File

@ -13,7 +13,7 @@ pub mut:
// Next timestamp from which point this job is allowed to be executed
timestamp time.Time
// Required for calculating next timestamp after having pop'ed a job
ce CronExpression
ce &CronExpression = unsafe { nil }
// Actual build config sent to the agent
config BuildConfig
// Whether this is a one-time job
@ -30,7 +30,7 @@ fn (r1 BuildJob) < (r2 BuildJob) bool {
// for each architecture. Agents receive jobs from this queue.
pub struct BuildJobQueue {
// Schedule to use for targets without explicitely defined cron expression
default_schedule CronExpression
default_schedule &CronExpression
// Base image to use for targets without defined base image
default_base_image string
mut:
@ -44,9 +44,9 @@ mut:
}
// new_job_queue initializes a new job queue
pub fn new_job_queue(default_schedule CronExpression, default_base_image string) BuildJobQueue {
pub fn new_job_queue(default_schedule &CronExpression, default_base_image string) BuildJobQueue {
return BuildJobQueue{
default_schedule: default_schedule
default_schedule: unsafe { default_schedule }
default_base_image: default_base_image
invalidated: map[int]time.Time{}
}

View File

@ -1,8 +1,21 @@
#include "expression.h"
#include <time.h>
const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ce_next(SimpleTime *out, CronExpression *ce, SimpleTime *ref) {
struct cron_expression *ce_init() {
return malloc(sizeof(struct cron_expression));
}
void ce_free(struct cron_expression *ce) {
free(ce->months);
free(ce->days);
free(ce->hours);
free(ce->minutes);
free(ce);
}
int ce_next(struct cron_simple_time *out, struct cron_expression *ce, struct 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
@ -26,12 +39,12 @@ int ce_next(SimpleTime *out, CronExpression *ce, SimpleTime *ref) {
day_index++;
}
if (day_index < ce->days_count && ref->day == ce->days[day_index]) {
if (day_index < ce->day_count && ref->day == ce->days[day_index]) {
while (hour_index < ce->hour_count && ref->hour > ce->hours[hour_index]) {
hour_index++;
}
if (hour_index < ce->hours_count && ref->hour == ce->hours[hour_index]) {
if (hour_index < ce->hour_count && ref->hour == ce->hours[hour_index]) {
// Minute is the only value where we explicitely make sure we
// can't match sref's value exactly. This is to ensure we only
// return values in the future.
@ -88,3 +101,20 @@ int ce_next(SimpleTime *out, CronExpression *ce, SimpleTime *ref) {
return 0;
}
int ce_next_from_now(struct cron_simple_time *out, struct cron_expression *ce) {
time_t t = time(NULL);
struct tm gm;
gmtime_r(&t, &gm);
struct cron_simple_time ref = {
.year = gm.tm_year,
// tm_mon goes from 0 to 11
.month = gm.tm_mon + 1,
.day = gm.tm_mday,
.hour = gm.tm_hour,
.minute = gm.tm_min
};
return ce_next(out, ce, &ref);
}

View File

@ -3,14 +3,14 @@
#include <stdlib.h>
#include <string.h>
typedef enum parse_error {
ParseOk = 0,
ParseInvalidExpression = 1,
ParseInvalidNumber = 2,
ParseOutOfRange = 3
} ParseError;
enum cron_parse_error {
CPEParseOk = 0,
CPEParseInvalidExpression = 1,
CPEParseInvalidNumber = 2,
CPEParseOutOfRange = 3
};
typedef struct cron_expression {
struct cron_expression {
uint8_t *minutes;
uint8_t *hours;
uint8_t *days;
@ -19,19 +19,25 @@ typedef struct cron_expression {
uint8_t hour_count;
uint8_t day_count;
uint8_t month_count;
} CronExpression;
};
typedef struct simple_time {
struct cron_simple_time {
int year;
int month;
int day;
int hour;
int minute;
} SimpleTime;
};
struct cron_expression *ce_init();
void cron_ce_free(struct cron_expression *ce);
/**
* Given a
*/
int ce_next(SimpleTime *out, CronExpression *ce, SimpleTime *ref);
int cron_ce_next(struct cron_simple_time *out, struct cron_expression *ce, struct ce_simple_time *ref);
ParseError ce_parse_expression(CronExpression *out, char *s);
int cron_ce_next_from_now(struct simple_time *out, struct cron_expression *ce);
enum cron_parse_error cron_ce_parse_expression(struct cron_expression *out, char *s);

View File

@ -6,10 +6,10 @@ const uint8_t max[4] = {59, 23, 31, 12};
#define SAFE_ATOI(v,s,min,max) \
int _##v = atoi(s); \
if ((_##v) == 0 && strcmp((s), "0") != 0) { \
return ParseInvalidNumber; \
return CPEParseInvalidNumber; \
} \
if (v < (min) || v > (max)) { \
return ParseOutOfRange; \
return CPEParseOutOfRange; \
} \
v = (uint8_t) (_##v);
@ -29,17 +29,17 @@ const uint8_t max[4] = {59, 23, 31, 12};
* - a/c
* - a-b/c
*/
ParseError 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
if (s[1] != '\0') {
return ParseInvalidExpression;
return CPEParseInvalidExpression;
}
*out = ~0;
return ParseOk;
return CPEParseOk;
}
size_t slash_index = 0;
@ -88,20 +88,20 @@ ParseError ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) {
}
}
return ParseOk;
return CPEParseOk;
}
ParseError 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;
ParseError res;
enum cron_parse_error res;
while ((next = strchr(s, ',')) != NULL) {
next[0] = '\0';
res = ce_parse_range(out, s, min, max);
if (res != ParseOk) {
if (res != CPEParseOk) {
return res;
}
@ -111,11 +111,11 @@ ParseError ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) {
// Make sure to parse the final range as well
res = ce_parse_range(out, s, min, max);
if (res != ParseOk) {
if (res != CPEParseOk) {
return res;
}
return ParseOk;
return CPEParseOk;
}
uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
@ -147,11 +147,14 @@ uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
return size;
}
ParseError ce_parse_expression(CronExpression *out, char *s) {
enum cron_parse_error ce_parse_expression(struct cron_expression *out, char *s) {
// The parsing functions modify the input string in-place
s = strdup(s);
uint8_t part_count = 0;
char *next;
ParseError res;
enum cron_parse_error res;
uint64_t bfs[4];
// Skip leading spaces
@ -159,11 +162,11 @@ ParseError ce_parse_expression(CronExpression *out, char *s) {
s++;
}
while (part_count < 4 && (next = strchr(s, ' ')) != NULL) {
while (part_count < 4 && ((next = strchr(s, ' ')) != NULL)) {
next[0] = '\0';
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
if (res != ParseOk) {
if (res != CPEParseOk) {
return res;
}
@ -184,7 +187,7 @@ ParseError ce_parse_expression(CronExpression *out, char *s) {
// Make sure to parse the final range as well
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
if (res != ParseOk) {
if (res != CPEParseOk) {
return res;
}
@ -193,7 +196,7 @@ ParseError ce_parse_expression(CronExpression *out, char *s) {
// At least two parts need to be provided
if (part_count < 2) {
return ParseInvalidExpression;
return CPEParseInvalidExpression;
}
// Ensure there's always 4 parts, as expressions can have between 2 and 4 parts
@ -208,5 +211,5 @@ ParseError ce_parse_expression(CronExpression *out, char *s) {
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]);
return ParseOk;
return CPEParseOk;
}

View File

@ -2,9 +2,10 @@ module expression
#flag -I @VMODROOT/c
#flag @VMODROOT/c/parse.o
#flag @VMODROOT/c/expression.o
#include "expression.h"
pub struct C.CronExpression {
pub struct C.cron_expression {
minutes &u8
hours &u8
days &u8
@ -15,4 +16,22 @@ pub struct C.CronExpression {
month_count u8
}
/* pub type CronExpression = C.CronExpression */
pub type CronExpression = C.cron_expression
struct C.cron_simple_time {
year int
month int
day int
hour int
minute int
}
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_from_now(out &C.cron_simple_time, ce &C.cron_expression) int
fn C.ce_parse_expression(out &C.cron_expression, s &char) int

View File

@ -2,123 +2,61 @@ module expression
import time
pub struct CronExpression {
minutes []int
hours []int
days []int
months []int
pub fn parse_expression(exp string) !&CronExpression {
out := C.ce_init()
res := C.ce_parse_expression(out, exp.str)
if res != 0 {
return error('yuhh')
}
return out
}
pub fn (ce &CronExpression) free() {
C.ce_free(ce)
}
// next calculates the earliest time this cron expression is valid. It will
// always pick a moment in the future, even if ref matches completely up to the
// minute. This function conciously does not take gap years into account.
pub fn (ce &CronExpression) next(ref time.Time) !time.Time {
// If the given ref matches the next cron occurence up to the minute, it
// will return that value. Because we always want to return a value in the
// future, we artifically shift the ref 60 seconds to make sure we always
// match in the future. A shift of 60 seconds is enough because the cron
// expression does not allow for accuracy smaller than one minute.
sref := 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
// to be incremented by one. For example, if the minutes have looped
// around, that means that the hour has to be incremented as well.
mut minute_index := 0
mut hour_index := 0
mut day_index := 0
mut month_index := 0
// This chain is the same logic multiple times, namely that if a "bigger"
// value loops around, then the smaller value will always reset as well.
// For example, if we're going to a new day, the hour & minute will always
// be their smallest value again.
for month_index < ce.months.len && sref.month > ce.months[month_index] {
month_index++
st := C.cron_simple_time{
year: ref.year
month: ref.month
day: ref.day
hour: ref.hour
minute: ref.minute
}
if month_index < ce.months.len && sref.month == ce.months[month_index] {
for day_index < ce.days.len && sref.day > ce.days[day_index] {
day_index++
}
out := C.cron_simple_time{}
res := C.ce_next(&out, ce, &st)
if day_index < ce.days.len && ce.days[day_index] == sref.day {
for hour_index < ce.hours.len && sref.hour > ce.hours[hour_index] {
hour_index++
}
if hour_index < ce.hours.len && ce.hours[hour_index] == sref.hour {
// Minute is the only value where we explicitely make sure we
// can't match sref's value exactly. This is to ensure we only
// return values in the future.
for minute_index < ce.minutes.len && sref.minute >= ce.minutes[minute_index] {
minute_index++
}
}
}
}
// Here, we increment the "bigger" values by one if the smaller ones loop
// around. The order is important, as it allows a sort-of waterfall effect
// to occur which updates all values if required.
if minute_index == ce.minutes.len && hour_index < ce.hours.len {
hour_index += 1
}
if hour_index == ce.hours.len && day_index < ce.days.len {
day_index += 1
}
if day_index == ce.days.len && month_index < ce.months.len {
month_index += 1
}
mut minute := ce.minutes[minute_index % ce.minutes.len]
mut hour := ce.hours[hour_index % ce.hours.len]
mut day := ce.days[day_index % ce.days.len]
// Sometimes, we end up with a day that does not exist within the selected
// month, e.g. day 30 in February. When this occurs, we reset day back to
// the smallest value & loop over to the next month that does have this
// day.
if day > time.month_days[ce.months[month_index % ce.months.len] - 1] {
day = ce.days[0]
month_index += 1
for day > time.month_days[ce.months[month_index & ce.months.len] - 1] {
month_index += 1
// If for whatever reason the day value ends up being something
// that can't be scheduled in any month, we have to make sure we
// don't create an infinite loop.
if month_index == 2 * ce.months.len {
return error('No schedulable moment.')
}
}
}
month := ce.months[month_index % ce.months.len]
mut year := sref.year
// If the month loops over, we need to increment the year.
if month_index >= ce.months.len {
year++
if res != 0 {
return error('yuhh')
}
return time.new_time(time.Time{
year: year
month: month
day: day
minute: minute
hour: hour
year: out.year
month: out.month
day: out.day
hour: out.hour
minute: out.minute
})
}
// next_from_now returns the result of ce.next(ref) where ref is the result of
// time.now().
pub fn (ce &CronExpression) next_from_now() !time.Time {
return ce.next(time.now())
out := C.cron_simple_time{}
res := C.ce_next_from_now(&out, ce)
if res != 0 {
return error('yuhh')
}
return time.new_time(time.Time{
year: out.year
month: out.month
day: out.day
hour: out.hour
minute: out.minute
})
}
// next_n returns the n next occurences of the expression, given a starting

View File

@ -1,146 +0,0 @@
module expression
import bitfield
// parse_range parses a given string into a range of sorted integers. Its
// result is a BitField with set bits for all numbers in the result.
fn parse_range(s string, min int, max int) !bitfield.BitField {
mut start := min
mut end := max
mut interval := 1
mut bf := bitfield.new(max - min + 1)
exps := s.split('/')
if exps.len > 2 {
return error('Invalid expression.')
}
if exps[0] != '*' {
dash_parts := exps[0].split('-')
if dash_parts.len > 2 {
return error('Invalid expression.')
}
start = dash_parts[0].int()
// The builtin parsing functions return zero if the string can't be
// parsed into a number, so we have to explicitely check whether they
// actually entered zero or if it's an invalid number.
if start == 0 && dash_parts[0] != '0' {
return error('Invalid number.')
}
// Check whether the start value is out of range
if start < min || start > max {
return error('Out of range.')
}
if dash_parts.len == 2 {
end = dash_parts[1].int()
if end == 0 && dash_parts[1] != '0' {
return error('Invalid number.')
}
if end < start || end > max {
return error('Out of range.')
}
}
}
if exps.len > 1 {
interval = exps[1].int()
// interval being zero is always invalid, but we want to check why
// it's invalid for better error messages.
if interval == 0 {
if exps[1] != '0' {
return error('Invalid number.')
} else {
return error('Step size zero not allowed.')
}
}
if interval > max - min {
return error('Step size too large.')
}
}
// Here, s solely consists of a number, so that's the only value we
// should return.
else if exps[0] != '*' && !exps[0].contains('-') {
bf.set_bit(start - min)
return bf
}
for start <= end {
bf.set_bit(start - min)
start += interval
}
return bf
}
// bf_to_ints takes a BitField and converts it into the expected list of actual
// integers.
fn bf_to_ints(bf bitfield.BitField, min int) []int {
mut out := []int{}
for i in 0 .. bf.get_size() {
if bf.get_bit(i) == 1 {
out << min + i
}
}
return out
}
// parse_part parses a given part of a cron expression & returns the
// corresponding array of ints.
fn parse_part(s string, min int, max int) ![]int {
mut bf := bitfield.new(max - min + 1)
for range in s.split(',') {
bf2 := parse_range(range, min, max)!
bf = bitfield.bf_or(bf, bf2)
}
return bf_to_ints(bf, min)
}
// parse_expression parses an entire cron expression string into a
// CronExpression object, if possible.
pub fn parse_expression(exp string) !CronExpression {
// The filter allows for multiple spaces between parts
mut parts := exp.split(' ').filter(it != '')
if parts.len < 2 || parts.len > 4 {
return error('Expression must contain between 2 and 4 space-separated parts.')
}
// For ease of use, we allow the user to only specify as many parts as they
// need.
for parts.len < 4 {
parts << '*'
}
mut part_results := [][]int{}
mins := [0, 0, 1, 1]
maxs := [59, 23, 31, 12]
// This for loop allows us to more clearly propagate the error to the user.
for i, min in mins {
part_results << parse_part(parts[i], min, maxs[i]) or {
return error('An error occurred with part $i: $err.msg()')
}
}
return CronExpression{
minutes: part_results[0]
hours: part_results[1]
days: part_results[2]
months: part_results[3]
}
}

View File

@ -4,6 +4,7 @@ import time { parse }
fn util_test_time(exp string, t1_str string, t2_str string) ! {
ce := parse_expression(exp)!
dump(ce)
t1 := parse(t1_str)!
t2 := parse(t2_str)!
@ -18,17 +19,18 @@ fn util_test_time(exp string, t1_str string, t2_str string) ! {
fn test_next_simple() ! {
// Very 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
util_test_time('0 3', '2002-01-01 03:00:00', '2002-01-02 03:00:00')!
util_test_time('0 3', '2002-01-01 04:00:00', '2002-01-02 03:00:00')!
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')!
util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')!
/* util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')! */
// Overlap to next month
util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')!
/* // Overlap to next month */
/* util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')! */
// Overlap to next year
util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')!
/* // Overlap to next year */
/* util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')! */
}