Skip to content
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
5 changes: 3 additions & 2 deletions crates/icp-cli/src/commands/network/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ pub(crate) async fn exec(ctx: &Context, args: &RunArgs) -> Result<(), CommandErr

if args.background {
let mut child = run_in_background()?;
nd.save_background_network_runner_pid(Pid::from(child.id() as usize))?;
nd.save_background_network_runner_pid(Pid::from(child.id() as usize))
.await?;
relay_child_output_until_healthy(ctx, &mut child, &nd).await?;
} else {
run_network(
Expand Down Expand Up @@ -230,7 +231,7 @@ async fn wait_for_healthy_network(nd: &NetworkDirectory) -> Result<(), CommandEr

// Wait for network descriptor to be written
let network = retry_with_timeout(
|| async move { nd.load_network_descriptor().unwrap_or(None) },
|| async move { nd.load_network_descriptor().await.unwrap_or(None) },
max_retries,
delay_ms,
)
Expand Down
20 changes: 16 additions & 4 deletions crates/icp-cli/src/commands/network/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::time::Duration;

use clap::Parser;
use icp::{
fs::remove_file, manifest, network::NetworkDirectory, project::DEFAULT_LOCAL_NETWORK_NAME,
fs::{lock::LockError, remove_file},
manifest,
network::NetworkDirectory,
project::DEFAULT_LOCAL_NETWORK_NAME,
};
use sysinfo::{Pid, ProcessesToUpdate, Signal, System};

Expand Down Expand Up @@ -37,6 +40,9 @@ pub enum CommandError {

#[error("process {pid} did not exit within {timeout} seconds")]
Timeout { pid: Pid, timeout: u64 },

#[error(transparent)]
LockFile(#[from] LockError),
}

pub async fn exec(ctx: &Context, cmd: &Cmd) -> Result<(), CommandError> {
Expand All @@ -61,7 +67,8 @@ pub async fn exec(ctx: &Context, cmd: &Cmd) -> Result<(), CommandError> {

// Load PID from file
let pid = nd
.load_background_network_runner_pid()?
.load_background_network_runner_pid()
.await?
.ok_or(CommandError::NotRunning {
name: cmd.name.clone(),
})?;
Expand All @@ -73,8 +80,13 @@ pub async fn exec(ctx: &Context, cmd: &Cmd) -> Result<(), CommandError> {
send_sigint(pid);
wait_for_process_exit(pid)?;

let pid_file = nd.structure.background_network_runner_pid_file();
let _ = remove_file(&pid_file); // Cleanup is nice, but optional
nd.root()?
.with_write(async |root| {
let pid_file = root.background_network_runner_pid_file();
let _ = remove_file(&pid_file); // Cleanup is nice, but optional
Ok::<_, CommandError>(())
})
.await??;

let _ = ctx.term.write_line("Network stopped successfully");

Expand Down
7 changes: 4 additions & 3 deletions crates/icp-cli/tests/network_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ fn network_same_port() {
.args(["network", "run", "my-network"])
.assert()
.failure()
.stderr(contains(
"Error: port 8080 is in use by the my-network network of the project at",
));
.stderr(contains(format!(
"Error: port 8080 is in use by the my-network network of the project at '{}'",
project_dir_a.canonicalize().unwrap().display()
)));
}

#[test]
Expand Down
31 changes: 19 additions & 12 deletions crates/icp/src/network/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use url::Url;

use crate::{
Network,
fs::json,
network::{Configuration, NetworkDirectory, access::GetNetworkAccessError::DecodeRootKey},
network::{
Configuration, NetworkDirectory, access::GetNetworkAccessError::DecodeRootKey,
directory::LoadNetworkFileError,
},
prelude::*,
};

Expand Down Expand Up @@ -42,11 +44,11 @@ pub enum GetNetworkAccessError {
#[snafu(display("failed to decode root key"))]
DecodeRootKey { source: hex::FromHexError },

#[snafu(transparent)]
LoadJsonWithLock { source: json::Error },

#[snafu(display("failed to load port {port} descriptor"))]
LoadPortDescriptor { port: u16, source: json::Error },
LoadPortDescriptor {
port: u16,
source: LoadNetworkFileError,
},

#[snafu(display("the {network} network for this project is not running"))]
NetworkNotRunning { network: String },
Expand All @@ -63,14 +65,16 @@ pub enum GetNetworkAccessError {
#[snafu(display("no descriptor found for port {port}"))]
NoPortDescriptor { port: u16 },

#[snafu(display("failed to load network descriptor"))]
LoadNetworkDescriptor { source: LoadNetworkFileError },
#[snafu(display("failed to parse URL {url}"))]
ParseUrl {
url: String,
source: url::ParseError,
},
}

pub fn get_network_access(
pub async fn get_network_access(
nd: NetworkDirectory,
network: &Network,
) -> Result<NetworkAccess, GetNetworkAccessError> {
Expand All @@ -79,11 +83,13 @@ pub fn get_network_access(
// Managed
Configuration::Managed { managed: _ } => {
// Load network descriptor
let desc =
nd.load_network_descriptor()?
.ok_or(GetNetworkAccessError::NetworkNotRunning {
network: nd.network_name.to_owned(),
})?;
let desc = nd
.load_network_descriptor()
.await
.context(LoadNetworkDescriptorSnafu)?
.ok_or(GetNetworkAccessError::NetworkNotRunning {
network: nd.network_name.to_owned(),
})?;

// Specify port
let port = desc.gateway.port;
Expand All @@ -92,6 +98,7 @@ pub fn get_network_access(
if desc.gateway.fixed {
let pdesc = nd
.load_port_descriptor(port)
.await
.context(LoadPortDescriptorSnafu { port })?
.context(NoPortDescriptorSnafu { port })?;

Expand Down
Loading