refactor --service/--server start time check (my stupid to restart

--server)
This commit is contained in:
rustdesk 2024-03-27 03:02:36 +08:00
parent b77add97db
commit 5aec5b084a
2 changed files with 11 additions and 36 deletions

View File

@ -486,43 +486,21 @@ pub fn lock_screen() {
pub fn start_os_service() { pub fn start_os_service() {
crate::platform::macos::hide_dock(); crate::platform::macos::hide_dock();
let exe = std::env::current_exe().unwrap_or_default(); let exe = std::env::current_exe().unwrap_or_default();
let tm0 = hbb_common::get_modified_time(&exe);
log::info!("Username: {}", crate::username()); log::info!("Username: {}", crate::username());
log::info!("Startime: {:?}", get_server_start_time()); log::info!("Startime: {:?}", get_server_start_time());
std::thread::spawn(move || loop { std::thread::spawn(move || loop {
loop { loop {
std::thread::sleep(std::time::Duration::from_millis(300)); std::thread::sleep(std::time::Duration::from_secs(1));
let now = hbb_common::get_modified_time(&exe);
let file_updated = now != tm0 && now != std::time::UNIX_EPOCH;
let Some(start_time) = get_server_start_time() else { let Some(start_time) = get_server_start_time() else {
continue; continue;
}; };
let dt = start_time.1 - start_time.0; if start_time.0 <= start_time.1 {
if file_updated || dt >= 0 { // I tried add delegate (using tao and with its main loop0, but it works in normal mode, but not work as daemon
// sleep a while to wait for resources file ready log::info!(
std::thread::sleep(std::time::Duration::from_millis(300)); "Agent start later, {:?}, will restart --service to make delegate work",
if file_updated { start_time
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");
std::process::exit(0); std::process::exit(0);
} }
} }
@ -626,7 +604,7 @@ pub fn hide_dock() {
} }
} }
fn get_server_start_time() -> Option<(i64, i64, Vec<hbb_common::sysinfo::Pid>)> { fn get_server_start_time() -> Option<(i64, i64)> {
use hbb_common::sysinfo::System; use hbb_common::sysinfo::System;
let mut sys = System::new(); let mut sys = System::new();
sys.refresh_processes(); sys.refresh_processes();
@ -641,7 +619,6 @@ fn get_server_start_time() -> Option<(i64, i64, Vec<hbb_common::sysinfo::Pid>)>
else { else {
return None; return None;
}; };
let mut all = Vec::new();
for (_, p) in sys.processes() { for (_, p) in sys.processes() {
let mut cur_path = p.exe().to_path_buf(); let mut cur_path = p.exe().to_path_buf();
if let Ok(linked) = cur_path.read_link() { if let Ok(linked) = cur_path.read_link() {
@ -653,13 +630,9 @@ fn get_server_start_time() -> Option<(i64, i64, Vec<hbb_common::sysinfo::Pid>)>
if p.pid().as_u32() == std::process::id() { if p.pid().as_u32() == std::process::id() {
continue; continue;
} }
all.push(p);
}
for p in all.iter() {
let parg = if p.cmd().len() <= 1 { "" } else { &p.cmd()[1] }; let parg = if p.cmd().len() <= 1 { "" } else { &p.cmd()[1] };
let pids = all.iter().map(|p| p.pid()).collect();
if parg == "--server" { 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 None

View File

@ -6,6 +6,8 @@
<string>com.carriez.RustDesk_service</string> <string>com.carriez.RustDesk_service</string>
<key>KeepAlive</key> <key>KeepAlive</key>
<true/> <true/>
<key>ThrottleInterval</key>
<integer>1</integer>
<key>ProgramArguments</key> <key>ProgramArguments</key>
<array> <array>
<string>/bin/sh</string> <string>/bin/sh</string>