#include #include #include #include struct crontask { void (*run)(); int last_run; // 0: run immediately on start, -1: run interval seconds after start int interval; }; #define TASKCOUNT 4 static struct crontask tasks[TASKCOUNT] = { {&traffic_updatetotal, 0, 10}, {&traffic_writetotal, -1, 3600}, {&traffic_updatespeeds, 0, 2}, {&temperature_update, 0, 60} }; /* Public functions */ void cron_initialize() { int i; for (i = 0; i < TASKCOUNT; i++) { if (tasks[i].last_run < 0) tasks[i].last_run = time(NULL); } } int cron_runtasks() { // Returns time of next event int i; int nextevent = -1; for (i = 0; i < TASKCOUNT; i++) { if (tasks[i].last_run + tasks[i].interval <= time(NULL)) { tasks[i].last_run = time(NULL); tasks[i].run(); } int ne = tasks[i].last_run + tasks[i].interval; if (nextevent < 0 || ne < nextevent) nextevent = ne; } if (nextevent < 0) fprintf(stderr, "Nothing to run!\n"); return nextevent; }