FairMQ: Make sure --catch-signals is always handled

This commit is contained in:
Dennis Klein 2017-09-20 18:10:56 +02:00 committed by Mohammad Al-Turany
parent fd08167a4c
commit 88b3b8ef18
2 changed files with 33 additions and 32 deletions

View File

@ -17,6 +17,7 @@ using namespace std;
namespace namespace
{ {
// ugly global state, but std::signal gives us no other choice
std::function<void(int)> gSignalHandlerClosure; std::function<void(int)> gSignalHandlerClosure;
extern "C" auto signal_handler(int signal) -> void extern "C" auto signal_handler(int signal) -> void
@ -62,24 +63,24 @@ Control::Control(const string name, const Plugin::Version version, const string
LOG(ERROR) << "Unrecognized control mode '" << control << "' requested. " << "Ignoring and falling back to static control mode."; LOG(ERROR) << "Unrecognized control mode '" << control << "' requested. " << "Ignoring and falling back to static control mode.";
fControllerThread = thread(&Control::StaticMode, this); fControllerThread = thread(&Control::StaticMode, this);
} }
LOG(DEBUG) << "catch-signals: " << GetProperty<int>("catch-signals");
if (GetProperty<int>("catch-signals") > 0)
{
gSignalHandlerClosure = bind(&Control::SignalHandler, this, placeholders::_1);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
}
else
{
LOG(WARN) << "Signal handling (e.g. Ctrl-C) has been deactivated.";
}
} }
catch (PluginServices::DeviceControlError& e) catch (PluginServices::DeviceControlError& e)
{ {
// If we are here, it means another plugin has taken control. That's fine, just print the exception message and do nothing else. // If we are here, it means another plugin has taken control. That's fine, just print the exception message and do nothing else.
LOG(DEBUG) << e.what(); LOG(DEBUG) << e.what();
} }
LOG(DEBUG) << "catch-signals: " << GetProperty<int>("catch-signals");
if (GetProperty<int>("catch-signals") > 0)
{
gSignalHandlerClosure = bind(&Control::SignalHandler, this, placeholders::_1);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
}
else
{
LOG(WARN) << "Signal handling (e.g. Ctrl-C) has been deactivated.";
}
} }
auto ControlPluginProgramOptions() -> Plugin::ProgOptions auto ControlPluginProgramOptions() -> Plugin::ProgOptions
@ -96,15 +97,6 @@ auto Control::InteractiveMode() -> void
{ {
try try
{ {
SubscribeToDeviceStateChange([&](DeviceState newState)
{
{
lock_guard<mutex> lock{fEventsMutex};
fEvents.push(newState);
}
fNewEvent.notify_one();
});
RunStartupSequence(); RunStartupSequence();
char input; // hold the user console input char input; // hold the user console input
@ -226,15 +218,6 @@ auto Control::StaticMode() -> void
{ {
try try
{ {
SubscribeToDeviceStateChange([&](DeviceState newState)
{
{
lock_guard<mutex> lock{fEventsMutex};
fEvents.push(newState);
}
fNewEvent.notify_one();
});
RunStartupSequence(); RunStartupSequence();
{ {
@ -261,7 +244,6 @@ auto Control::StaticMode() -> void
auto Control::SignalHandler(int signal) -> void auto Control::SignalHandler(int signal) -> void
{ {
if (!fDeviceTerminationRequested) if (!fDeviceTerminationRequested)
{ {
fDeviceTerminationRequested = true; fDeviceTerminationRequested = true;
@ -271,6 +253,16 @@ auto Control::SignalHandler(int signal) -> void
LOG(INFO) << "Received device shutdown request (signal " << signal << ")."; LOG(INFO) << "Received device shutdown request (signal " << signal << ").";
LOG(INFO) << "Waiting for graceful device shutdown. Hit Ctrl-C again to abort immediately."; LOG(INFO) << "Waiting for graceful device shutdown. Hit Ctrl-C again to abort immediately.";
UnsubscribeFromDeviceStateChange(); // In case, static or interactive mode have subscribed already
SubscribeToDeviceStateChange([&](DeviceState newState)
{
{
lock_guard<mutex> lock{fEventsMutex};
fEvents.push(newState);
}
fNewEvent.notify_one();
});
fSignalHandlerThread = thread(&Control::RunShutdownSequence, this); fSignalHandlerThread = thread(&Control::RunShutdownSequence, this);
} }
else else
@ -317,6 +309,15 @@ auto Control::RunShutdownSequence() -> void
auto Control::RunStartupSequence() -> void auto Control::RunStartupSequence() -> void
{ {
SubscribeToDeviceStateChange([&](DeviceState newState)
{
{
lock_guard<mutex> lock{fEventsMutex};
fEvents.push(newState);
}
fNewEvent.notify_one();
});
ChangeDeviceState(DeviceStateTransition::InitDevice); ChangeDeviceState(DeviceStateTransition::InitDevice);
while (WaitForNextState() != DeviceState::DeviceReady) {} while (WaitForNextState() != DeviceState::DeviceReady) {}
ChangeDeviceState(DeviceStateTransition::InitTask); ChangeDeviceState(DeviceStateTransition::InitTask);

View File

@ -55,7 +55,7 @@ auto ControlPluginProgramOptions() -> Plugin::ProgOptions;
REGISTER_FAIRMQ_PLUGIN( REGISTER_FAIRMQ_PLUGIN(
Control, // Class name Control, // Class name
control, // Plugin name (string, lower case chars only) control, // Plugin name (string, lower case chars only)
(Plugin::Version{1,0,0}), // Version (Plugin::Version{1,0,1}), // Version
"FairRootGroup <fairroot@gsi.de>", // Maintainer "FairRootGroup <fairroot@gsi.de>", // Maintainer
"https://github.com/FairRootGroup/FairRoot", // Homepage "https://github.com/FairRootGroup/FairRoot", // Homepage
ControlPluginProgramOptions // Free function which declares custom program options for the plugin ControlPluginProgramOptions // Free function which declares custom program options for the plugin