From 5d843a488ec349d94e22314e90a73885ebfce007 Mon Sep 17 00:00:00 2001 From: default Date: Tue, 20 Sep 2022 12:00:13 +0200 Subject: [PATCH] New function enqueue(). --- data.c | 37 ++++++++++++++++++++++++++++++++++++- main.c | 2 +- snac.c | 4 ++-- snac.h | 2 +- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/data.c b/data.c index 667d1e6..fe21be6 100644 --- a/data.c +++ b/data.c @@ -391,7 +391,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) } /* build the new filename */ - xs *ntid = tid(); + xs *ntid = tid(0); xs *md5 = xs_md5_hex(id, strlen(id)); xs *fn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5); xs *md; @@ -519,3 +519,38 @@ int is_muted(snac *snac, char *actor) return !!(mtime(fn) != 0.0); } + + +void enqueue(snac *snac, char *actor, char *msg, int retries) +/* enqueues a message for an actor */ +{ + if (strcmp(actor, snac->actor) == 0) { + snac_debug(snac, 1, xs_str_new("enqueue refused to myself")); + return; + } + + int qrt = xs_number_get(xs_dict_get(srv_config, "query_retry_minutes")); + xs *ntid = tid(retries * 60 * qrt); + xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid); + xs *tfn = xs_str_cat(fn, ".tmp"); + FILE *f; + + if ((f = fopen(tfn, "w")) != NULL) { + xs *qmsg = xs_dict_new(); + xs *rn = xs_number_new(retries); + xs *j; + + qmsg = xs_dict_append(qmsg, "actor", actor); + qmsg = xs_dict_append(qmsg, "object", msg); + qmsg = xs_dict_append(qmsg, "retries", rn); + + j = xs_json_dumps_pp(qmsg, 4); + + fwrite(j, strlen(j), 1, f); + fclose(f); + + rename(tfn, fn); + + snac_debug(snac, 2, xs_fmt("enqueue %s %s %d", actor, fn, retries)); + } +} diff --git a/main.c b/main.c index 4a9690b..4f77c0d 100644 --- a/main.c +++ b/main.c @@ -9,7 +9,7 @@ int main(int argc, char *argv[]) { snac snac; - printf("%s\n", tid()); + printf("%s\n", tid(0)); srv_open("/home/angel/lib/snac/comam.es/"); diff --git a/snac.c b/snac.c index 3ba3d38..fc1fe7b 100644 --- a/snac.c +++ b/snac.c @@ -42,7 +42,7 @@ d_char *xs_time(char *fmt, int local) } -d_char *tid(void) +d_char *tid(int offset) /* returns a time-based Id */ { struct timeval tv; @@ -50,7 +50,7 @@ d_char *tid(void) gettimeofday(&tv, &tz); - return xs_fmt("%10d.%06d", tv.tv_sec, tv.tv_usec); + return xs_fmt("%10d.%06d", tv.tv_sec + offset, tv.tv_usec); } diff --git a/snac.h b/snac.h index 1e4a1a6..8f6d32c 100644 --- a/snac.h +++ b/snac.h @@ -11,7 +11,7 @@ d_char *xs_time(char *fmt, int local); #define xs_local_time(fmt) xs_time(fmt, 1) #define xs_utc_time(fmt) xs_time(fmt, 0) -d_char *tid(void); +d_char *tid(int offset); void srv_debug(int level, d_char *str); #define srv_log(str) srv_debug(0, str)