switch to coroutine execution model

This commit is contained in:
Martin Michelsen
2025-04-30 21:43:06 -07:00
parent f65b1f1c14
commit cc99050964
160 changed files with 269127 additions and 227736 deletions
+29 -35
View File
@@ -1,6 +1,5 @@
#include "SignalWatcher.hh"
#include <event2/event.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
@@ -18,47 +17,42 @@ using namespace std;
SignalWatcher::SignalWatcher(shared_ptr<ServerState> state)
: log("[SignalWatcher] "),
state(state),
sigusr1_event(evsignal_new(this->state->base.get(), SIGUSR1, &SignalWatcher::dispatch_on_signal, this), event_free),
sigusr2_event(evsignal_new(this->state->base.get(), SIGUSR2, &SignalWatcher::dispatch_on_signal, this), event_free) {
event_add(this->sigusr1_event.get(), nullptr);
event_add(this->sigusr2_event.get(), nullptr);
signals(*this->state->io_context) {
asio::co_spawn(*this->state->io_context, this->signal_handler_task(), asio::detached);
}
void SignalWatcher::dispatch_on_signal(evutil_socket_t signal, short what, void* ctx) {
if (what != EV_SIGNAL) {
throw logic_error("dispatch_on_signal called for non-signal event");
}
reinterpret_cast<SignalWatcher*>(ctx)->on_signal(signal);
}
void SignalWatcher::on_signal(int signum) {
switch (signum) {
case SIGUSR1:
this->log.info("Received SIGUSR1; reloading config.json");
this->state->forward_to_event_thread([s = this->state]() {
asio::awaitable<void> SignalWatcher::signal_handler_task() {
#ifndef PHOSG_WINDOWS
this->signals.add(SIGUSR1);
this->signals.add(SIGUSR2);
for (;;) {
int signum = co_await this->signals.async_wait(asio::use_awaitable);
switch (signum) {
case SIGUSR1:
this->log.info_f("Received SIGUSR1; reloading config.json");
try {
s->load_config_early();
s->load_config_late();
fprintf(stderr, "Configuration update complete\n");
this->state->load_config_early();
this->state->load_config_late();
phosg::fwrite_fmt(stderr, "Configuration update complete\n");
} catch (const exception& e) {
fprintf(stderr, "FAILED: %s\n", e.what());
fprintf(stderr, "Some configuration may have been reloaded. Fix the underlying issue and try again.\n");
phosg::fwrite_fmt(stderr, "FAILED: {}\n", e.what());
phosg::fwrite_fmt(stderr, "Some configuration may have been reloaded. Fix the underlying issue and try again.\n");
}
});
break;
case SIGUSR2:
this->log.info("Received SIGUSR2; reloading config.json and all dependencies");
this->state->forward_to_event_thread([s = this->state]() {
break;
case SIGUSR2:
this->log.info_f("Received SIGUSR2; reloading config.json and all dependencies");
try {
s->load_all();
fprintf(stderr, "Configuration update complete\n");
this->state->load_all(true);
phosg::fwrite_fmt(stderr, "Configuration update complete\n");
} catch (const exception& e) {
fprintf(stderr, "FAILED: %s\n", e.what());
fprintf(stderr, "Some configuration may have been reloaded. Fix the underlying issue and try again.\n");
phosg::fwrite_fmt(stderr, "FAILED: {}\n", e.what());
phosg::fwrite_fmt(stderr, "Some configuration may have been reloaded. Fix the underlying issue and try again.\n");
}
});
break;
default:
this->log.warning("Unknown signal received: %d", signum);
break;
default:
this->log.warning_f("Unknown signal received: {}", signum);
}
}
#endif
co_return;
}