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",
|
||||||
"serde_cbor",
|
"serde_cbor",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"signal-hook",
|
||||||
"strum 0.24.1",
|
"strum 0.24.1",
|
||||||
"strum_macros 0.24.3",
|
"strum_macros 0.24.3",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|||||||
@@ -46,9 +46,13 @@ toml = "0.5"
|
|||||||
unicode-width = "0.1.9"
|
unicode-width = "0.1.9"
|
||||||
url = "2.2"
|
url = "2.2"
|
||||||
cursive_buffered_backend = "0.6.1"
|
cursive_buffered_backend = "0.6.1"
|
||||||
|
|
||||||
[target.'cfg(target_os = "linux")'.dependencies]
|
[target.'cfg(target_os = "linux")'.dependencies]
|
||||||
wl-clipboard-rs = {version = "0.7", optional = true}
|
wl-clipboard-rs = {version = "0.7", optional = true}
|
||||||
|
|
||||||
|
[target.'cfg(unix)'.dependencies]
|
||||||
|
signal-hook = "0.3.0"
|
||||||
|
|
||||||
[dependencies.rspotify]
|
[dependencies.rspotify]
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["client-ureq", "ureq-rustls-tls"]
|
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 librespot_playback::audio_backend;
|
||||||
use log::{error, info, trace};
|
use log::{error, info, trace};
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
use signal_hook::{consts::SIGTERM, iterator::Signals};
|
||||||
|
|
||||||
mod authentication;
|
mod authentication;
|
||||||
mod command;
|
mod command;
|
||||||
mod commands;
|
mod commands;
|
||||||
@@ -313,9 +316,22 @@ async fn main() -> Result<(), String> {
|
|||||||
|
|
||||||
cursive.add_fullscreen_layer(layout.with_name("main"));
|
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
|
// cursive event loop
|
||||||
while cursive.is_running() {
|
while cursive.is_running() {
|
||||||
cursive.step();
|
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() {
|
for event in event_manager.msg_iter() {
|
||||||
match event {
|
match event {
|
||||||
Event::Player(state) => {
|
Event::Player(state) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user