From 8c6fecd356003309f8eebec30374344272ca6072 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 19 Mar 2026 12:33:48 +0000 Subject: Gracefully handle SIGTERM Apache sends SIGTERM to the logger process to it shutdown. Honestly I thought it would just close stdin and I should have checked. --- src/ingestor.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/ingestor.cpp') diff --git a/src/ingestor.cpp b/src/ingestor.cpp index def3ce1..e2018ad 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -3,6 +3,7 @@ #include "uaLookup.hpp" #include "util.hpp" #include +#include #include #include #include @@ -64,8 +65,11 @@ namespace WebStat { return std::make_tuple(std::get(ENTITY_TYPE_MAP)(std::get(values))...); }(std::make_index_sequence()); } + } + Ingestor * Ingestor::currentIngestor = nullptr; + Ingestor::Ingestor(const utsname & host, IngestorSettings settings) : Ingestor {host, std::make_shared( @@ -84,6 +88,31 @@ namespace WebStat { bindMany(ins, 0, hostnameId, host.nodename, host.sysname, host.release, host.version, host.machine, host.domainname); ins->execute(); + + assert(!currentIngestor); + currentIngestor = this; + signal(SIGTERM, &sigtermHandler); + } + + Ingestor::~Ingestor() + { + assert(currentIngestor); + signal(SIGTERM, SIG_DFL); + currentIngestor = nullptr; + } + + void + Ingestor::sigtermHandler(int sigNo) + { + assert(currentIngestor); + currentIngestor->terminate(sigNo); + } + + void + Ingestor::terminate(int) + { + terminated = true; + curl_multi_wakeup(curl.get()); } Ingestor::ScanResult @@ -138,7 +167,7 @@ namespace WebStat { const auto curlTimeOut = static_cast( std::chrono::duration_cast(settings.checkJobsAfter).count()); - while (curl_multi_poll(curl.get(), &logIn, 1, curlTimeOut, nullptr) == CURLM_OK) { + while (!terminated && curl_multi_poll(curl.get(), &logIn, 1, curlTimeOut, nullptr) == CURLM_OK) { if (logIn.revents) { if (auto line = scn::scan(input, "{:[^\n]}\n")) { linesRead++; -- cgit v1.3