]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Submit stats regularly
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Sep 2024 02:37:17 +0000 (02:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Sep 2024 02:37:17 +0000 (02:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/daemon.c

index beca68728ca6eef95b275c3116174b2c0dbb776f..9ceac6f9afbaddca4594795b98ebcbb1ff2e3529 100644 (file)
@@ -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)