Skip to content

Commit

Permalink
API improvements
Browse files Browse the repository at this point in the history
- agent exits gracefully when no testrun available
- API doesn't log every error
  • Loading branch information
dynco-nym committed Nov 5, 2024
1 parent cbe9c06 commit 088fe7b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
37 changes: 25 additions & 12 deletions nym-node-status-agent/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::bail;
use clap::{Parser, Subcommand};
use nym_bin_common::bin_info;
use nym_common_models::ns_api::TestrunAssignment;
Expand Down Expand Up @@ -51,11 +52,13 @@ impl Args {
let version = probe.version().await;
tracing::info!("Probe version:\n{}", version);

let testrun = request_testrun(&server_address).await?;
if let Some(testrun) = request_testrun(&server_address).await? {
let log = probe.run_and_get_log(&Some(testrun.gateway_identity_key));

let log = probe.run_and_get_log(&Some(testrun.gateway_identity_key));

submit_results(&server_address, testrun.testrun_id, log).await?;
submit_results(&server_address, testrun.testrun_id, log).await?;
} else {
tracing::info!("No testruns available, exiting")
}

Ok(())
}
Expand All @@ -64,16 +67,26 @@ impl Args {
const URL_BASE: &str = "internal/testruns";

#[instrument(level = "debug", skip_all)]
async fn request_testrun(server_addr: &str) -> anyhow::Result<TestrunAssignment> {
async fn request_testrun(server_addr: &str) -> anyhow::Result<Option<TestrunAssignment>> {
let target_url = format!("{}/{}", server_addr, URL_BASE);
let client = reqwest::Client::new();
let res = client
.get(target_url)
.send()
.await
.and_then(|response| response.error_for_status())?;
res.json()
.await
let res = client.get(target_url).send().await?;
let status = res.status();
let response_text = res.text().await?;

if status.is_client_error() {
bail!("{}: {}", status, response_text);
} else if status.is_server_error() {
if matches!(status, reqwest::StatusCode::SERVICE_UNAVAILABLE)
&& response_text.contains("No testruns available")
{
return Ok(None);
} else {
bail!("{}: {}", status, response_text);
}
}

serde_json::from_str(&response_text)
.map(|testrun| {
tracing::info!("Received testrun assignment: {:?}", testrun);
testrun
Expand Down
10 changes: 2 additions & 8 deletions nym-node-status-api/src/http/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use anyhow::anyhow;
use axum::{response::Redirect, Router};
use tokio::net::ToSocketAddrs;
use tower_http::{
cors::CorsLayer,
trace::{DefaultOnResponse, TraceLayer},
};
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

Expand Down Expand Up @@ -61,10 +58,7 @@ impl RouterBuilder {
// CORS layer needs to wrap other API layers
.layer(setup_cors())
// logger should be outermost layer
.layer(
TraceLayer::new_for_http()
.on_response(DefaultOnResponse::new().level(tracing::Level::DEBUG)),
)
.layer(TraceLayer::new_for_http())
}
}

Expand Down
3 changes: 2 additions & 1 deletion nym-node-status-api/src/http/api/testruns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ async fn request_testrun(State(state): State<AppState>) -> HttpResult<Json<Testr
);
Ok(Json(testrun))
} else {
Err(HttpError::no_available_testruns())
tracing::debug!("No testruns available for agent");
Err(HttpError::no_testruns_available())
}
}
Err(err) => Err(HttpError::internal_with_logging(err)),
Expand Down
4 changes: 2 additions & 2 deletions nym-node-status-api/src/http/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl HttpError {
}
}

pub(crate) fn no_available_testruns() -> Self {
pub(crate) fn no_testruns_available() -> Self {
Self {
message: serde_json::json!({"message": "No available testruns"}).to_string(),
message: serde_json::json!({"message": "No testruns available"}).to_string(),
status: axum::http::StatusCode::SERVICE_UNAVAILABLE,
}
}
Expand Down

0 comments on commit 088fe7b

Please sign in to comment.