From 08f99232e0854529bfd6f74d22d56fd4c8cc4cb5 Mon Sep 17 00:00:00 2001 From: grunfink Date: Fri, 12 Sep 2025 21:48:34 +0200 Subject: New function instance_failure(). --- data.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'data.c') diff --git a/data.c b/data.c index 1533305..5c72184 100644 --- a/data.c +++ b/data.c @@ -112,6 +112,9 @@ int srv_open(const char *basedir, int auto_upgrade) xs *tmpdir = xs_fmt("%s/tmp", srv_basedir); mkdirx(tmpdir); + xs *faildir = xs_fmt("%s/failure", srv_basedir); + mkdirx(faildir); + #ifdef __APPLE__ /* Apple uses st_atimespec instead of st_atim etc */ #define st_atim st_atimespec @@ -3039,6 +3042,56 @@ xs_list *content_search(snac *user, const char *regex, } +int instance_failure(const char *url, int op) +/* do some checks and accounting on instance failures */ +{ + int ret = 0; + xs *l = xs_split(url, "/"); + const char *hostname = xs_list_get(l, 2); + double mt; + + if (!xs_is_string(hostname)) + return 0; + + xs *md5 = xs_md5_hex(hostname, strlen(hostname)); + xs *fn = xs_fmt("%s/%s", srv_basedir, md5); + + switch (op) { + case 0: /** check **/ + if ((mt = mtime(fn)) != 0.0) { + /* grace time */ + double seconds_failing = 30 * (24 * 60 * 60); + + if ((double)time(NULL) - mt > seconds_failing) + ret = -1; + } + + break; + + case 1: /** register a failure **/ + if (mtime(fn) == 0.0) { + FILE *f; + + /* only create once, as the date will be used */ + if ((f = fopen(fn, "w")) != NULL) { + fprintf(f, "%s\n", hostname); + fclose(f); + } + } + + break; + + case 2: /** clear a failure **/ + /* called whenever a message comes from this instance */ + unlink(fn); + + break; + } + + return ret; +} + + /** notifications **/ xs_str *notify_check_time(snac *snac, int reset) -- cgit v1.2.3