Execute the server from the same directory the main executable is

This commit is contained in:
Vladimir Borisov 2024-04-22 12:53:53 +03:00
parent f76435e745
commit b8c68029ed

View file

@ -1,72 +1,77 @@
use native_windows_gui as nwg; use native_windows_gui as nwg;
use std::os::windows::process::CommandExt; use std::{
use std::process::Command; env, fs, os::windows::process::CommandExt, path, process::Command, thread, time::Duration,
use std::thread; };
use std::time::Duration; use winapi::um::{
use winapi::um::{ processthreadsapi::GetCurrentProcess,
processthreadsapi::GetCurrentProcess, winbase::CreateJobObjectA,
winbase::CreateJobObjectA, winnt::{
winnt::{ JobObjectExtendedLimitInformation, JOBOBJECT_BASIC_LIMIT_INFORMATION,
JobObjectExtendedLimitInformation, JOBOBJECT_BASIC_LIMIT_INFORMATION, JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JOB_OBJECT_LIMIT_BREAKAWAY_OK,
JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JOB_OBJECT_LIMIT_BREAKAWAY_OK, JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, },
}, };
};
const CREATE_NO_WINDOW: u32 = 0x08000000;
const CREATE_NO_WINDOW: u32 = 0x08000000;
pub struct StremioServer {}
pub struct StremioServer {}
impl StremioServer {
impl StremioServer { pub fn new() -> StremioServer {
pub fn new() -> StremioServer { thread::spawn(move || {
thread::spawn(move || { // Use Win32JobObject to kill the child process when the parent process is killed
// Use Win32JobObject to kill the child process when the parent process is killed // With the JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK and JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flags
// With the JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK and JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flags unsafe {
unsafe { let job_main_process = CreateJobObjectA(std::ptr::null_mut(), std::ptr::null_mut());
let job_main_process = CreateJobObjectA(std::ptr::null_mut(), std::ptr::null_mut()); let jeli = JOBOBJECT_EXTENDED_LIMIT_INFORMATION {
let jeli = JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation: JOBOBJECT_BASIC_LIMIT_INFORMATION {
BasicLimitInformation: JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags: JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
LimitFlags: JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION
| JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | JOB_OBJECT_LIMIT_BREAKAWAY_OK,
| JOB_OBJECT_LIMIT_BREAKAWAY_OK, ..std::mem::zeroed()
..std::mem::zeroed() },
}, ..std::mem::zeroed()
..std::mem::zeroed() };
}; winapi::um::jobapi2::SetInformationJobObject(
winapi::um::jobapi2::SetInformationJobObject( job_main_process,
job_main_process, JobObjectExtendedLimitInformation,
JobObjectExtendedLimitInformation, &jeli as *const _ as *mut _,
&jeli as *const _ as *mut _, std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32, );
); winapi::um::jobapi2::AssignProcessToJobObject(
winapi::um::jobapi2::AssignProcessToJobObject( job_main_process,
job_main_process, GetCurrentProcess(),
GetCurrentProcess(), );
); }
} let mut path = env::current_exe()
loop { .and_then(fs::canonicalize)
let child = Command::new("./stremio-runtime") .expect("Cannot get the current executable path");
.arg("server.js") path.pop();
.creation_flags(CREATE_NO_WINDOW) loop {
.spawn(); let runtime_path = path.clone().join(path::Path::new("stremio-runtime"));
match child { let server_path = path.clone().join(path::Path::new("server.js"));
Ok(mut child) => { let child = Command::new(runtime_path)
// TODO: store somehow last few lines of the child's stdout/stderr instead of just waiting .arg(server_path)
child.wait().expect("Cannot wait for the server"); .creation_flags(CREATE_NO_WINDOW)
} .spawn();
Err(err) => { match child {
nwg::error_message( Ok(mut child) => {
"Stremio server", // TODO: store somehow last few lines of the child's stdout/stderr instead of just waiting
format!("Cannot execute stremio-runtime: {}", &err).as_str(), child.wait().expect("Cannot wait for the server");
); }
break; Err(err) => {
} nwg::error_message(
}; "Stremio server",
// TODO: show error message with the child's stdout/stderr format!("Cannot execute stremio-runtime: {}", &err).as_str(),
thread::sleep(Duration::from_millis(500)); );
dbg!("Trying to restart the server..."); break;
} }
}); };
StremioServer {} // TODO: show error message with the child's stdout/stderr
} thread::sleep(Duration::from_millis(500));
} dbg!("Trying to restart the server...");
}
});
StremioServer {}
}
}