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() {
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<hbb_common::sysinfo::Pid>)> {
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<hbb_common::sysinfo::Pid>)>
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<hbb_common::sysinfo::Pid>)>
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

View File

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