Skip to content

Commit

Permalink
Gracefully exit when stopped by systemd or pkill
Browse files Browse the repository at this point in the history
  • Loading branch information
loki committed Jun 30, 2021
1 parent e8fadd2 commit ae04c4a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions sunshine/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(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);
});

Expand Down Expand Up @@ -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 };

Expand Down

0 comments on commit ae04c4a

Please sign in to comment.