From ae04c4afbbc44e1b6e15fcb38e9afdd6473b5005 Mon Sep 17 00:00:00 2001 From: loki Date: Wed, 30 Jun 2021 17:28:58 +0200 Subject: [PATCH] Gracefully exit when stopped by systemd or pkill --- sunshine/main.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/sunshine/main.cpp b/sunshine/main.cpp index 5b178360..987098f6 100644 --- a/sunshine/main.cpp +++ b/sunshine/main.cpp @@ -187,11 +187,33 @@ int main(int argc, char *argv[]) { return fn->second(argv[0], config::sunshine.cmd.argc, config::sunshine.cmd.argv); } + task_pool.start(1); + // Create signal handler after logging has been initialized auto shutdown_event = mail::man->event(mail::shutdown); on_signal(SIGINT, [shutdown_event]() { BOOST_LOG(info) << "Interrupt handler called"sv; + task_pool.pushDelayed([]() { + BOOST_LOG(fatal) << "10 seconds passed, yet Sunshine's still running: Forcing shutdown"sv; + log_flush(); + std::abort(); + }, + 10s); + + shutdown_event->raise(true); + }); + + on_signal(SIGTERM, [shutdown_event]() { + BOOST_LOG(info) << "Terminate handler called"sv; + + task_pool.pushDelayed([]() { + BOOST_LOG(fatal) << "10 seconds passed, yet Sunshine's still running: Forcing shutdown"sv; + log_flush(); + std::abort(); + }, + 10s); + shutdown_event->raise(true); }); @@ -226,8 +248,6 @@ int main(int argc, char *argv[]) { return 0; } - task_pool.start(1); - std::thread httpThread { nvhttp::start }; std::thread configThread { confighttp::start };