mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
Fix windows signals support (#325)
This commit is contained in:
parent
0e21f46210
commit
18d3516343
3 changed files with 48 additions and 15 deletions
|
@ -1,5 +1,9 @@
|
|||
# Changes
|
||||
|
||||
## [1.0.3] - 2024-03-29
|
||||
|
||||
* Fix windows signals support
|
||||
|
||||
## [1.0.2] - 2024-03-25
|
||||
|
||||
* Add basic signals support
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ntex-server"
|
||||
version = "1.0.2"
|
||||
version = "1.0.3"
|
||||
authors = ["ntex contributors <team@ntex.rs>"]
|
||||
description = "Server for ntex framework"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
|
|
@ -85,20 +85,49 @@ pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
|
|||
/// Signals are handled by oneshots, you have to re-register
|
||||
/// after each signal.
|
||||
pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
|
||||
let _ = thread::Builder::new()
|
||||
.name("ntex-server signals".to_string())
|
||||
.spawn(move || {
|
||||
ctrlc::set_handler(move || {
|
||||
srv.signal(Signal::Int);
|
||||
use std::sync::Mutex;
|
||||
|
||||
System::current().arbiter().exec_fn(|| {
|
||||
HANDLERS.with(|handlers| {
|
||||
for tx in handlers.borrow_mut().drain(..) {
|
||||
let _ = tx.send(Signal::Int);
|
||||
use ntex_rt::spawn;
|
||||
|
||||
static CUR_SYS: Mutex<RefCell<Option<System>>> = Mutex::new(RefCell::new(None));
|
||||
|
||||
let guard = match CUR_SYS.lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(_) => {
|
||||
log::error!("Cannot lock mutex");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut sys = guard.borrow_mut();
|
||||
let started = sys.is_some();
|
||||
*sys = Some(System::current());
|
||||
|
||||
let rx = signal();
|
||||
let _ = spawn(async move {
|
||||
if let Ok(sig) = rx.await {
|
||||
srv.signal(sig);
|
||||
}
|
||||
});
|
||||
|
||||
if !started {
|
||||
let _ = thread::Builder::new()
|
||||
.name("ntex-server signals".to_string())
|
||||
.spawn(move || {
|
||||
ctrlc::set_handler(move || {
|
||||
if let Ok(guard) = CUR_SYS.lock() {
|
||||
if let Some(sys) = &*guard.borrow() {
|
||||
sys.arbiter().exec_fn(|| {
|
||||
HANDLERS.with(|handlers| {
|
||||
for tx in handlers.borrow_mut().drain(..) {
|
||||
let _ = tx.send(Signal::Int);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
})
|
||||
.expect("Error setting Ctrl-C handler");
|
||||
});
|
||||
}
|
||||
})
|
||||
.expect("Error setting Ctrl-C handler");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue