summaryrefslogtreecommitdiffstats
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/main.cpp55
1 files changed, 48 insertions, 7 deletions
diff --git a/server/src/main.cpp b/server/src/main.cpp
index 9d576a2..fa85f78 100644
--- a/server/src/main.cpp
+++ b/server/src/main.cpp
@@ -1,7 +1,6 @@
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>
5 4
6import std; 5import std;
7import routemon; 6import routemon;
@@ -125,6 +124,33 @@ auto load_situations(
125 return pub; 124 return pub;
126} 125}
127 126
127auto sd_notify(std::string_view msg) -> net::awaitable<void>
128{
129 try
130 {
131 auto endpoint_cstr = std::getenv("NOTIFY_SOCKET");
132 if (!endpoint_cstr || endpoint_cstr[0] == '\0')
133 co_return;
134 if (endpoint_cstr[0] != '/' && endpoint_cstr[0] != '@')
135 throw std::runtime_error{
136 "NOTIFY_SOCKET specifies an unsupported endpoint"
137 };
138 auto endpoint_str = std::string{endpoint_cstr};
139 if (endpoint_str[0] == '@')
140 endpoint_str[0] = '\0';
141 auto endpoint = net::local::datagram_protocol::endpoint{endpoint_str};
142 auto sock = net::local::datagram_protocol::socket{
143 co_await net::this_coro::executor
144 };
145 co_await sock.async_send_to(net::buffer(msg), endpoint);
146 }
147 catch (std::exception const& e)
148 {
149 throw std::runtime_error{std::format(
150 "failed to notify service manager: {}", e.what())};
151 }
152}
153
128auto server_main(routemon::log::logger& l, routemon::config::app config) 154auto server_main(routemon::log::logger& l, routemon::config::app config)
129 -> exit_status 155 -> exit_status
130{ 156{
@@ -185,7 +211,7 @@ auto server_main(routemon::log::logger& l, routemon::config::app config)
185 ioc, 211 ioc,
186 [&]() -> net::awaitable<void> 212 [&]() -> net::awaitable<void>
187 { 213 {
188 sd_notify(0, "READY=1"); 214 co_await sd_notify("READY=1");
189 215
190 while (true) 216 while (true)
191 { 217 {
@@ -196,8 +222,8 @@ auto server_main(routemon::log::logger& l, routemon::config::app config)
196 std::chrono::duration_cast<std::chrono::microseconds>( 222 std::chrono::duration_cast<std::chrono::microseconds>(
197 std::chrono::steady_clock::now().time_since_epoch()) 223 std::chrono::steady_clock::now().time_since_epoch())
198 .count()); 224 .count());
199 auto const reloading_msg = std::format("RELOADING=1\nMONOTONIC_USEC={}", usec); 225 co_await sd_notify(
200 sd_notify(0, reloading_msg.c_str()); 226 std::format("RELOADING=1\nMONOTONIC_USEC={}", usec));
201 227
202 l.info("Received reload request"); 228 l.info("Received reload request");
203 auto mpub = load_situations(l, config.situations.datex2_filename); 229 auto mpub = load_situations(l, config.situations.datex2_filename);
@@ -211,11 +237,26 @@ auto server_main(routemon::log::logger& l, routemon::config::app config)
211 l.error("Failed to update situation publication"); 237 l.error("Failed to update situation publication");
212 } 238 }
213 239
214 sd_notify(0, "READY=1"); 240 co_await sd_notify("READY=1");
215 } 241 }
216 } 242 }
217 }, 243 },
218 net::detached); 244 [&](std::exception_ptr e)
245 {
246 if (e)
247 {
248 try
249 {
250 std::rethrow_exception(e);
251 }
252 catch (std::exception const& e)
253 {
254 l.sub("signal_handler")
255 .error("Error in signal handler: {}", e.what());
256 ioc.stop();
257 }
258 }
259 });
219 260
220 auto threads = std::vector<std::thread>{nthread}; 261 auto threads = std::vector<std::thread>{nthread};
221 for (auto& thread : threads) 262 for (auto& thread : threads)
@@ -223,7 +264,7 @@ auto server_main(routemon::log::logger& l, routemon::config::app config)
223 for (auto& thread : threads) 264 for (auto& thread : threads)
224 thread.join(); 265 thread.join();
225 266
226 l.error("I/O contexts stopped"); 267 l.error("I/O context event processing loop stoped");
227 return exit_status::failure; 268 return exit_status::failure;
228} 269}
229 270