Advanced servers support clean shutdown via SIGINT or SIGTERM

fix #1026
This commit is contained in:
Vinnie Falco
2018-02-21 12:41:54 -08:00
parent a4714746dc
commit 10ce0283c2
6 changed files with 66 additions and 23 deletions
@@ -23,6 +23,7 @@
#include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/steady_timer.hpp>
@@ -1272,6 +1273,17 @@ int main(int argc, char* argv[])
tcp::endpoint{address, port},
doc_root)->run();
// Capture SIGINT and SIGTERM to perform a clean shutdown
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
signals.async_wait(
[&](boost::system::error_code const&, int)
{
// Stop the `io_context`. This will cause `run()`
// to return immediately, eventually destroying the
// `io_context` and all of the sockets in it.
ioc.stop();
});
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
@@ -1283,5 +1295,11 @@ int main(int argc, char* argv[])
});
ioc.run();
// (If we get here, it means we got a SIGINT or SIGTERM)
// Block until all the threads exit
for(auto& t : v)
t.join();
return EXIT_SUCCESS;
}