Exit gracefully on SIGTERM
* Exit gracefully on `SIGTERM` Save current state and close ncspot on `SIGTERM` Fixes #948 * Disable signal handling on non-UNIX platforms
This commit is contained in:
committed by
GitHub
parent
222b41c9cf
commit
b1f1c20484
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1589,6 +1589,7 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_cbor",
|
||||
"serde_json",
|
||||
"signal-hook",
|
||||
"strum 0.24.1",
|
||||
"strum_macros 0.24.3",
|
||||
"tokio",
|
||||
|
||||
@@ -46,9 +46,13 @@ toml = "0.5"
|
||||
unicode-width = "0.1.9"
|
||||
url = "2.2"
|
||||
cursive_buffered_backend = "0.6.1"
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
wl-clipboard-rs = {version = "0.7", optional = true}
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
signal-hook = "0.3.0"
|
||||
|
||||
[dependencies.rspotify]
|
||||
default-features = false
|
||||
features = ["client-ureq", "ureq-rustls-tls"]
|
||||
|
||||
16
src/main.rs
16
src/main.rs
@@ -18,6 +18,9 @@ use librespot_core::cache::Cache;
|
||||
use librespot_playback::audio_backend;
|
||||
use log::{error, info, trace};
|
||||
|
||||
#[cfg(unix)]
|
||||
use signal_hook::{consts::SIGTERM, iterator::Signals};
|
||||
|
||||
mod authentication;
|
||||
mod command;
|
||||
mod commands;
|
||||
@@ -313,9 +316,22 @@ async fn main() -> Result<(), String> {
|
||||
|
||||
cursive.add_fullscreen_layer(layout.with_name("main"));
|
||||
|
||||
// catch SIGTERM to save current state
|
||||
#[cfg(unix)]
|
||||
let mut signals = Signals::new(&[SIGTERM]).expect("could not register SIGTERM handler");
|
||||
|
||||
// cursive event loop
|
||||
while cursive.is_running() {
|
||||
cursive.step();
|
||||
#[cfg(unix)]
|
||||
for signal in signals.pending() {
|
||||
if signal == SIGTERM {
|
||||
info!("Caught SIGTERM, cleaning up and closing");
|
||||
if let Some(data) = cursive.user_data::<UserData>().cloned() {
|
||||
data.cmd.handle(&mut cursive, Command::Quit);
|
||||
}
|
||||
}
|
||||
}
|
||||
for event in event_manager.msg_iter() {
|
||||
match event {
|
||||
Event::Player(state) => {
|
||||
|
||||
Reference in New Issue
Block a user