Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/hm/more-healthcheck-subcommands'…
Browse files Browse the repository at this point in the history
… into hm/butterflynet-reset-2
  • Loading branch information
hanabi1224 committed Feb 6, 2025
2 parents 9221c32 + c3590a2 commit 067bc58
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
92 changes: 62 additions & 30 deletions src/cli/subcommands/healthcheck_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ pub enum HealthcheckCommand {
#[arg(long, default_value_t=DEFAULT_HEALTHCHECK_PORT)]
healthcheck_port: u16,
},
/// Display live status
Live {
/// Don't exit until node is ready
#[arg(long)]
wait: bool,
/// Healthcheck port
#[arg(long, default_value_t=DEFAULT_HEALTHCHECK_PORT)]
healthcheck_port: u16,
},
/// Display live status
Healthy {
/// Don't exit until node is ready
#[arg(long)]
wait: bool,
/// Healthcheck port
#[arg(long, default_value_t=DEFAULT_HEALTHCHECK_PORT)]
healthcheck_port: u16,
},
}

impl HealthcheckCommand {
Expand All @@ -31,42 +49,56 @@ impl HealthcheckCommand {
Self::Ready {
wait,
healthcheck_port,
} => {
let ticker = Ticker::new(0.., Duration::from_secs(1));
let mut stdout = stdout();
} => Self::check(&client, "readyz", healthcheck_port, wait).await,
Self::Live {
wait,
healthcheck_port,
} => Self::check(&client, "livez", healthcheck_port, wait).await,
Self::Healthy {
wait,
healthcheck_port,
} => Self::check(&client, "healthz", healthcheck_port, wait).await,
}
}

async fn check(
client: &rpc::Client,
endpoint: &str,
healthcheck_port: u16,
wait: bool,
) -> anyhow::Result<()> {
let ticker = Ticker::new(0.., Duration::from_secs(1));
let mut stdout = stdout();

let url = format!(
"http://{}:{}/readyz?verbose",
client.base_url().host_str().unwrap_or("localhost"),
healthcheck_port,
);
let url = format!(
"http://{}:{healthcheck_port}/{endpoint}?verbose",
client.base_url().host_str().unwrap_or("localhost"),
);

for _ in ticker {
let response = reqwest::get(&url).await?;
let status = response.status();
let text = response.text().await?;
for _ in ticker {
let response = reqwest::get(&url).await?;
let status = response.status();
let text = response.text().await?;

println!("{}", text);
println!("{}", text);

if !wait {
break;
}
if status == StatusCode::OK {
println!("Done!");
break;
}
if !wait {
break;
}
if status == StatusCode::OK {
println!("Done!");
break;
}

for _ in 0..(text.matches('\n').count() + 1) {
write!(
stdout,
"\r{}{}",
anes::MoveCursorUp(1),
anes::ClearLine::All,
)?;
}
}
Ok(())
for _ in 0..(text.matches('\n').count() + 1) {
write!(
stdout,
"\r{}{}",
anes::MoveCursorUp(1),
anes::ClearLine::All,
)?;
}
}
Ok(())
}
}
4 changes: 3 additions & 1 deletion src/health/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const VERBOSE_PARAM: &str = "verbose";
/// In our case, we require:
/// - The node is not in an error state (i.e., boot-looping)
/// - At least 1 peer is connected (without peers, the node is isolated and cannot sync)
/// - The RPC server is running if not disabled
///
/// If any of these conditions are not met, the node is **not** healthy. If this happens for a prolonged period of time, the application should be restarted.
pub(crate) async fn livez(
Expand All @@ -29,6 +30,7 @@ pub(crate) async fn livez(
let mut lively = true;
lively &= check_sync_state_not_error(&state, &mut acc);
lively &= check_peers_connected(&state, &mut acc);
lively &= check_rpc_server_running(&state, &mut acc).await;

if lively {
Ok(acc.result_ok())
Expand All @@ -42,7 +44,7 @@ pub(crate) async fn livez(
/// In our case, we require:
/// - The node is in sync with the network
/// - The current epoch of the node is not too far behind the network
/// - The RPC server is running
/// - The RPC server is running if not disabled
/// - The Ethereum mapping is up to date
/// - The F3 side car is running if enabled
///
Expand Down

0 comments on commit 067bc58

Please sign in to comment.