diff --git a/services/histserv/app/Application.cxx b/services/histserv/app/Application.cxx index 942dda4f8dd35f05acacaca9c97f4c1b64194983..14c5ee7aeede57d8d90f7d261c9dab74cf0ace67 100644 --- a/services/histserv/app/Application.cxx +++ b/services/histserv/app/Application.cxx @@ -42,7 +42,9 @@ using cbm::services::histserv::Application; // --------------------------------------------------------------------------------------------------------------------- // -Application::Application(ProgramOptions const& opt) : fOpt(opt) +Application::Application(ProgramOptions const& opt, volatile sig_atomic_t* signalStatus) + : fOpt(opt) + , fSignalStatus(signalStatus) { /// Read options from executable LOG(info) << "Options for Application:"; @@ -84,12 +86,6 @@ Application::Application(ProgramOptions const& opt) : fOpt(opt) fServer->Restrict("/Stop_Server", "allow=admin"); } - // Provide signal handling for external interruptions - // NOTE: SZh 02.04.2024: - // This function is needed for ZMQ to throw an exception (zmq::error_t) on the interrupt signal. It seems, that - // the actual body of the function does not matter. This behaviour is not understood. - signal(SIGINT, [](int) {}); - LOG(info) << "JSROOT location: " << jsrootsys; } @@ -100,7 +96,7 @@ void Application::Exec() fStopThread = false; fThread = std::thread(&Application::UpdateHttpServer, this); LOG(info) << "Listening to ZMQ messages ..."; - while (!(fUiCmdActor->GetServerStop())) { // + while (!(fUiCmdActor->GetServerStop()) && *fSignalStatus == 0) { try { /// Infinite loop, this is a service which should survive until told otherwise after all /// FIXME: Start listening to <SOMETHING?!?> to receive histograms and configuration @@ -111,27 +107,23 @@ void Application::Exec() if (!ret) continue; std::lock_guard<std::mutex> lk(mtx); - if (*ret > 3) { // + if (*ret > 3) { ReceiveConfigAndData(vMsg); } - else if (*ret == 1) { // + else if (*ret == 1) { ReceiveData(vMsg[0]); } - else { // + else { LOG(error) << "Invalid number of message parts received: should be either 1 or more than 3 vs " << *ret; } } catch (const zmq::error_t& err) { - if (err.num() == EINTR) { - // FIXME: SZh: Are the socket and the context finished properly? - LOG(info) << "Histogram server execution was interrupted by user. Finishing application"; - break; - } - else { - throw err; + if (err.num() != EINTR) { + throw; } } } + // FIXME: SZh: Are the socket and the context finished properly? } // --------------------------------------------------------------------------------------------------------------------- diff --git a/services/histserv/app/Application.h b/services/histserv/app/Application.h index 69ee11b6797c8143cef418d8bfd35d919c3555b6..29993ea08eed6f894517b5da73733dcec0985fba 100644 --- a/services/histserv/app/Application.h +++ b/services/histserv/app/Application.h @@ -25,7 +25,7 @@ namespace cbm::services::histserv public: /** @brief Standard constructor, initialises the application ** @param opt **/ - explicit Application(ProgramOptions const& opt); + explicit Application(ProgramOptions const& opt, volatile sig_atomic_t* signalStatus); /** @brief Copy constructor forbidden **/ Application(const Application&) = delete; @@ -84,6 +84,7 @@ namespace cbm::services::histserv private: ProgramOptions const& fOpt; ///< Program options object + volatile sig_atomic_t* fSignalStatus; ///< Global signal status THttpServer* fServer = nullptr; ///< ROOT Histogram server (JSroot) std::thread fThread; bool fStopThread = false; diff --git a/services/histserv/app/main.cxx b/services/histserv/app/main.cxx index 4dbaa8862b133acb0bcdf344088ea1fe386f0b77..61d1931a87be6e1001ad6d6542424a448bce4f17 100644 --- a/services/histserv/app/main.cxx +++ b/services/histserv/app/main.cxx @@ -5,15 +5,25 @@ #include "Application.h" #include "ProgramOptions.h" +#include <csignal> + +namespace { +volatile sig_atomic_t signal_status = 0; +} + +static void signal_handler(int sig) { signal_status = sig; } using namespace cbm::services::histserv; int main(int argc, char* argv[]) { + std::signal(SIGINT, signal_handler); + std::signal(SIGTERM, signal_handler); + LOG(info) << "***** Histogram server without FairMQ *****"; try { ProgramOptions opt(argc, argv); - Application app(opt); + Application app(opt, &signal_status); app.Exec(); } catch (std::exception const& e) {