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:
Henrik Friedrichsen
2022-10-03 00:07:40 +02:00
committed by GitHub
parent 222b41c9cf
commit b1f1c20484
3 changed files with 21 additions and 0 deletions

View File

@@ -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) => {