Aktualisiere WebSocket-Server und Daemon-Konfiguration
- Ändere die Pfade für SSL-Zertifikate in der Konfigurationsdatei. - Verbessere die Fehlerbehandlung beim Entfernen alter vorbereiteter Anweisungen in HouseWorker. - Füge Debug-Ausgaben zur Nachverfolgung von Verbindungen und Nachrichten im WebSocket-Server hinzu. - Implementiere Timeout-Logik für das Stoppen von Worker- und Watchdog-Threads. - Optimiere die Signalverarbeitung und Shutdown-Logik in main.cpp für bessere Responsivität.
This commit is contained in:
committed by
Torsten (PC)
parent
8fe816dddc
commit
bd961a03d4
39
src/main.cpp
39
src/main.cpp
@@ -17,6 +17,7 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstdlib>
|
||||
#include <systemd/sd-daemon.h>
|
||||
|
||||
std::atomic<bool> keepRunning(true);
|
||||
@@ -26,8 +27,10 @@ void handleSignal(int signal) {
|
||||
std::cerr << "Signal erhalten: " << signal << ". Beende Anwendung..." << std::endl;
|
||||
|
||||
if (signal == SIGINT || signal == SIGTERM) {
|
||||
std::cerr << "Setze Shutdown-Flags..." << std::endl;
|
||||
keepRunning.store(false, std::memory_order_relaxed);
|
||||
shutdownRequested.store(true, std::memory_order_relaxed);
|
||||
std::cerr << "Shutdown-Flags gesetzt. keepRunning=" << keepRunning.load() << ", shutdownRequested=" << shutdownRequested.load() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +39,7 @@ int main() {
|
||||
std::signal(SIGTERM, handleSignal);
|
||||
|
||||
try {
|
||||
Config config("/etc/yourpart/daemon.conf");
|
||||
Config config("../daemon.conf");
|
||||
ConnectionPool pool(
|
||||
config.get("DB_HOST"),
|
||||
config.get("DB_PORT"),
|
||||
@@ -78,24 +81,52 @@ int main() {
|
||||
sd_notify(0, "READY=1");
|
||||
|
||||
// Hauptschleife mit besserer Signal-Behandlung
|
||||
while (keepRunning && !shutdownRequested) {
|
||||
std::cerr << "Hauptschleife gestartet. keepRunning=" << keepRunning.load() << ", shutdownRequested=" << shutdownRequested.load() << std::endl;
|
||||
while (keepRunning.load() && !shutdownRequested.load()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
std::cerr << "Hauptschleife beendet. keepRunning=" << keepRunning.load() << ", shutdownRequested=" << shutdownRequested.load() << std::endl;
|
||||
|
||||
std::cerr << "Starte sauberes Herunterfahren..." << std::endl;
|
||||
|
||||
// Stoppe alle Worker
|
||||
auto shutdownStart = std::chrono::steady_clock::now();
|
||||
const auto maxShutdownTime = std::chrono::seconds(10);
|
||||
|
||||
// Stoppe alle Worker-Threads
|
||||
std::cerr << "Stoppe Worker-Threads..." << std::endl;
|
||||
for (auto &worker : workers) {
|
||||
worker->stopWorkerThread();
|
||||
if (std::chrono::steady_clock::now() - shutdownStart > maxShutdownTime) {
|
||||
std::cerr << "Shutdown-Timeout erreicht, erzwinge Beendigung..." << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Stoppe Watchdog-Threads
|
||||
std::cerr << "Stoppe Watchdog-Threads..." << std::endl;
|
||||
for (auto &worker : workers) {
|
||||
worker->stopWatchdogThread();
|
||||
if (std::chrono::steady_clock::now() - shutdownStart > maxShutdownTime) {
|
||||
std::cerr << "Shutdown-Timeout erreicht, erzwinge Beendigung..." << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Stoppe WebSocket Server
|
||||
std::cerr << "Stoppe WebSocket-Server..." << std::endl;
|
||||
websocketServer.stop();
|
||||
|
||||
// Stoppe Message Broker
|
||||
std::cerr << "Stoppe Message Broker..." << std::endl;
|
||||
broker.stop();
|
||||
|
||||
std::cerr << "Anwendung erfolgreich beendet." << std::endl;
|
||||
auto shutdownDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now() - shutdownStart);
|
||||
std::cerr << "Anwendung erfolgreich beendet in " << shutdownDuration.count() << "ms." << std::endl;
|
||||
|
||||
// Erzwinge sofortiges Exit nach Shutdown
|
||||
std::cerr << "Erzwinge Prozess-Beendigung..." << std::endl;
|
||||
std::_Exit(0);
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Fehler: " << e.what() << std::endl;
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user