Skip to content

Commit

Permalink
Merge pull request #941 from redboltz/add_signal_handler_for_docker
Browse files Browse the repository at this point in the history
Added explicit signal handlers to work well with docker.
  • Loading branch information
redboltz authored Jun 18, 2022
2 parents 1a73c47 + 4f98e26 commit 05e1ede
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
23 changes: 23 additions & 0 deletions example/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,10 +964,33 @@ int main(int argc, char **argv) {
);
}
}

as::io_context ioc_signal;
as::signal_set signals{ioc_signal, SIGINT, SIGTERM};
signals.async_wait(
[] (
boost::system::error_code const& ec,
int num
) {
if (!ec) {
std::cerr << "Signal " << num << " received. exit program" << std::endl;
exit(-1);
}
}
);
std::thread th_signal {
[&] {
ioc_signal.run();
}
};


for (auto& th : ths) th.join();
th_timer.join();
tim_progress->cancel();
th_progress_timer.join();
signals.clear();
th_signal.join();
};

std::cout << "Prepare clients" << std::endl;
Expand Down
24 changes: 24 additions & 0 deletions example/broker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,26 @@ void run_broker(boost::program_options::variables_map const& vm) {
}
}

as::io_context ioc_signal;
as::signal_set signals{ioc_signal, SIGINT, SIGTERM};
signals.async_wait(
[] (
boost::system::error_code const& ec,
int num
) {
if (!ec) {
MQTT_LOG("mqtt_broker", trace)
<< "Signal " << num << " received. exit program";
exit(-1);
}
}
);
std::thread th_signal {
[&] {
ioc_signal.run();
}
};

th_accept.join();
MQTT_LOG("mqtt_broker", trace) << "th_accept joined";

Expand All @@ -631,6 +651,10 @@ void run_broker(boost::program_options::variables_map const& vm) {
th_timer.join();
MQTT_LOG("mqtt_broker", trace) << "th_timer joined";

signals.clear();
th_signal.join();
MQTT_LOG("mqtt_broker", trace) << "th_signal joined";

} catch(std::exception &e) {
MQTT_LOG("mqtt_broker", error) << e.what();
}
Expand Down
19 changes: 17 additions & 2 deletions example/client_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,19 @@ int main(int argc, char* argv[]) {
/* 0 means stdin */
boost::asio::posix::stream_descriptor console_input(ioc, 0);

as::signal_set signals{ioc, SIGINT, SIGTERM};
signals.async_wait(
[] (
boost::system::error_code const& ec,
int num
) {
if (!ec) {
std::cerr << "Signal " << num << " received. exit program" << std::endl;
exit(-1);
}
}
);

auto setup = [&](auto& client) {
using packet_id_t = typename std::remove_reference_t<decltype(client)>::packet_id_t;

Expand Down Expand Up @@ -799,12 +812,14 @@ int main(int argc, char* argv[]) {
);

client.set_close_handler(
[]() {
[&]() {
std::cout << "< closed." << std::endl;
signals.clear();
});
client.set_error_handler(
[](boost::system::error_code const& ec) {
[&](boost::system::error_code const& ec) {
std::cout << "< error:" << ec.message() << std::endl;
signals.clear();
});
MQTT_NS::v5::properties props;
if (sei != 0) {
Expand Down

0 comments on commit 05e1ede

Please sign in to comment.