Fix bugs in Cron

This commit is contained in:
Jordan Bancino 2022-12-26 16:28:58 +00:00
parent 3c8d89a52e
commit 1a43ea6470
5 changed files with 32 additions and 12 deletions

View file

@ -42,12 +42,12 @@ typedef struct Job
{ {
unsigned long interval; unsigned long interval;
unsigned long lastExec; unsigned long lastExec;
void (*func) (void *); JobFunc *func;
void *args; void *args;
} Job; } Job;
static Job * static Job *
JobCreate(long interval, void (*func) (void *), void *args) JobCreate(long interval, JobFunc *func, void *args)
{ {
Job *job; Job *job;
@ -84,6 +84,7 @@ CronThread(void *args)
pthread_mutex_lock(&cron->lock); pthread_mutex_lock(&cron->lock);
ts = UtilServerTs(); ts = UtilServerTs();
for (i = 0; i < ArraySize(cron->jobs); i++) for (i = 0; i < ArraySize(cron->jobs); i++)
{ {
Job *job = ArrayGet(cron->jobs, i); Job *job = ArrayGet(cron->jobs, i);
@ -107,7 +108,22 @@ CronThread(void *args)
/* Only sleep if the jobs didn't overrun the tick */ /* Only sleep if the jobs didn't overrun the tick */
if (cron->tick > (te - ts)) if (cron->tick > (te - ts))
{ {
UtilSleepMillis(cron->tick - (te - ts)); const unsigned long microTick = 100;
unsigned long remainingTick = cron->tick - (te - ts);
/* Only sleep for microTick ms at a time because if the
* job scheduler is supposed to stop before the tick is up,
* we don't want to be stuck in a long sleep */
while (remainingTick >= microTick && !cron->stop)
{
UtilSleepMillis(microTick);
remainingTick -= microTick;
}
if (remainingTick && !cron->stop)
{
UtilSleepMillis(remainingTick);
}
} }
} }
@ -132,6 +148,7 @@ CronCreate(unsigned long tick)
} }
cron->tick = tick; cron->tick = tick;
cron->stop = 1;
pthread_mutex_init(&cron->lock, NULL); pthread_mutex_init(&cron->lock, NULL);
@ -139,7 +156,7 @@ CronCreate(unsigned long tick)
} }
void void
CronOnce(Cron * cron, void (*func) (void *), void *args) CronOnce(Cron * cron, JobFunc *func, void *args)
{ {
Job *job; Job *job;
@ -160,7 +177,7 @@ void
} }
void void
CronEvery(Cron * cron, unsigned long interval, void (*func) (void *), void *args) CronEvery(Cron * cron, unsigned long interval, JobFunc *func, void *args)
{ {
Job *job; Job *job;

View file

@ -41,6 +41,7 @@
#include <Matrix.h> #include <Matrix.h>
#include <Db.h> #include <Db.h>
#include <Cron.h> #include <Cron.h>
#include <UserInteractiveAuth.h>
static void static void
TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args) TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args)
@ -607,7 +608,7 @@ main(int argc, char **argv)
Log(lc, LOG_DEBUG, "Registering jobs..."); Log(lc, LOG_DEBUG, "Registering jobs...");
/* TODO: Register jobs here */ CronEvery(cron, 30 * 60 * 1000, (JobFunc *) UserInteractiveAuthCleanup, &matrixArgs);
Log(lc, LOG_NOTICE, "Starting job scheduler..."); Log(lc, LOG_NOTICE, "Starting job scheduler...");
CronStart(cron); CronStart(cron);

View file

@ -135,7 +135,7 @@ UserInteractiveAuth(HttpServerContext * context, Db * db,
} }
void void
UserInteractiveAuthCleanup(Db * db) UserInteractiveAuthCleanup(MatrixHttpHandlerArgs *args)
{ {
Log(args->lc, LOG_DEBUG, "Purging old user interactive auth sessions...");
} }

View file

@ -26,14 +26,16 @@
typedef struct Cron Cron; typedef struct Cron Cron;
typedef void (JobFunc) (void *);
extern Cron * extern Cron *
CronCreate(unsigned long); CronCreate(unsigned long);
extern void extern void
CronOnce(Cron *, void (*) (void *), void *); CronOnce(Cron *, JobFunc *, void *);
extern void extern void
CronEvery(Cron *, unsigned long, void (*) (void *), void *); CronEvery(Cron *, unsigned long, JobFunc *, void *);
extern void extern void
CronStart(Cron *); CronStart(Cron *);

View file

@ -26,10 +26,10 @@
#include <HashMap.h> #include <HashMap.h>
#include <HttpServer.h> #include <HttpServer.h>
#include <Db.h> #include <Matrix.h>
extern void extern void
UserInteractiveAuthCleanup(Db *); UserInteractiveAuthCleanup(MatrixHttpHandlerArgs *);
extern HashMap * extern HashMap *
UserInteractiveAuth(HttpServerContext *, Db *, HashMap *); UserInteractiveAuth(HttpServerContext *, Db *, HashMap *);