From a2b46df66ce12ac31392cbfe80b1f983a9350278 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Tue, 1 Dec 2026 16:03:28 +0100 Subject: Reloading --- server/src/main.cpp | 129 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 90 insertions(+), 39 deletions(-) (limited to 'server/src/main.cpp') diff --git a/server/src/main.cpp b/server/src/main.cpp index f5d233b..a94ac7e 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -1,6 +1,7 @@ #include #include // for malloc_trim(3) #include +#include import std; import routemon; @@ -78,6 +79,52 @@ auto read_secret_to_string(std::string const& filename) -> std::string std::string_view{static_cast(buf.p), buf.size})}; } +auto load_situations( + routemon::log::logger const& l, + std::filesystem::path const& datex2_filename) + -> std::optional +{ + l.with("filename", datex2_filename).info("Loading situations"); + + auto const before_load = chrono::steady_clock::now(); + auto d2loader = routemon::datex2::loader{}; + auto pub = routemon::datex2::situation_publication{}; + try + { + pub = d2loader.load_situation_publication(datex2_filename); + } + catch (std::exception const& e) + { + l.error("Failed to load DATEX II situations publication: {}", e.what()); + return std::nullopt; + } + if (!d2loader.warnings().empty()) + { + auto const& warns = d2loader.warnings(); + l.warn( + "Encountered {} unique warning(s) while loading DATEX II situations " + "publication", + warns.size()); + auto i = 0uz; + for (auto it = warns.begin(); it != warns.end(); + it = warns.upper_bound(*it)) + { + l.warn("Warning {} (appeared {}×): {}", ++i, warns.count(*it), *it); + } + } + // Processing the feed is by far the most memory-intensive operation + // during the run time of this application (at the moment); the + // resident set will likely never be this big again. So we ask libc + // to return as much memory as possible to the OS. + malloc_trim(0); + auto const after_load = chrono::steady_clock::now(); + auto const dur_load = + chrono::duration_cast(after_load - before_load); + l.info("Loading situations finished in {}", dur_load); + + return pub; +} + auto server_main(routemon::log::logger& l, routemon::config::app config) -> exit_status { @@ -121,50 +168,54 @@ auto server_main(routemon::log::logger& l, routemon::config::app config) return exit_status::failure; } - l.with("filename", config.situations.datex2_filename) - .info("Loading situations"); - auto const before_load = chrono::steady_clock::now(); - auto d2loader = routemon::datex2::loader{}; - auto pub = routemon::datex2::situation_publication{}; - try - { - pub = - d2loader.load_situation_publication(config.situations.datex2_filename); - } - catch (std::exception const& e) - { - l.error("Failed to load DATEX II situations publication: {}", e.what()); + auto mpub = load_situations(l, config.situations.datex2_filename); + if (!mpub) return exit_status::failure; - } - if (!d2loader.warnings().empty()) - { - auto const& warns = d2loader.warnings(); - l.warn( - "Encountered {} unique warning(s) while loading DATEX II situations " - "publication", - warns.size()); - auto i = 0uz; - for (auto it = warns.begin(); it != warns.end(); - it = warns.upper_bound(*it)) - { - l.warn("Warning {} (appeared {}×): {}", ++i, warns.count(*it), *it); - } - } - // Processing the feed is by far the most memory-intensive operation - // during the run time of this application (at the moment); the - // resident set will likely never be this big again. So we ask libc - // to return as much memory as possible to the OS. - malloc_trim(0); - auto const after_load = chrono::steady_clock::now(); - auto const dur_load = - chrono::duration_cast(after_load - before_load); - l.info("Loading situations finished in {}", dur_load); - auto handler = routemon::api::handler{l, dbc, std::move(pub)}; + auto handler = + std::make_shared(l, dbc, std::move(*mpub)); auto http_server = routemon::http::server{ - l, config.http_server, rwgps_creds, std::move(lsel), std::move(handler), + l, config.http_server, rwgps_creds, std::move(lsel), handler, }; http_server.spawn(ioc); + + auto signal_handler = net::signal_set{ioc, SIGHUP}; + net::co_spawn( + ioc, + [&]() -> net::awaitable + { + sd_notify(0, "READY=1"); + + while (true) + { + auto signal = co_await signal_handler.async_wait(); + if (signal == SIGHUP) + { + auto const usec = static_cast( + std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count()); + auto const reloading_msg = std::format("RELOADING=1\nMONOTONIC_USEC={}", usec); + sd_notify(0, reloading_msg.c_str()); + + l.info("Received reload request"); + auto mpub = load_situations(l, config.situations.datex2_filename); + if (mpub) + { + handler->situation_publication(std::move(*mpub)); + l.info("Updated situation publication"); + } + else + { + l.error("Failed to update situation publication"); + } + + sd_notify(0, "READY=1"); + } + } + }, + net::detached); + ioc.run(); l.error("I/O context stopped"); -- cgit v1.3.1