// PID File Descriptor
int pidfd;
- // Process Event
- sd_event_source* process;
+ // Event Loop
+ sd_event* loop;
+
+ // HTTP Client
+ struct pakfire_httpclient* client;
};
static int pakfire_parse_job(struct pakfire_worker* worker, json_object* data) {
}
static void pakfire_worker_free(struct pakfire_worker* worker) {
+ if (worker->client)
+ pakfire_httpclient_unref(worker->client);
+ if (worker->loop)
+ sd_event_unref(worker->loop);
if (worker->daemon)
pakfire_daemon_unref(worker->daemon);
if (worker->ctx)
free(worker);
}
+static int pakfire_worker_setup_loop(struct pakfire_worker* worker) {
+ int r;
+
+ // Create a new event loop
+ r = sd_event_new(&worker->loop);
+ if (r < 0) {
+ CTX_ERROR(worker->ctx, "Could not setup event loop: %s\n", strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
+static int pakfire_worker_setup_httpclient(struct pakfire_worker* worker) {
+ struct pakfire_config* config = NULL;
+ int r;
+
+ // Fetch the configuration
+ config = pakfire_ctx_get_config(worker->ctx);
+ if (!config) {
+ CTX_ERROR(worker->ctx, "Could not fetch configuration: %m\n");
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Fetch the URL
+ const char* url = pakfire_config_get(config, "daemon", "url", "https://pakfire.ipfire.org");
+
+ // Create the HTTP client
+ r = pakfire_httpclient_create(&worker->client, worker->ctx, worker->loop);
+ if (r)
+ goto ERROR;
+
+ // Configure the base URL
+ r = pakfire_httpclient_set_baseurl(worker->client, url);
+ if (r) {
+ CTX_ERROR(worker->ctx, "Could not configure the URL\n");
+ goto ERROR;
+ }
+
+ERROR:
+ if (config)
+ pakfire_config_unref(config);
+
+ return r;
+}
+
int pakfire_worker_create(struct pakfire_worker** worker, struct pakfire_ctx* ctx,
struct pakfire_daemon* daemon, json_object* data) {
struct pakfire_worker* w = NULL;
// Store a reference to the daemon
w->daemon = pakfire_daemon_ref(daemon);
+ // Setup a new event loop
+ r = pakfire_worker_setup_loop(w);
+ if (r)
+ goto ERROR;
+
+ // Setup the HTTP client
+ r = pakfire_worker_setup_httpclient(w);
+ if (r)
+ goto ERROR;
+
// Parse the job
r = pakfire_parse_job(w, data);
if (r)
}
static int pakfire_worker_child(struct pakfire_worker* worker) {
+ int r;
+
// Fetch our PID
pid_t pid = getpid();
CTX_DEBUG(worker->ctx, "Launched worker child as PID %d\n", pid);
- // XXX TODO
+ // Run the main loop
+ r = sd_event_loop(worker->loop);
+ if (r < 0) {
+ CTX_ERROR(worker->ctx, "Could not run the event loop: %s\n", strerror(-r));
+ goto ERROR;
+ }
- return 0;
+ERROR:
+ return r;
}
/*