Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef XBOT_FRAMEWORK_XBOTSERVICEINTERFACE_HPP
#define XBOT_FRAMEWORK_XBOTSERVICEINTERFACE_HPP

#include <functional>

#include <xbot-service-interface/ServiceDiscovery.hpp>
#include <xbot-service-interface/ServiceIO.hpp>

Expand All @@ -15,6 +17,14 @@ struct Context {
void *ctx = nullptr;
};

using ShutdownCallback = std::function<void()>;

/**
* Register a callback invoked after Stop() when SIGINT/SIGTERM is received.
* Must be called before Start() if register_signal_handlers is true.
*/
void SetShutdownCallback(ShutdownCallback callback);

/**
* Call this method to start xbot_framework.
*
Expand Down
12 changes: 11 additions & 1 deletion libxbot-service-interface/src/XbotServiceInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ hub::CrowToSpeedlogHandler logger{};
std::unique_ptr<PlotJugglerBridge> pjb = nullptr;
std::unique_ptr<crow::SimpleApp> crow_app = nullptr;

ShutdownCallback shutdown_callback{};

void SignalHandler(int signal) {
Stop();
if (shutdown_callback) {
shutdown_callback();
}
}

void xbot::serviceif::SetShutdownCallback(ShutdownCallback callback) {
shutdown_callback = std::move(callback);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
struct sigaction act;

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

if (register_handlers) {
// Register own signal handlers
struct sigaction act {};
act.sa_handler = SignalHandler;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, nullptr);
sigaction(SIGTERM, &act, nullptr);
}
Expand Down
Loading