From: Michael Tremer Date: Sun, 15 Sep 2024 02:37:17 +0000 (+0000) Subject: daemon: Submit stats regularly X-Git-Tag: 0.9.30~1200 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b9d528a80fc6afbb600b572f57f2e70b3c620217;p=pakfire.git daemon: Submit stats regularly Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c index beca68728..9ceac6f9a 100644 --- a/src/libpakfire/daemon.c +++ b/src/libpakfire/daemon.c @@ -46,6 +46,9 @@ struct pakfire_daemon { sd_event_source* connect_timer; unsigned int reconnect_holdoff; + // Timer for submitting stats + sd_event_source* stat_timer; + // Workers struct pakfire_worker* workers[MAX_WORKERS]; unsigned int running_workers; @@ -198,6 +201,36 @@ static int pakfire_daemon_connect(sd_event_source* s, uint64_t usec, void* data) return r; } +static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void* data) { + struct pakfire_daemon* daemon = data; + int r; + + // Submit stats + r = pakfire_buildservice_submit_stats(daemon->service); + if (r < 0) + CTX_ERROR(daemon->ctx, "Could not submit stats: %s\n", strerror(-r)); + + // If we have any workers running, we will submit our stats + // every five seconds, otherwise 30 seconds is enough. + uint64_t next = (daemon->running_workers > 0) ? 5000000 : 30000000; + + // Reset the timer + r = sd_event_source_set_time_relative(daemon->stat_timer, next); + if (r < 0) { + CTX_ERROR(daemon->ctx, "Could not set the stat timer: %s\n", strerror(-r)); + return r; + } + + // Activate the timer + r = sd_event_source_set_enabled(daemon->stat_timer, SD_EVENT_ONESHOT); + if (r < 0) { + CTX_ERROR(daemon->ctx, "Could not activate the stat timer: %s\n", strerror(-r)); + return r; + } + + return r; +} + static int pakfire_daemon_setup_loop(struct pakfire_daemon* daemon) { int r; @@ -239,12 +272,22 @@ static int pakfire_daemon_setup_loop(struct pakfire_daemon* daemon) { return r; } + // Submit stats + r = sd_event_add_time_relative(daemon->loop, &daemon->stat_timer, + CLOCK_MONOTONIC, 0, 0, pakfire_daemon_submit_stats, daemon); + if (r < 0) { + CTX_ERROR(daemon->ctx, "Could not register the stat timer: %s\n", strerror(-r)); + return r; + } + return 0; } static void pakfire_daemon_free(struct pakfire_daemon* daemon) { if (daemon->connect_timer) sd_event_source_unref(daemon->connect_timer); + if (daemon->stat_timer) + sd_event_source_unref(daemon->stat_timer); if (daemon->service) pakfire_buildservice_unref(daemon->service); if (daemon->loop)