fix
This commit is contained in:
117
src/worker.h
Normal file
117
src/worker.h
Normal file
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
|
||||
#include "connection_pool.h"
|
||||
#include "message_broker.h"
|
||||
#include "database.h"
|
||||
|
||||
class Worker {
|
||||
public:
|
||||
Worker(ConnectionPool &pool, MessageBroker &broker, std::string name)
|
||||
: pool(pool),
|
||||
broker(broker),
|
||||
workerName(std::move(name)),
|
||||
runningWorker(false),
|
||||
runningWatchdog(false)
|
||||
{}
|
||||
|
||||
virtual ~Worker() {
|
||||
stopWorkerThread();
|
||||
stopWatchdogThread();
|
||||
}
|
||||
|
||||
void startWorkerThread() {
|
||||
if (runningWorker.load()) {
|
||||
std::cerr << "[" << workerName << "] Worker thread already running, skipping start.\n";
|
||||
return;
|
||||
}
|
||||
runningWorker.store(true);
|
||||
workerThread = std::thread([this]() { run(); });
|
||||
}
|
||||
|
||||
void stopWorkerThread() {
|
||||
runningWorker.store(false);
|
||||
if (workerThread.joinable()) {
|
||||
workerThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void enableWatchdog() {
|
||||
if (runningWatchdog.load()) {
|
||||
std::cerr << "[" << workerName << "] Watchdog already enabled, skipping.\n";
|
||||
return;
|
||||
}
|
||||
runningWatchdog.store(true);
|
||||
watchdogThread = std::thread([this]() { watchdog(); });
|
||||
}
|
||||
|
||||
void stopWatchdogThread() {
|
||||
runningWatchdog.store(false);
|
||||
if (watchdogThread.joinable()) {
|
||||
watchdogThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
std::string getCurrentStep() {
|
||||
std::lock_guard<std::mutex> lock(stepMutex);
|
||||
return currentStep;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void run() = 0;
|
||||
|
||||
void watchdog() {
|
||||
try {
|
||||
while (runningWatchdog.load()) {
|
||||
std::this_thread::sleep_for(watchdogInterval);
|
||||
bool isActive = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(activityMutex);
|
||||
isActive = active;
|
||||
active = false;
|
||||
}
|
||||
if (!isActive) {
|
||||
std::cerr << "[" << workerName << "] Watchdog: Keine Aktivität! Starte Worker neu...\n";
|
||||
std::cerr << "[" << workerName << "] Letzte Aktivität: " << getCurrentStep() << "\n";
|
||||
stopWorkerThread();
|
||||
startWorkerThread();
|
||||
}
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "[" << workerName << "] Watchdog: Ausnahme gefangen: " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
std::cerr << "[" << workerName << "] Watchdog: Unbekannte Ausnahme gefangen.\n";
|
||||
}
|
||||
}
|
||||
|
||||
void signalActivity() {
|
||||
std::lock_guard<std::mutex> lock(activityMutex);
|
||||
active = true;
|
||||
}
|
||||
|
||||
void setCurrentStep(const std::string &step) {
|
||||
std::lock_guard<std::mutex> lock(stepMutex);
|
||||
currentStep = step;
|
||||
}
|
||||
|
||||
protected:
|
||||
ConnectionPool &pool;
|
||||
MessageBroker &broker;
|
||||
std::string workerName;
|
||||
std::atomic<bool> runningWorker;
|
||||
std::atomic<bool> runningWatchdog;
|
||||
std::atomic<bool> active{false};
|
||||
std::thread workerThread;
|
||||
std::thread watchdogThread;
|
||||
std::mutex activityMutex;
|
||||
std::chrono::seconds watchdogInterval{10};
|
||||
std::mutex stepMutex;
|
||||
std::string currentStep;
|
||||
};
|
||||
Reference in New Issue
Block a user