Skip to content

Commit e112a9f

Browse files
rovo89ClemensElflein
authored andcommitted
Shutdown callback for service interfaces
1 parent 9b52058 commit e112a9f

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

libxbot-service-interface/include/xbot-service-interface/XbotServiceInterface.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef XBOT_FRAMEWORK_XBOTSERVICEINTERFACE_HPP
66
#define XBOT_FRAMEWORK_XBOTSERVICEINTERFACE_HPP
77

8+
#include <functional>
9+
810
#include <xbot-service-interface/ServiceDiscovery.hpp>
911
#include <xbot-service-interface/ServiceIO.hpp>
1012

@@ -15,6 +17,14 @@ struct Context {
1517
void *ctx = nullptr;
1618
};
1719

20+
using ShutdownCallback = std::function<void()>;
21+
22+
/**
23+
* Register a callback invoked after Stop() when SIGINT/SIGTERM is received.
24+
* Must be called before Start() if register_signal_handlers is true.
25+
*/
26+
void SetShutdownCallback(ShutdownCallback callback);
27+
1828
/**
1929
* Call this method to start xbot_framework.
2030
*

libxbot-service-interface/src/XbotServiceInterface.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ hub::CrowToSpeedlogHandler logger{};
2626
std::unique_ptr<PlotJugglerBridge> pjb = nullptr;
2727
std::unique_ptr<crow::SimpleApp> crow_app = nullptr;
2828

29+
ShutdownCallback shutdown_callback{};
30+
2931
void SignalHandler(int signal) {
3032
Stop();
33+
if (shutdown_callback) {
34+
shutdown_callback();
35+
}
36+
}
37+
38+
void xbot::serviceif::SetShutdownCallback(ShutdownCallback callback) {
39+
shutdown_callback = std::move(callback);
3140
}
32-
struct sigaction act;
3341

3442
xbot::serviceif::Context xbot::serviceif::Start(bool register_handlers, std::string bind_ip, bool start_rest_api) {
3543
std::unique_lock lk{mtx};
@@ -104,7 +112,9 @@ xbot::serviceif::Context xbot::serviceif::Start(bool register_handlers, std::str
104112

105113
if (register_handlers) {
106114
// Register own signal handlers
115+
struct sigaction act {};
107116
act.sa_handler = SignalHandler;
117+
sigemptyset(&act.sa_mask);
108118
sigaction(SIGINT, &act, nullptr);
109119
sigaction(SIGTERM, &act, nullptr);
110120
}

0 commit comments

Comments
 (0)