Skip to content

Commit

Permalink
feat: supply discord username on launchpad
Browse files Browse the repository at this point in the history
The Discord username is hooked up to the `safenode` `--owner` argument in the service definition.
  • Loading branch information
jacderida authored and joshuef committed May 19, 2024
1 parent 8dfde99 commit 7248531
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion node-launchpad/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl App {
let app_data = AppData::load()?;

let tab = Tab::default();
let home = Home::new(app_data.allocated_disk_space)?;
let home = Home::new(app_data.allocated_disk_space, &app_data.discord_username)?;
let config = Config::new()?;
let discord_username_input =
DiscordUsernameInputBox::new(app_data.discord_username.clone());
Expand Down
29 changes: 22 additions & 7 deletions node-launchpad/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ pub struct Home {
node_services: Vec<NodeServiceData>,
node_table_state: TableState,
allocated_disk_space: usize,
discord_username: String,
// Currently the node registry file does not support concurrent actions and thus can lead to
// inconsistent state. A simple file lock or a db like file would work.
lock_registry: bool,
}

impl Home {
pub fn new(allocated_disk_space: usize) -> Result<Self> {
pub fn new(allocated_disk_space: usize, discord_username: &str) -> Result<Self> {
let mut home = Self {
action_sender: Default::default(),
config: Default::default(),
Expand All @@ -47,6 +48,7 @@ impl Home {
allocated_disk_space,
node_table_state: Default::default(),
lock_registry: Default::default(),
discord_username: discord_username.to_string(),
};
home.load_node_registry_and_update_states()?;

Expand Down Expand Up @@ -178,24 +180,32 @@ impl Component for Home {
Action::StoreAllocatedDiskSpace(space) => {
self.allocated_disk_space = space;
}
Action::StoreDiscordUserName(username) => {
self.discord_username = username;
}
Action::HomeActions(HomeActions::StartNodes) => {
if self.lock_registry {
error!("Registry is locked. Cannot start node now.");
return Ok(None);
}

if self.allocated_disk_space == 0 {
info!("Disk space not allocated, ask for input");
// trigger resource allocation if not set
info!("Disk space not allocated. Ask for input.");
return Ok(Some(Action::HomeActions(
HomeActions::TriggerResourceAllocationInputBox,
)));
}
if self.discord_username.is_empty() {
info!("Discord username not assigned. Ask for input.");
return Ok(Some(Action::HomeActions(
HomeActions::TriggerDiscordUsernameInputBox,
)));
}

let node_count = self.allocated_disk_space / GB_PER_NODE;
let running_nodes = self.get_running_nodes();

if running_nodes.len() > node_count {
// stop some nodes
let to_stop_count = running_nodes.len() - node_count;
let nodes_to_stop = running_nodes
.into_iter()
Expand All @@ -212,7 +222,6 @@ impl Component for Home {
self.lock_registry = true;
stop_nodes(nodes_to_stop, action_sender);
} else if running_nodes.len() < node_count {
// run some nodes
let to_start_count = node_count - running_nodes.len();

let inactive_nodes = self.get_inactive_nodes();
Expand All @@ -229,7 +238,12 @@ impl Component for Home {
?to_add_count,
"We are adding+starting {to_add_count:?} nodes + starting these services: {inactive_nodes:?}"
);
add_and_start_nodes(to_add_count, inactive_nodes, action_sender);
add_and_start_nodes(
to_add_count,
self.discord_username.clone(),
inactive_nodes,
action_sender,
);
} else {
// start these nodes
let nodes_to_start =
Expand Down Expand Up @@ -427,6 +441,7 @@ fn start_nodes(services: Vec<String>, action_sender: UnboundedSender<Action>) {

fn add_and_start_nodes(
count: usize,
owner: String,
mut nodes_to_start: Vec<String>,
action_sender: UnboundedSender<Action>,
) {
Expand All @@ -443,7 +458,7 @@ fn add_and_start_nodes(
None,
None,
None,
None,
Some(owner),
peers,
None,
None,
Expand Down

0 comments on commit 7248531

Please sign in to comment.