feat: delay put profiles and retry

This commit is contained in:
GyDi 2022-01-05 02:01:32 +08:00
parent 03f9fa4bc2
commit b3cd207444

View File

@ -1,14 +1,12 @@
extern crate log; extern crate log;
use crate::{ use crate::{
config::ProfilesConfig,
events::{ events::{
emit::{clash_start, ClashInfoPayload}, emit::{clash_start, ClashInfoPayload},
state, state,
}, },
utils::{ utils::{app_home_dir, clash, config},
app_home_dir, clash,
config::{read_clash_controller, read_profiles, read_yaml, save_yaml},
},
}; };
use reqwest::header::HeaderMap; use reqwest::header::HeaderMap;
use serde_yaml::{Mapping, Value}; use serde_yaml::{Mapping, Value};
@ -40,15 +38,7 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
match result { match result {
Ok((mut rx, cmd_child)) => { Ok((mut rx, cmd_child)) => {
log::info!("Successfully execute clash sidecar"); log::info!("Successfully execute clash sidecar");
payload.controller = Some(read_clash_controller()); payload.controller = Some(config::read_clash_controller());
// update the profile
let payload_ = payload.clone();
tauri::async_runtime::spawn(async move {
if let Err(err) = clash::put_clash_profile(&payload_).await {
log::error!("failed to put config for `{}`", err);
};
});
if let Ok(mut state) = app_handle.state::<state::ClashSidecarState>().0.lock() { if let Ok(mut state) = app_handle.state::<state::ClashSidecarState>().0.lock() {
*state = Some(cmd_child); *state = Some(cmd_child);
@ -63,6 +53,21 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
} }
} }
}); });
// update the profile
let payload_ = payload.clone();
tauri::async_runtime::spawn(async move {
let mut count = 5; // retry times
let mut err = String::from("");
while count > 0 {
match clash::put_clash_profile(&payload_).await {
Ok(_) => return,
Err(e) => err = e,
}
count -= 1;
}
log::error!("failed to put config for `{}`", err);
});
} }
Err(err) => { Err(err) => {
log::error!("Failed to execute clash sidecar for \"{}\"", err); log::error!("Failed to execute clash sidecar for \"{}\"", err);
@ -78,7 +83,7 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
/// Update the clash profile firstly /// Update the clash profile firstly
pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String> { pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String> {
let profile = { let profile = {
let profiles = read_profiles(); let profiles = ProfilesConfig::read_file();
let current = profiles.current.unwrap_or(0) as usize; let current = profiles.current.unwrap_or(0) as usize;
match profiles.items { match profiles.items {
Some(items) => { Some(items) => {
@ -111,7 +116,7 @@ pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String>
// Only the following fields are allowed: // Only the following fields are allowed:
// proxies/proxy-providers/proxy-groups/rule-providers/rules // proxies/proxy-providers/proxy-groups/rule-providers/rules
let config = read_yaml::<Mapping>(file_path.clone()); let config = config::read_yaml::<Mapping>(file_path.clone());
let mut new_config = Mapping::new(); let mut new_config = Mapping::new();
vec![ vec![
"proxies", "proxies",
@ -129,7 +134,7 @@ pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String>
} }
}); });
match save_yaml( match config::save_yaml(
temp_path.clone(), temp_path.clone(),
&new_config, &new_config,
Some("# Clash Verge Temp File"), Some("# Clash Verge Temp File"),