mirror of
https://github.com/Stremio/stremio-shell-ng.git
synced 2026-03-11 17:15:49 +00:00
Better error handling and fixed var names
This commit is contained in:
parent
24b0fbaee8
commit
90463729da
1 changed files with 33 additions and 37 deletions
|
|
@ -1,64 +1,60 @@
|
|||
use std::process::Command;
|
||||
use std::thread;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use winapi::um::handleapi::CloseHandle;
|
||||
use winapi::um::processthreadsapi::{OpenProcess, TerminateProcess};
|
||||
use winapi::um::winnt::PROCESS_TERMINATE;
|
||||
use winapi::um::handleapi::CloseHandle;
|
||||
|
||||
pub struct StremioServer {
|
||||
should_stop: Arc<std::sync::Mutex<u32>>,
|
||||
handle: Option<thread::JoinHandle<()>>,
|
||||
pid_mutex: Arc<std::sync::Mutex<u32>>,
|
||||
}
|
||||
|
||||
impl StremioServer {
|
||||
pub fn new() -> StremioServer {
|
||||
let server_pid_mutex = Arc::new(Mutex::new(0));
|
||||
let server_pid_mutex2 = server_pid_mutex.clone();
|
||||
StremioServer {
|
||||
should_stop: server_pid_mutex,
|
||||
handle: Some(thread::spawn(move || loop {
|
||||
let mut child = Command::new("node")
|
||||
.arg("server.js")
|
||||
.spawn()
|
||||
.expect("Cannot run the server");
|
||||
{
|
||||
let mut kill_request = server_pid_mutex2.lock().unwrap();
|
||||
*kill_request = child.id();
|
||||
};
|
||||
let _status = child.wait().expect("Cannot wait for the server");
|
||||
let kill_request = server_pid_mutex2.lock().unwrap();
|
||||
if *kill_request == 0 {
|
||||
thread::spawn(move || loop {
|
||||
let mut child = Command::new("node")
|
||||
.arg("server.js")
|
||||
.spawn()
|
||||
.expect("Cannot run the server");
|
||||
{
|
||||
let mut server_pid = server_pid_mutex2
|
||||
.lock()
|
||||
.expect("Trying to lock the mutex twice");
|
||||
*server_pid = child.id();
|
||||
};
|
||||
child.wait().expect("Cannot wait for the server");
|
||||
{
|
||||
let server_pid = server_pid_mutex2
|
||||
.lock()
|
||||
.expect("Trying to lock the mutex twice");
|
||||
if *server_pid == 0 {
|
||||
dbg!("Exit server guard loop...");
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
dbg!("Trying to restart the server...");
|
||||
})),
|
||||
};
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
dbg!("Trying to restart the server...");
|
||||
});
|
||||
StremioServer {
|
||||
pid_mutex: server_pid_mutex,
|
||||
}
|
||||
}
|
||||
pub fn try_kill(mut self) {
|
||||
pub fn try_kill(&self) {
|
||||
dbg!("Trying to kill the server...");
|
||||
let should_stop = self.should_stop.clone();
|
||||
let mut tremination_request = should_stop.lock().unwrap();
|
||||
let servr_pid_mutex = self.pid_mutex.clone();
|
||||
let mut server_pid = servr_pid_mutex
|
||||
.lock()
|
||||
.expect("Trying to lock the mutex twice");
|
||||
unsafe {
|
||||
let handle = OpenProcess(PROCESS_TERMINATE, 0, *tremination_request);
|
||||
let handle = OpenProcess(PROCESS_TERMINATE, 0, *server_pid);
|
||||
if !handle.is_null() {
|
||||
TerminateProcess(handle, 101);
|
||||
CloseHandle(handle);
|
||||
}
|
||||
}
|
||||
*tremination_request = 0;
|
||||
|
||||
drop(self.handle.take())
|
||||
// .map(thread::JoinHandle::join);
|
||||
*server_pid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// impl Drop for StremioServer {
|
||||
// fn drop(&mut self) {
|
||||
// dbg!("Server dropped!");
|
||||
// self.try_kill();
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
Loading…
Reference in a new issue