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(swarm): list all running instances (ports etc) #1051

Merged
merged 4 commits into from
Jun 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl AllocatedPorts {
self.ports[name]
}

pub fn into_ports(self) -> HashMap<&'static str, u16> {
self.ports
}

pub async fn get_or_next_port(&mut self, name: &'static str) -> u16 {
if let Some(port) = self.ports.get(name) {
return *port;
Expand Down
48 changes: 45 additions & 3 deletions applications/tari_swarm_daemon/src/webserver/rpc/instances.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::{collections::HashMap, path::PathBuf};

use axum_jrpc::error::{JsonRpcError, JsonRpcErrorReason};
use serde::{Deserialize, Serialize};

use crate::webserver::context::HandlerContext;
use crate::{config::InstanceType, process_manager::InstanceId, webserver::context::HandlerContext};

pub type StartInstanceRequest = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
pub struct StartInstanceResponse {
pub success: bool,
}
Expand Down Expand Up @@ -38,7 +40,7 @@ pub async fn start(

pub type StopInstanceRequest = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
pub struct StopInstanceResponse {
pub success: bool,
}
Expand All @@ -63,6 +65,46 @@ pub async fn stop(context: &HandlerContext, req: StopInstanceRequest) -> Result<
Ok(StopInstanceResponse { success: true })
}

#[derive(Debug, Clone, Deserialize)]
pub struct ListInstancesRequest {
pub by_type: Option<InstanceType>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ListInstancesResponse {
pub instances: Vec<InstanceInfo>,
}

#[derive(Debug, Clone, Serialize)]
pub struct InstanceInfo {
pub id: InstanceId,
pub name: String,
pub ports: HashMap<&'static str, u16>,
pub base_path: PathBuf,
pub instance_type: InstanceType,
pub is_running: bool,
}

impl From<crate::process_manager::InstanceInfo> for InstanceInfo {
fn from(value: crate::process_manager::InstanceInfo) -> Self {
Self {
id: value.id,
name: value.name,
ports: value.ports.into_ports(),
base_path: value.base_path,
instance_type: value.instance_type,
is_running: value.is_running,
}
}
}

pub async fn list(context: &HandlerContext, req: ListInstancesRequest) -> Result<ListInstancesResponse, anyhow::Error> {
let instances = context.process_manager().list_instances(req.by_type).await?;
Ok(ListInstancesResponse {
instances: instances.into_iter().map(Into::into).collect(),
})
}

#[derive(Debug, Clone, Deserialize)]
pub struct DeleteInstanceDataRequest {
pub name: String,
Expand Down
1 change: 1 addition & 0 deletions applications/tari_swarm_daemon/src/webserver/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async fn json_rpc_handler(Extension(context): Extension<Arc<HandlerContext>>, va
"add_validator_node" => call_handler(context, value, rpc::validator_nodes::create).await,
"start" => call_handler(context, value, rpc::instances::start).await,
"stop" => call_handler(context, value, rpc::instances::stop).await,
"list_instances" => call_handler(context, value, rpc::instances::list).await,
"delete_data" => call_handler(context, value, rpc::instances::delete_data).await,
_ => Ok(value.method_not_found(&value.method)),
}
Expand Down
13 changes: 7 additions & 6 deletions applications/tari_swarm_daemon/webui/src/routes/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,13 @@ function ExtraInfoVN({name, url, setRow, addTxToPool, autoRefresh, state, horizo
return (<>
<hr/>
<h3>Pool transaction</h3>
<table style={{width: "100%"}}>
<tr>
<td>Tx Id</td>
<td>Ready</td>
<td>Decision</td>
<td>Stage</td>
<table style={{
width: "100%"}}>
<tr>
<td>Tx Id</td>
<td>Ready</td>
<td>Decision</td>
<td>Stage</td>
</tr>
{pool.map(({atom}, i) => (
<tr key={i}>
Expand Down
Loading