diff options
| author | Rutger Broekhoff | 2026-12-01 16:03:28 +0100 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-12-01 16:03:28 +0100 |
| commit | a2b46df66ce12ac31392cbfe80b1f983a9350278 (patch) | |
| tree | 76805a95b221ecf8afed3075f09290f98789238a /server/src/main.cpp | |
| parent | 9b8d8fccaec087031ba37dd68e516929c3ddb4a2 (diff) | |
| download | routemon-a2b46df66ce12ac31392cbfe80b1f983a9350278.tar.gz routemon-a2b46df66ce12ac31392cbfe80b1f983a9350278.zip | |
Reloading
Diffstat (limited to 'server/src/main.cpp')
| -rw-r--r-- | server/src/main.cpp | 129 |
1 files changed, 90 insertions, 39 deletions
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 @@ | |||
| 1 | #include <boost/asio.hpp> | 1 | #include <boost/asio.hpp> |
| 2 | #include <malloc.h> // for malloc_trim(3) | 2 | #include <malloc.h> // for malloc_trim(3) |
| 3 | #include <openssl/crypto.h> | 3 | #include <openssl/crypto.h> |
| 4 | #include <systemd/sd-daemon.h> | ||
| 4 | 5 | ||
| 5 | import std; | 6 | import std; |
| 6 | import routemon; | 7 | import routemon; |
| @@ -78,6 +79,52 @@ auto read_secret_to_string(std::string const& filename) -> std::string | |||
| 78 | std::string_view{static_cast<char const*>(buf.p), buf.size})}; | 79 | std::string_view{static_cast<char const*>(buf.p), buf.size})}; |
| 79 | } | 80 | } |
| 80 | 81 | ||
| 82 | auto load_situations( | ||
| 83 | routemon::log::logger const& l, | ||
| 84 | std::filesystem::path const& datex2_filename) | ||
| 85 | -> std::optional<routemon::datex2::situation_publication> | ||
| 86 | { | ||
| 87 | l.with("filename", datex2_filename).info("Loading situations"); | ||
| 88 | |||
| 89 | auto const before_load = chrono::steady_clock::now(); | ||
| 90 | auto d2loader = routemon::datex2::loader{}; | ||
| 91 | auto pub = routemon::datex2::situation_publication{}; | ||
| 92 | try | ||
| 93 | { | ||
| 94 | pub = d2loader.load_situation_publication(datex2_filename); | ||
| 95 | } | ||
| 96 | catch (std::exception const& e) | ||
| 97 | { | ||
| 98 | l.error("Failed to load DATEX II situations publication: {}", e.what()); | ||
| 99 | return std::nullopt; | ||
| 100 | } | ||
| 101 | if (!d2loader.warnings().empty()) | ||
| 102 | { | ||
| 103 | auto const& warns = d2loader.warnings(); | ||
| 104 | l.warn( | ||
| 105 | "Encountered {} unique warning(s) while loading DATEX II situations " | ||
| 106 | "publication", | ||
| 107 | warns.size()); | ||
| 108 | auto i = 0uz; | ||
| 109 | for (auto it = warns.begin(); it != warns.end(); | ||
| 110 | it = warns.upper_bound(*it)) | ||
| 111 | { | ||
| 112 | l.warn("Warning {} (appeared {}×): {}", ++i, warns.count(*it), *it); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | // Processing the feed is by far the most memory-intensive operation | ||
| 116 | // during the run time of this application (at the moment); the | ||
| 117 | // resident set will likely never be this big again. So we ask libc | ||
| 118 | // to return as much memory as possible to the OS. | ||
| 119 | malloc_trim(0); | ||
| 120 | auto const after_load = chrono::steady_clock::now(); | ||
| 121 | auto const dur_load = | ||
| 122 | chrono::duration_cast<chrono::milliseconds>(after_load - before_load); | ||
| 123 | l.info("Loading situations finished in {}", dur_load); | ||
| 124 | |||
| 125 | return pub; | ||
| 126 | } | ||
| 127 | |||
| 81 | auto server_main(routemon::log::logger& l, routemon::config::app config) | 128 | auto server_main(routemon::log::logger& l, routemon::config::app config) |
| 82 | -> exit_status | 129 | -> exit_status |
| 83 | { | 130 | { |
| @@ -121,50 +168,54 @@ auto server_main(routemon::log::logger& l, routemon::config::app config) | |||
| 121 | return exit_status::failure; | 168 | return exit_status::failure; |
| 122 | } | 169 | } |
| 123 | 170 | ||
| 124 | l.with("filename", config.situations.datex2_filename) | 171 | auto mpub = load_situations(l, config.situations.datex2_filename); |
| 125 | .info("Loading situations"); | 172 | if (!mpub) |
| 126 | auto const before_load = chrono::steady_clock::now(); | ||
| 127 | auto d2loader = routemon::datex2::loader{}; | ||
| 128 | auto pub = routemon::datex2::situation_publication{}; | ||
| 129 | try | ||
| 130 | { | ||
| 131 | pub = | ||
| 132 | d2loader.load_situation_publication(config.situations.datex2_filename); | ||
| 133 | } | ||
| 134 | catch (std::exception const& e) | ||
| 135 | { | ||
| 136 | l.error("Failed to load DATEX II situations publication: {}", e.what()); | ||
| 137 | return exit_status::failure; | 173 | return exit_status::failure; |
| 138 | } | ||
| 139 | if (!d2loader.warnings().empty()) | ||
| 140 | { | ||
| 141 | auto const& warns = d2loader.warnings(); | ||
| 142 | l.warn( | ||
| 143 | "Encountered {} unique warning(s) while loading DATEX II situations " | ||
| 144 | "publication", | ||
| 145 | warns.size()); | ||
| 146 | auto i = 0uz; | ||
| 147 | for (auto it = warns.begin(); it != warns.end(); | ||
| 148 | it = warns.upper_bound(*it)) | ||
| 149 | { | ||
| 150 | l.warn("Warning {} (appeared {}×): {}", ++i, warns.count(*it), *it); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | // Processing the feed is by far the most memory-intensive operation | ||
| 154 | // during the run time of this application (at the moment); the | ||
| 155 | // resident set will likely never be this big again. So we ask libc | ||
| 156 | // to return as much memory as possible to the OS. | ||
| 157 | malloc_trim(0); | ||
| 158 | auto const after_load = chrono::steady_clock::now(); | ||
| 159 | auto const dur_load = | ||
| 160 | chrono::duration_cast<chrono::milliseconds>(after_load - before_load); | ||
| 161 | l.info("Loading situations finished in {}", dur_load); | ||
| 162 | 174 | ||
| 163 | auto handler = routemon::api::handler{l, dbc, std::move(pub)}; | 175 | auto handler = |
| 176 | std::make_shared<routemon::api::handler>(l, dbc, std::move(*mpub)); | ||
| 164 | auto http_server = routemon::http::server{ | 177 | auto http_server = routemon::http::server{ |
| 165 | l, config.http_server, rwgps_creds, std::move(lsel), std::move(handler), | 178 | l, config.http_server, rwgps_creds, std::move(lsel), handler, |
| 166 | }; | 179 | }; |
| 167 | http_server.spawn(ioc); | 180 | http_server.spawn(ioc); |
| 181 | |||
| 182 | auto signal_handler = net::signal_set{ioc, SIGHUP}; | ||
| 183 | net::co_spawn( | ||
| 184 | ioc, | ||
| 185 | [&]() -> net::awaitable<void> | ||
| 186 | { | ||
| 187 | sd_notify(0, "READY=1"); | ||
| 188 | |||
| 189 | while (true) | ||
| 190 | { | ||
| 191 | auto signal = co_await signal_handler.async_wait(); | ||
| 192 | if (signal == SIGHUP) | ||
| 193 | { | ||
| 194 | auto const usec = static_cast<std::int64_t>( | ||
| 195 | std::chrono::duration_cast<std::chrono::microseconds>( | ||
| 196 | std::chrono::steady_clock::now().time_since_epoch()) | ||
| 197 | .count()); | ||
| 198 | auto const reloading_msg = std::format("RELOADING=1\nMONOTONIC_USEC={}", usec); | ||
| 199 | sd_notify(0, reloading_msg.c_str()); | ||
| 200 | |||
| 201 | l.info("Received reload request"); | ||
| 202 | auto mpub = load_situations(l, config.situations.datex2_filename); | ||
| 203 | if (mpub) | ||
| 204 | { | ||
| 205 | handler->situation_publication(std::move(*mpub)); | ||
| 206 | l.info("Updated situation publication"); | ||
| 207 | } | ||
| 208 | else | ||
| 209 | { | ||
| 210 | l.error("Failed to update situation publication"); | ||
| 211 | } | ||
| 212 | |||
| 213 | sd_notify(0, "READY=1"); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | }, | ||
| 217 | net::detached); | ||
| 218 | |||
| 168 | ioc.run(); | 219 | ioc.run(); |
| 169 | 220 | ||
| 170 | l.error("I/O context stopped"); | 221 | l.error("I/O context stopped"); |