Notify systemd when the service is ready or stopping

This commit is contained in:
Martchus 2023-10-16 23:48:19 +02:00
parent 37ee4f8fa7
commit d3e556cea7
4 changed files with 43 additions and 0 deletions

View File

@ -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)

View File

@ -35,6 +35,10 @@
#include <sys/resource.h>
#endif
#ifdef USE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
#include <algorithm>
#include <cerrno>
#include <cstdlib>
@ -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);

View File

@ -9,6 +9,10 @@
#include <boost/asio/signal_set.hpp>
#ifdef USE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
#include <iostream>
#include <thread>
@ -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()

View File

@ -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