Run server as win32 job

This commit is contained in:
Vladimir Borisov 2021-08-02 16:10:11 +03:00
parent 0105cf1898
commit 39edc86cef
No known key found for this signature in database
GPG key ID: F9A584BE4FCB6603
4 changed files with 53 additions and 58 deletions

31
Cargo.lock generated
View file

@ -526,6 +526,7 @@ dependencies = [
"urlencoding",
"webview2",
"webview2-sys",
"win32job",
"winapi",
]
@ -607,6 +608,26 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "thiserror"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "toml"
version = "0.5.8"
@ -765,6 +786,16 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c168940144dd21fd8046987c16a46a33d5fc84eec29ef9dcddc2ac9e31526b7c"
[[package]]
name = "win32job"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b2a14136f6c8be9146ac6345774ab32cb93e7985319b4a1b42abb663bd64235"
dependencies = [
"thiserror",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"

View file

@ -22,6 +22,7 @@ bitflags = "1.2.1"
strum = "0.21"
strum_macros = "0.21"
heck = "0.3"
win32job = "1"
[build-dependencies]
embed-resource = "1.3"
[dev-dependencies]

View file

@ -41,11 +41,9 @@ fn main() {
let opt = Opt::from_args();
let streaming_server: Option<StremioServer> = if opt.development {
None
} else {
Some(StremioServer::new())
};
if !opt.development {
StremioServer::new();
}
let webui_url = if opt.development && opt.webui_url == WEB_ENDPOINT {
"http://localhost:11470".to_string()
@ -60,7 +58,4 @@ fn main() {
})
.expect("Failed to build UI");
nwg::dispatch_thread_events();
if let Some(streaming_server) = streaming_server {
streaming_server.try_kill();
}
}

View file

@ -1,60 +1,28 @@
use std::process::Command;
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 win32job::Job;
pub struct StremioServer {
pid_mutex: Arc<std::sync::Mutex<u32>>,
}
pub struct StremioServer {}
impl StremioServer {
pub fn new() -> StremioServer {
let server_pid_mutex = Arc::new(Mutex::new(0));
let server_pid_mutex2 = server_pid_mutex.clone();
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...");
});
StremioServer {
pid_mutex: server_pid_mutex,
}
}
pub fn try_kill(&self) {
dbg!("Trying to kill the server...");
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, *server_pid);
if !handle.is_null() {
TerminateProcess(handle, 101);
CloseHandle(handle);
thread::spawn(move || {
let job = Job::create().expect("Cannont create job");
let mut info = job.query_extended_limit_info().expect("Cannont get info");
info.limit_kill_on_job_close();
job.set_extended_limit_info(&mut info).ok();
job.assign_current_process().ok();
loop {
let mut child = Command::new("node")
.arg("server.js")
.spawn()
.expect("Cannot run the server");
child.wait().expect("Cannot wait for the server");
thread::sleep(Duration::from_millis(500));
dbg!("Trying to restart the server...");
}
}
*server_pid = 0;
});
StremioServer {}
}
}