diff --git a/librepomgr/CMakeLists.txt b/librepomgr/CMakeLists.txt index 4f06445..d6ffb5e 100644 --- a/librepomgr/CMakeLists.txt +++ b/librepomgr/CMakeLists.txt @@ -104,6 +104,17 @@ if (BOOST_ASIO_IO_URING) use_pkg_config_module(PKG_CONFIG_MODULES "liburing" VISIBILITY PUBLIC) endif () +# find systemd library +option(SYSTEMD_SUPPORT "enable use of libsystemd for signaling when the service is ready" ON) +if (SYSTEMD_SUPPORT) + message(STATUS "Using libsystemd") + set_property( + SOURCE serversetup.cpp webapi/server.cpp + APPEND + PROPERTY COMPILE_DEFINITIONS USE_LIBSYSTEMD) + use_pkg_config_module(PKG_CONFIG_MODULES "libsystemd" VISIBILITY PRIVATE) +endif () + # find reflective-rapidjson find_package(reflective_rapidjson${CONFIGURATION_PACKAGE_SUFFIX} REQUIRED) use_reflective_rapidjson(VISIBILITY PUBLIC) diff --git a/librepomgr/serversetup.cpp b/librepomgr/serversetup.cpp index f03f557..48eadd5 100644 --- a/librepomgr/serversetup.cpp +++ b/librepomgr/serversetup.cpp @@ -35,6 +35,10 @@ #include #endif +#ifdef USE_LIBSYSTEMD +#include +#endif + #include #include #include @@ -768,6 +772,10 @@ void ServiceSetup::initStorage() int ServiceSetup::run() { +#ifdef USE_LIBSYSTEMD + sd_notify(0, "STATUS=Loading databases"); +#endif + #ifndef CPP_UTILITIES_DEBUG_BUILD try { #endif @@ -793,12 +801,21 @@ int ServiceSetup::run() #endif printDatabases(); +#ifdef USE_LIBSYSTEMD + sd_notify(0, "STATUS=Initializing SSL"); +#endif cout << Phrases::SuccessMessage << "Initializing SSL" << Phrases::End; webServer.initSsl(); +#ifdef USE_LIBSYSTEMD + sd_notify(0, "STATUS=Allocating worker thread pool"); +#endif cout << Phrases::SuccessMessage << "Allocating worker thread pool (thread count: " << building.threadCount << ")" << Phrases::End; const auto buildWorker = building.allocateBuildWorker(); +#ifdef USE_LIBSYSTEMD + sd_notify(0, "STATUS=Starting web server"); +#endif cout << Phrases::SuccessMessage << "Starting web server (thread count: " << webServer.threadCount << "):" << TextAttribute::Reset << " http://" << webServer.address << ':' << webServer.port << endl; WebAPI::Server::serve(*this); diff --git a/librepomgr/webapi/server.cpp b/librepomgr/webapi/server.cpp index 6b9a103..e299837 100644 --- a/librepomgr/webapi/server.cpp +++ b/librepomgr/webapi/server.cpp @@ -9,6 +9,10 @@ #include +#ifdef USE_LIBSYSTEMD +#include +#endif + #include #include @@ -76,9 +80,19 @@ void Server::serve(ServiceSetup &setup) } }); + // signal systemd that the service is ready +#ifdef USE_LIBSYSTEMD + sd_notify(0, argsToString("READY=1\nSTATUS=Listening on http://", setup.webServer.address.to_string(), ':', setup.webServer.port).data()); +#endif + // run the IO service on the requested number of threads const auto additionalThreads = ThreadPool("Web server", setup.webServer.ioContext, setup.webServer.threadCount - 1); setup.webServer.ioContext.run(); + + // signal systemd that the service is stopping +#ifdef USE_LIBSYSTEMD + sd_notify(0, "STOPPING=1"); +#endif } void Server::stop() diff --git a/srv/cmake/templates/systemd.service.in b/srv/cmake/templates/systemd.service.in index aab2bd2..56271f6 100644 --- a/srv/cmake/templates/systemd.service.in +++ b/srv/cmake/templates/systemd.service.in @@ -3,6 +3,7 @@ Description=@SYSTEMD_SERVICE_DESCRIPTION@ After=network.target [Service] +Type=notify User=@SYSTEMD_SERVICE_USER_NAME@ ExecStart=@SYSTEMD_SERVICE_EXEC@ KillSignal=SIGQUIT