Skip to content

Commit

Permalink
fix(rust): avoid a long-lived probing request (#1272)
Browse files Browse the repository at this point in the history
The `Probe` D-Bus function is a blocking one. It causes the call `POST
/api/manager/probe` to be a long-lived request which is causing some
problems (see #1254).

The proposed fix is to return immediately and run the request as a
separate Tokio task. After all, we will get an update on the status
through a WebSocket. If needed, we could also emit an error through the
WebSocket.
  • Loading branch information
imobachgs authored May 29, 2024
2 parents 7f8997d + 0411f0a commit 8e2ff3a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
31 changes: 26 additions & 5 deletions rust/agama-server/src/manager/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{

#[derive(Clone)]
pub struct ManagerState<'a> {
dbus: zbus::Connection,
manager: ManagerClient<'a>,
}

Expand Down Expand Up @@ -86,8 +87,8 @@ pub async fn manager_service(dbus: zbus::Connection) -> Result<Router, ServiceEr

let status_router = service_status_router(&dbus, DBUS_SERVICE, DBUS_PATH).await?;
let progress_router = progress_router(&dbus, DBUS_SERVICE, DBUS_PATH).await?;
let manager = ManagerClient::new(dbus).await?;
let state = ManagerState { manager };
let manager = ManagerClient::new(dbus.clone()).await?;
let state = ManagerState { manager, dbus };
Ok(Router::new()
.route("/probe", post(probe_action))
.route("/install", post(install_action))
Expand All @@ -100,16 +101,36 @@ pub async fn manager_service(dbus: zbus::Connection) -> Result<Router, ServiceEr
}

/// Starts the probing process.
// The Probe D-Bus method is blocking and will not return until the probing is finished. To avoid a
// long-lived HTTP connection, this method returns immediately (with a 200) and runs the request on
// a separate task.
#[utoipa::path(
get,
path = "/probe",
context_path = "/api/manager",
responses(
(status = 200, description = "The probing process was started.")
(
status = 200,
description = "The probing was requested but there is no way to know whether it succeeded."
)
)
)]
async fn probe_action(State(state): State<ManagerState<'_>>) -> Result<(), Error> {
state.manager.probe().await?;
async fn probe_action<'a>(State(state): State<ManagerState<'a>>) -> Result<(), Error> {
let dbus = state.dbus.clone();
tokio::spawn(async move {
let result = dbus
.call_method(
Some("org.opensuse.Agama.Manager1"),
"/org/opensuse/Agama/Manager1",
Some("org.opensuse.Agama.Manager1"),
"Probe",
&(),
)
.await;
if let Err(error) = result {
tracing::error!("Could not start probing: {:?}", error);
}
});
Ok(())
}

Expand Down
6 changes: 6 additions & 0 deletions rust/package/agama.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Wed May 29 10:40:21 UTC 2024 - Imobach Gonzalez Sosa <[email protected]>

- The HTTP request to perform a probing is not blocking anymore
(gh#openSUSE/agama#1272).

-------------------------------------------------------------------
Mon May 27 14:11:55 UTC 2024 - Imobach Gonzalez Sosa <[email protected]>

Expand Down

0 comments on commit 8e2ff3a

Please sign in to comment.