Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jstzd): Implement health check for OctezNode #566

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ bollard.workspace = true
futures-util.workspace = true
http.workspace = true
octez = { path = "../octez" }
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true
tokio.workspace = true

[dev-dependencies]
rand.workspace = true
reqwest.workspace = true

[[bin]]
name = "jstzd"
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 @@ pub 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(),
};

let status = node.generate_identity().await?.wait().await?;
Expand Down Expand Up @@ -176,6 +177,7 @@ impl Task for OctezNode {
.await?,
),
}))),
config,
})
}

Expand All @@ -187,7 +189,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()
ryutamago marked this conversation as resolved.
Show resolved Hide resolved
.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