refactor: replace ConfigSource with bool parameter (#1516)

This commit is contained in:
Mg Pig
2025-11-04 13:48:10 +08:00
committed by GitHub
parent 6bb2fd9a15
commit 89cc75f674
8 changed files with 51 additions and 102 deletions

View File

@@ -8,7 +8,7 @@ use crate::{
global_ctx::{EventBusSubscriber, GlobalCtxEvent},
scoped_task::ScopedTask,
},
launcher::{ConfigSource, NetworkInstance, NetworkInstanceRunningInfo},
launcher::{NetworkInstance, NetworkInstanceRunningInfo},
proto::{self},
rpc_service::InstanceRpcService,
};
@@ -37,32 +37,18 @@ impl NetworkInstanceManager {
}
fn start_instance_task(&self, instance_id: uuid::Uuid) -> Result<(), anyhow::Error> {
if tokio::runtime::Handle::try_current().is_err() {
return Err(anyhow::anyhow!(
"tokio runtime not found, cannot start instance task"
));
}
let instance = self
.instance_map
.get(&instance_id)
.ok_or_else(|| anyhow::anyhow!("instance {} not found", instance_id))?;
match instance.get_config_source() {
ConfigSource::FFI | ConfigSource::GUI => {
// FFI and GUI have no tokio runtime, so we don't need to spawn a task
return Ok(());
}
_ => {
if tokio::runtime::Handle::try_current().is_err() {
return Err(anyhow::anyhow!(
"tokio runtime not found, cannot start instance task"
));
}
}
}
let instance_stop_notifier = instance.get_stop_notifier();
let instance_event_receiver = match instance.get_config_source() {
ConfigSource::Cli | ConfigSource::File | ConfigSource::Web => {
Some(instance.subscribe_event())
}
ConfigSource::GUI | ConfigSource::FFI => None,
};
let instance_event_receiver = instance.subscribe_event();
let instance_map = self.instance_map.clone();
let instance_stop_tasks = self.instance_stop_tasks.clone();
@@ -76,7 +62,6 @@ impl NetworkInstanceManager {
return;
};
let _t = instance_event_receiver
.flatten()
.map(|event| ScopedTask::from(handle_event(instance_id, event)));
instance_stop_notifier.notified().await;
if let Some(instance) = instance_map.get(&instance_id) {
@@ -97,18 +82,20 @@ impl NetworkInstanceManager {
pub fn run_network_instance(
&self,
cfg: TomlConfigLoader,
source: ConfigSource,
watch_event: bool,
) -> Result<uuid::Uuid, anyhow::Error> {
let instance_id = cfg.get_id();
if self.instance_map.contains_key(&instance_id) {
anyhow::bail!("instance {} already exists", instance_id);
}
let mut instance = NetworkInstance::new(cfg, source);
let mut instance = NetworkInstance::new(cfg);
instance.start()?;
self.instance_map.insert(instance_id, instance);
self.start_instance_task(instance_id)?;
if watch_event {
self.start_instance_task(instance_id)?;
}
Ok(instance_id)
}
@@ -429,32 +416,20 @@ mod tests {
c.set_listeners(vec![format!("tcp://0.0.0.0:{}", port).parse().unwrap()]);
})
.unwrap(),
ConfigSource::Cli,
true,
)
.unwrap();
let instance_id2 = manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::File,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true)
.unwrap();
let instance_id3 = manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::GUI,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), false)
.unwrap();
let instance_id4 = manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::Web,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true)
.unwrap();
let instance_id5 = manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::FFI,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), false)
.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(1)).await; // to make instance actually started
@@ -489,16 +464,10 @@ mod tests {
let port = crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
assert!(manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::Cli,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true,)
.is_err());
assert!(manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::File,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true,)
.is_err());
assert!(manager
.run_network_instance(
@@ -507,20 +476,14 @@ mod tests {
c.set_listeners(vec![format!("tcp://0.0.0.0:{}", port).parse().unwrap()]);
})
.unwrap(),
ConfigSource::GUI,
false,
)
.is_ok());
assert!(manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::Web,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true,)
.is_err());
assert!(manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
ConfigSource::FFI,
)
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), false,)
.is_ok());
std::thread::sleep(std::time::Duration::from_secs(1)); // wait instance actually started
@@ -546,7 +509,8 @@ mod tests {
let free_tcp_port =
crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
for config_source in [ConfigSource::Cli, ConfigSource::File] {
// Test with event watching enabled (for CLI/File/RPC usage) - instance should auto-stop on error
for watch_event in [true] {
let _port_holder =
std::net::TcpListener::bind(format!("0.0.0.0:{}", free_tcp_port)).unwrap();
@@ -561,7 +525,7 @@ mod tests {
manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
config_source.clone(),
watch_event,
)
.unwrap();
@@ -570,11 +534,14 @@ mod tests {
assert_eq!(manager.list_network_instance_ids().len(), 1);
}
_ = 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", watch_event);
}
}
}
for config_source in [ConfigSource::Web, ConfigSource::GUI, ConfigSource::FFI] {
// Test without event watching (for FFI usage) - instance should remain even if failed
{
let watch_event = false;
let _port_holder =
std::net::TcpListener::bind(format!("0.0.0.0:{}", free_tcp_port)).unwrap();
@@ -589,7 +556,7 @@ mod tests {
manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
config_source.clone(),
watch_event,
)
.unwrap();
@@ -616,7 +583,7 @@ mod tests {
manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
ConfigSource::Cli,
true,
)
.unwrap();
@@ -625,7 +592,7 @@ mod tests {
manager
.run_network_instance(
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
ConfigSource::Cli,
true,
)
.unwrap();