Skip to content

Commit

Permalink
feat(jstzd): implement OctezNode health check
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Sep 26, 2024
1 parent 13a3519 commit d10eab8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async-trait.workspace = true
bollard.workspace = true
futures-util.workspace = true
octez = { path = "../octez" }
reqwest.workspace = true
serde.workspace = true
tempfile.workspace = true
tokio.workspace = true
Expand Down
25 changes: 21 additions & 4 deletions crates/jstzd/src/task/octez_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DEFAULT_RPC_ENDPOINT: &str = "localhost:8732";
const DEFAULT_NETWORK: &str = "sandbox";
const DEFAULT_BINARY_PATH: &str = "octez-node";

#[derive(Clone)]
#[derive(Default, Clone)]
pub struct OctezNodeConfig {
/// Path to the octez node binary.
binary_path: PathBuf,
Expand Down Expand Up @@ -132,6 +132,7 @@ impl AsyncDrop for ChildWrapper {
#[derive(Default, Clone)]
pub struct OctezNode {
inner: Arc<RwLock<AsyncDropper<ChildWrapper>>>,
config: OctezNodeConfig,
}

#[async_trait]
Expand All @@ -141,8 +142,8 @@ impl Task for OctezNode {
/// Spins up the task with the given config.
async fn spawn(config: Self::Config) -> Result<Self> {
let node = AsyncOctezNode {
octez_node_bin: Some(config.binary_path),
octez_node_dir: config.data_dir,
octez_node_bin: Some(config.binary_path.clone()),
octez_node_dir: config.data_dir.clone(),
};

node.generate_identity().await?;
Expand All @@ -163,6 +164,7 @@ impl Task for OctezNode {
.await?,
),
}))),
config,
})
}

Expand All @@ -174,7 +176,22 @@ impl Task for OctezNode {

/// Conducts a health check on the running task.
async fn health_check(&self) -> Result<bool> {
todo!()
// Returns whether or not the node is ready to answer to requests.
// https://gitlab.com/tezos/tezos/-/raw/2e84c439c25c4d9b363127a6685868e223877034/docs/api/rpc-openapi.json
let res =
reqwest::get(format!("http://{}/health/ready", &self.config.rpc_endpoint))
.await;
if res.is_err() {
return Ok(false);
}
let body = res
.unwrap()
.json::<std::collections::HashMap<String, bool>>()
.await?;
if let Some(v) = body.get("ready") {
return Ok(*v);
}
return Err(anyhow::anyhow!("unexpected error: `ready` cannot be retrieved from octez-node health check endpoint"));
}
}

Expand Down
15 changes: 2 additions & 13 deletions crates/jstzd/tests/octez_node_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,13 @@ async fn octez_node_test() {
.await
.unwrap();

let health_check_endpoint = format!("http://{}/health/ready", rpc_endpoint);
// Should be able to hit the endpoint since the node should have been launched
let node_ready = retry(10, 1000, || async {
let res = reqwest::get(&health_check_endpoint).await;
if let Ok(raw_body) = res {
let body = raw_body
.json::<std::collections::HashMap<String, bool>>()
.await
.unwrap();
return body.get("ready").cloned().ok_or(anyhow::anyhow!(""));
}
Err(anyhow::anyhow!(""))
})
.await;
let node_ready = retry(10, 1000, || async { f.health_check().await }).await;
assert!(node_ready);

let _ = f.kill().await;
// Wait for the process to shutdown entirely
let health_check_endpoint = format!("http://{}/health/ready", rpc_endpoint);
let node_destroyed = retry(10, 1000, || async {
let res = reqwest::get(&health_check_endpoint).await;
// Should get an error since the node should have been terminated
Expand Down

0 comments on commit d10eab8

Please sign in to comment.