]> git.ipfire.org Git - pakfire.git/commitdiff
job: Send log messages to the build service
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Oct 2024 14:18:26 +0000 (14:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Oct 2024 14:18:26 +0000 (14:18 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/job.c

index 2a377afa461887fb5506209b20acd4f534a98083..e66d7ea5d8ebffa1c1770e949a7daa0dde761b2b 100644 (file)
@@ -287,6 +287,72 @@ int pakfire_job_exited(sd_event_source* s, const siginfo_t* si, void* data) {
        return 0;
 }
 
+static int pakfire_job_send_log(struct pakfire_job* job, int priority, const char* line, size_t length) {
+       struct json_object* message = NULL;
+       struct json_object* data = NULL;
+       int r;
+
+       // Bail if we don't have a control connection
+       if (!job->control) {
+               CTX_DEBUG(job-ctx, "Cannot send log message because the control connection is down\n");
+               return 0;
+       }
+
+       // Create a new JSON object
+       data = json_object_new_object();
+       if (!data) {
+               CTX_ERROR(job->ctx, "Could not create a new JSON object: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Add the priority
+       r = pakfire_json_add_uint64(data, "priority", priority);
+       if (r)
+               goto ERROR;
+
+       // Add the line
+       r = pakfire_json_add_stringn(data, "line", line, length);
+       if (r)
+               goto ERROR;
+
+       // Create a new stats object
+       message = json_object_new_object();
+       if (!message) {
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Set type
+       r = pakfire_json_add_string(message, "type", "log");
+       if (r)
+               goto ERROR;
+
+       // Set data
+       r = json_object_object_add(message, "data", json_object_get(data));
+       if (r)
+               goto ERROR;
+
+       // Serialize to string
+       const char* m = json_object_to_json_string_length(message,
+               JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY, &length);
+
+       // Send the message
+       r = pakfire_xfer_send_message(job->control, m, length);
+       if (r) {
+               CTX_ERROR(job->ctx, "Could not send log message: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
+ERROR:
+       if (message)
+               json_object_put(message);
+       if (data)
+               json_object_put(data);
+
+       return r;
+}
+
 static int __pakfire_job_log_forward(struct pakfire_job* job, int priority, uint32_t events, struct log* log) {
        char* line = NULL;
        ssize_t length = 0;
@@ -300,6 +366,11 @@ static int __pakfire_job_log_forward(struct pakfire_job* job, int priority, uint
                        if (length < 0)
                                break;
 
+                       // Send the line to the build service
+                       r = pakfire_job_send_log(job, priority, line, length);
+                       if (r < 0)
+                               return r;
+
                        // Enqueue the line in the log buffer
                        r = pakfire_log_buffer_enqueue(job->log.buffer, priority, line, length);
                        if (r < 0) {