cli: Implement `rad node stop`

This commit is contained in:
Alexis Sellier 2023-06-29 17:58:07 +02:00
parent 0d0d354d55
commit e32e28f8b4
No known key found for this signature in database
3 changed files with 15 additions and 7 deletions

View File

@ -44,9 +44,9 @@ pub fn start(daemon: bool, options: Vec<OsString>) -> anyhow::Result<()> {
} }
pub fn stop(node: Node) -> anyhow::Result<()> { pub fn stop(node: Node) -> anyhow::Result<()> {
let spinner = term::spinner("Stopping the node..."); let spinner = term::spinner("Stopping node...");
if let Err(err) = node.shutdown() { if node.shutdown().is_err() {
spinner.error(format!("Error occurred while shutting down node: {err}")); spinner.error("node is not running");
} else { } else {
spinner.finish(); spinner.finish();
} }

View File

@ -211,6 +211,7 @@ fn command<H: Handle<Error = runtime::HandleError> + 'static>(
// Channel might already be disconnected if shutdown // Channel might already be disconnected if shutdown
// came from somewhere else. Ignore errors. // came from somewhere else. Ignore errors.
handle.shutdown().ok(); handle.shutdown().ok();
CommandResult::ok().to_writer(writer).ok();
} }
} }
Ok(()) Ok(())

View File

@ -10,7 +10,7 @@ use std::io::{BufRead, BufReader};
use std::ops::Deref; use std::ops::Deref;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{fmt, io, net, time}; use std::{fmt, io, net, thread, time};
use amplify::WrapperMut; use amplify::WrapperMut;
use cyphernet::addr::{HostName, NetAddr}; use cyphernet::addr::{HostName, NetAddr};
@ -697,7 +697,14 @@ impl Handle for Node {
} }
fn shutdown(self) -> Result<(), Error> { fn shutdown(self) -> Result<(), Error> {
todo!(); for line in self.call::<&str, CommandResult>(CommandName::Shutdown, [], DEFAULT_TIMEOUT)? {
line?;
}
// Wait until the shutdown has completed.
while self.is_running() {
thread::sleep(time::Duration::from_secs(1));
}
Ok(())
} }
} }
@ -709,7 +716,7 @@ pub trait AliasStore {
impl<T: AliasStore + ?Sized> AliasStore for &T { impl<T: AliasStore + ?Sized> AliasStore for &T {
fn alias(&self, nid: &NodeId) -> Option<String> { fn alias(&self, nid: &NodeId) -> Option<String> {
dbg!((*self).alias(nid)) (*self).alias(nid)
} }
} }
@ -721,7 +728,7 @@ impl<T: AliasStore + ?Sized> AliasStore for Box<T> {
impl AliasStore for HashMap<NodeId, String> { impl AliasStore for HashMap<NodeId, String> {
fn alias(&self, nid: &NodeId) -> Option<String> { fn alias(&self, nid: &NodeId) -> Option<String> {
dbg!(self.get(nid).map(ToOwned::to_owned)) self.get(nid).map(ToOwned::to_owned)
} }
} }