From 5aec5b084ac36665dfac105348a9e5d56cced9e1 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Wed, 27 Mar 2024 03:02:36 +0800 Subject: [PATCH] refactor --service/--server start time check (my stupid to restart --server) --- src/platform/macos.rs | 45 ++++---------------- src/platform/privileges_scripts/daemon.plist | 2 + 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/src/platform/macos.rs b/src/platform/macos.rs index b8fae77cb..422844323 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -486,43 +486,21 @@ pub fn lock_screen() { pub fn start_os_service() { crate::platform::macos::hide_dock(); let exe = std::env::current_exe().unwrap_or_default(); - let tm0 = hbb_common::get_modified_time(&exe); log::info!("Username: {}", crate::username()); log::info!("Startime: {:?}", get_server_start_time()); std::thread::spawn(move || loop { loop { - std::thread::sleep(std::time::Duration::from_millis(300)); - let now = hbb_common::get_modified_time(&exe); - let file_updated = now != tm0 && now != std::time::UNIX_EPOCH; + std::thread::sleep(std::time::Duration::from_secs(1)); let Some(start_time) = get_server_start_time() else { continue; }; - let dt = start_time.1 - start_time.0; - if file_updated || dt >= 0 { - // sleep a while to wait for resources file ready - std::thread::sleep(std::time::Duration::from_millis(300)); - if file_updated { - log::info!("{:?} updated, will restart", exe); - } - if dt >= 0 { - // I tried add delegate (using tao and with its main loop0, but it works in normal mode, but not work as daemon - log::info!( - "Agent start later, {:?}, will restart to make delegate work", - start_time - ); - } - for pid in start_time.2 { - unsafe { - libc::kill(pid.as_u32() as _, libc::SIGTERM); - } - } - // https://emorydunn.github.io/LaunchAgent/Classes/LaunchAgent.html#/s:11LaunchAgentAAC16throttleIntervalSiSgvp, - // by default, ThrottleInterval = 10, we changed it to 1 - if dt >= 0 { - std::thread::sleep(std::time::Duration::from_secs(dt.clamp(0, 15) as _)); - } - log::info!("The others killed"); + if start_time.0 <= start_time.1 { + // I tried add delegate (using tao and with its main loop0, but it works in normal mode, but not work as daemon + log::info!( + "Agent start later, {:?}, will restart --service to make delegate work", + start_time + ); std::process::exit(0); } } @@ -626,7 +604,7 @@ pub fn hide_dock() { } } -fn get_server_start_time() -> Option<(i64, i64, Vec)> { +fn get_server_start_time() -> Option<(i64, i64)> { use hbb_common::sysinfo::System; let mut sys = System::new(); sys.refresh_processes(); @@ -641,7 +619,6 @@ fn get_server_start_time() -> Option<(i64, i64, Vec)> else { return None; }; - let mut all = Vec::new(); for (_, p) in sys.processes() { let mut cur_path = p.exe().to_path_buf(); if let Ok(linked) = cur_path.read_link() { @@ -653,13 +630,9 @@ fn get_server_start_time() -> Option<(i64, i64, Vec)> if p.pid().as_u32() == std::process::id() { continue; } - all.push(p); - } - for p in all.iter() { let parg = if p.cmd().len() <= 1 { "" } else { &p.cmd()[1] }; - let pids = all.iter().map(|p| p.pid()).collect(); if parg == "--server" { - return Some((my_start_time as _, p.start_time() as _, pids)); + return Some((my_start_time as _, p.start_time() as _)); } } None diff --git a/src/platform/privileges_scripts/daemon.plist b/src/platform/privileges_scripts/daemon.plist index 82e082347..004957e36 100644 --- a/src/platform/privileges_scripts/daemon.plist +++ b/src/platform/privileges_scripts/daemon.plist @@ -6,6 +6,8 @@ com.carriez.RustDesk_service KeepAlive + ThrottleInterval + 1 ProgramArguments /bin/sh