mirror of
https://github.com/EasyTier/EasyTier.git
synced 2025-10-05 08:47:01 +08:00
fix exit code when error occcurs (#1173)
This commit is contained in:
@@ -1111,6 +1111,14 @@ async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
|||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = manager.wait() => {
|
_ = manager.wait() => {
|
||||||
|
let infos = manager.collect_network_infos()?;
|
||||||
|
let errs = infos
|
||||||
|
.into_values()
|
||||||
|
.filter_map(|info| info.error_msg)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
if errs.len() > 0 {
|
||||||
|
return Err(anyhow::anyhow!("some instances stopped with errors"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ = tokio::signal::ctrl_c() => {
|
_ = tokio::signal::ctrl_c() => {
|
||||||
println!("ctrl-c received, exiting...");
|
println!("ctrl-c received, exiting...");
|
||||||
|
@@ -48,7 +48,6 @@ impl NetworkInstanceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let instance_stop_notifier = instance.get_stop_notifier();
|
let instance_stop_notifier = instance.get_stop_notifier();
|
||||||
let instance_config_source = instance.get_config_source();
|
|
||||||
let instance_event_receiver = match instance.get_config_source() {
|
let instance_event_receiver = match instance.get_config_source() {
|
||||||
ConfigSource::Cli | ConfigSource::File | ConfigSource::Web => {
|
ConfigSource::Cli | ConfigSource::File | ConfigSource::Web => {
|
||||||
Some(instance.subscribe_event())
|
Some(instance.subscribe_event())
|
||||||
@@ -78,14 +77,8 @@ impl NetworkInstanceManager {
|
|||||||
eprintln!("instance {} stopped with error: {}", instance_id, e);
|
eprintln!("instance {} stopped with error: {}", instance_id, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match instance_config_source {
|
stop_check_notifier.notify_one();
|
||||||
ConfigSource::Cli | ConfigSource::File => {
|
|
||||||
instance_map.remove(&instance_id);
|
|
||||||
}
|
|
||||||
ConfigSource::Web | ConfigSource::GUI | ConfigSource::FFI => {}
|
|
||||||
}
|
|
||||||
instance_stop_tasks.remove(&instance_id);
|
instance_stop_tasks.remove(&instance_id);
|
||||||
stop_check_notifier.notify_waiters();
|
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -160,7 +153,11 @@ impl NetworkInstanceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn wait(&self) {
|
pub async fn wait(&self) {
|
||||||
while self.instance_map.len() > 0 {
|
while self
|
||||||
|
.instance_map
|
||||||
|
.iter()
|
||||||
|
.any(|item| item.value().is_easytier_running())
|
||||||
|
{
|
||||||
self.stop_check_notifier.notified().await;
|
self.stop_check_notifier.notified().await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -338,6 +335,7 @@ mod tests {
|
|||||||
use crate::common::config::*;
|
use crate::common::config::*;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
#[serial_test::serial]
|
||||||
async fn it_works() {
|
async fn it_works() {
|
||||||
let manager = NetworkInstanceManager::new();
|
let manager = NetworkInstanceManager::new();
|
||||||
let cfg_str = r#"
|
let cfg_str = r#"
|
||||||
@@ -404,6 +402,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[serial_test::serial]
|
||||||
fn test_no_tokio_runtime() {
|
fn test_no_tokio_runtime() {
|
||||||
let manager = NetworkInstanceManager::new();
|
let manager = NetworkInstanceManager::new();
|
||||||
let cfg_str = r#"
|
let cfg_str = r#"
|
||||||
@@ -466,6 +465,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
#[serial_test::serial]
|
||||||
async fn test_single_instance_failed() {
|
async fn test_single_instance_failed() {
|
||||||
let free_tcp_port =
|
let free_tcp_port =
|
||||||
crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
|
crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
|
||||||
@@ -491,7 +491,7 @@ mod tests {
|
|||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = manager.wait() => {
|
_ = manager.wait() => {
|
||||||
assert_eq!(manager.list_network_instance_ids().len(), 0);
|
assert_eq!(manager.list_network_instance_ids().len(), 1);
|
||||||
}
|
}
|
||||||
_ = tokio::time::sleep(std::time::Duration::from_secs(5)) => {
|
_ = tokio::time::sleep(std::time::Duration::from_secs(5)) => {
|
||||||
panic!("instance manager with single failed instance({:?}) should not running", config_source);
|
panic!("instance manager with single failed instance({:?}) should not running", config_source);
|
||||||
@@ -522,6 +522,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
#[serial_test::serial]
|
||||||
async fn test_multiple_instances_one_failed() {
|
async fn test_multiple_instances_one_failed() {
|
||||||
let free_tcp_port =
|
let free_tcp_port =
|
||||||
crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
|
crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
|
||||||
@@ -557,7 +558,7 @@ mod tests {
|
|||||||
panic!("instance manager with multiple instances one failed should still running");
|
panic!("instance manager with multiple instances one failed should still running");
|
||||||
}
|
}
|
||||||
_ = tokio::time::sleep(std::time::Duration::from_secs(2)) => {
|
_ = tokio::time::sleep(std::time::Duration::from_secs(2)) => {
|
||||||
assert_eq!(manager.list_network_instance_ids().len(), 1);
|
assert_eq!(manager.list_network_instance_ids().len(), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user