Skip to content

Commit

Permalink
Merge pull request #285 from monadicus/feat/misc-task-finishing
Browse files Browse the repository at this point in the history
Feat: misc task finishing
  • Loading branch information
gluax authored Sep 17, 2024
2 parents ae44e41 + 8914dc1 commit 4db274e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
6 changes: 4 additions & 2 deletions crates/aot/src/runner/rpc/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ pub struct NodeRpcServer<N: Network> {
}

impl<N: Network> NodeService for NodeRpcServer<N> {
async fn set_log_level(self, _: context::Context, verbosity: u8) -> Result<(), AgentError> {
tracing::debug!("NodeService Setting log verbosity {verbosity:?}");
async fn status(self, _: context::Context) -> Result<(), AgentError> {
Ok(())
}

async fn set_log_level(self, _: context::Context, verbosity: u8) -> Result<(), AgentError> {
self.log_level_handler
.modify(|filter| *filter = make_env_filter(verbosity))
.map_err(|_| AgentError::FailedToChangeLogLevel)?;
Expand Down
1 change: 1 addition & 0 deletions crates/snops-agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ async fn main() {
.execute(
AgentRpcServer {
state: state.to_owned(),
version: env!("CARGO_PKG_VERSION"),
}
.serve(),
)
Expand Down
14 changes: 13 additions & 1 deletion crates/snops-agent/src/rpc/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use snops_common::{
rpc::{
control::{
agent::{
AgentMetric, AgentService, AgentServiceRequest, AgentServiceResponse, Handshake,
AgentMetric, AgentService, AgentServiceRequest, AgentServiceResponse, AgentStatus,
Handshake,
},
ControlServiceRequest, ControlServiceResponse,
},
Expand All @@ -42,6 +43,7 @@ define_rpc_mux!(child;
#[derive(Clone)]
pub struct AgentRpcServer {
pub state: AppState,
pub version: &'static str,
}

impl AgentService for AgentRpcServer {
Expand Down Expand Up @@ -624,4 +626,14 @@ impl AgentService for AgentRpcServer {
.await
.map_err(|_| AgentError::FailedToMakeRequest)?
}

async fn get_status(self, ctx: context::Context) -> Result<AgentStatus, AgentError> {
let lock = self.state.node_client.lock().await;
let node_client = lock.as_ref().ok_or(AgentError::NodeClientNotSet)?;

Ok(AgentStatus {
aot_online: node_client.status(ctx).await.is_ok(),
version: self.version.to_string(),
})
}
}
8 changes: 8 additions & 0 deletions crates/snops-cli/src/commands/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ enum AgentCommands {
/// Get the specific agent's TPS.
Tps,

/// Get the specific agent's status.
Status,

SetLogLevel {
/// The log level to set.
level: String,
Expand Down Expand Up @@ -138,6 +141,11 @@ impl Agent {

client.post(ep).send()?
}
Status => {
let ep = format!("{url}/api/v1/agents/{}/status", self.id);

client.get(ep).send()?
}
Tps => {
let ep = format!("{url}/api/v1/agents/{}/tps", self.id);

Expand Down
1 change: 1 addition & 0 deletions crates/snops-common/src/rpc/agent/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{rpc::error::AgentError, state::snarkos_status::SnarkOSLiteBlock};
#[tarpc::service]
pub trait NodeService {
// todo @gluax this should return an A different kind of error.
async fn status() -> Result<(), AgentError>;
async fn set_log_level(verbosity: u8) -> Result<(), AgentError>;
async fn get_block_lite(block_hash: String) -> Result<Option<SnarkOSLiteBlock>, AgentError>;
async fn find_transaction(tx_id: String) -> Result<Option<String>, AgentError>;
Expand Down
8 changes: 8 additions & 0 deletions crates/snops-common/src/rpc/control/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ pub trait AgentService {
) -> Result<Option<SnarkOSLiteBlock>, AgentError>;

async fn set_aot_log_level(verbosity: u8) -> Result<(), AgentError>;

async fn get_status() -> Result<AgentStatus, AgentError>;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentStatus {
pub aot_online: bool,
pub version: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
15 changes: 15 additions & 0 deletions crates/snops/src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub(super) fn routes() -> Router<AppState> {
.route("/log/:level", post(set_log_level))
.route("/agents", get(get_agents))
.route("/agents/:id", get(get_agent))
.route("/agents/:id/status", get(get_agent_status))
.route("/agents/:id/kill", post(kill_agent))
.route("/agents/:id/tps", get(get_agent_tps))
.route("/agents/:id/log/:level", post(set_agent_log_level))
Expand Down Expand Up @@ -333,6 +334,20 @@ async fn get_agent(state: State<AppState>, Path(id): Path<String>) -> Response {
Json(AgentStatusResponse::from(agent.value())).into_response()
}

async fn get_agent_status(state: State<AppState>, Path(id): Path<String>) -> Response {
let id = unwrap_or_not_found!(id_or_none(&id));
let agent = unwrap_or_not_found!(state.pool.get(&id));

let Some(rpc) = agent.rpc() else {
return StatusCode::SERVICE_UNAVAILABLE.into_response();
};

match rpc.get_status(tarpc::context::current()).await {
Ok(status) => Json(status).into_response(),
Err(_e) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
}
}

async fn kill_agent(state: State<AppState>, Path(id): Path<String>) -> Response {
let id = unwrap_or_not_found!(id_or_none(&id));
let client = unwrap_or_not_found!(state.pool.get(&id).and_then(|a| a.client_owned()));
Expand Down

0 comments on commit 4db274e

Please sign in to comment.