stabilized app
This commit is contained in:
committed by
Torsten (PC)
parent
51fd9fcd13
commit
1451225978
62
src/worker.h
62
src/worker.h
@@ -3,14 +3,14 @@
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "connection_pool.h"
|
||||
#include "message_broker.h"
|
||||
#include "database.h"
|
||||
#include "connection_guard.h"
|
||||
|
||||
class Worker {
|
||||
public:
|
||||
@@ -64,6 +64,11 @@ public:
|
||||
return currentStep;
|
||||
}
|
||||
|
||||
std::string getStatus() {
|
||||
std::lock_guard<std::mutex> lock(stepMutex);
|
||||
return "{\"worker\":\"" + workerName + "\", \"currentStep\":\"" + currentStep + "\"}";
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void run() = 0;
|
||||
|
||||
@@ -101,6 +106,42 @@ protected:
|
||||
currentStep = step;
|
||||
}
|
||||
|
||||
void sendMessageToRegionUsers(const int ®ionId, nlohmann::json message) {
|
||||
ConnectionGuard guard(pool);
|
||||
auto &db = guard.get();
|
||||
db.prepare("QUERY_GET_REGION_USERS", QUERY_GET_REGION_USERS);
|
||||
auto users = db.execute("QUERY_GET_REGION_USERS", {std::to_string(regionId)});
|
||||
for (const auto &user: users) {
|
||||
message["user_id"] = user.at("user_id");
|
||||
broker.publish(message.dump());
|
||||
}
|
||||
}
|
||||
|
||||
void sendMessageToFalukantUsers(const int &falukantUserId, nlohmann::json message) {
|
||||
message["user_id"] = falukantUserId;
|
||||
broker.publish(message.dump());
|
||||
}
|
||||
|
||||
void changeFalukantUserMoney(int falukantUserId, double moneyChange, std::string action, nlohmann::json message) {
|
||||
try {
|
||||
ConnectionGuard connGuard(pool);
|
||||
auto &db = connGuard.get();
|
||||
db.prepare("QUERY_UPDATE_MONEY", QUERY_UPDATE_MONEY);
|
||||
db.execute("QUERY_UPDATE_MONEY", {
|
||||
std::to_string(falukantUserId),
|
||||
std::to_string(moneyChange),
|
||||
action
|
||||
});
|
||||
sendMessageToFalukantUsers(falukantUserId, message);
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "[" << workerName << "] Fehler in changeFalukantUserMoney: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
time_t getLastActivity() {
|
||||
return lastActivity;
|
||||
}
|
||||
|
||||
protected:
|
||||
ConnectionPool &pool;
|
||||
MessageBroker &broker;
|
||||
@@ -114,4 +155,21 @@ protected:
|
||||
std::chrono::seconds watchdogInterval{10};
|
||||
std::mutex stepMutex;
|
||||
std::string currentStep;
|
||||
time_t lastActivity;
|
||||
|
||||
private:
|
||||
static constexpr const char *QUERY_GET_REGION_USERS = R"(
|
||||
select c.user_id
|
||||
from falukant_data."character" c
|
||||
where c.region_id = $1
|
||||
and c.user_id is not null;
|
||||
)";
|
||||
|
||||
static constexpr const char *QUERY_UPDATE_MONEY = R"(
|
||||
SELECT falukant_data.update_money(
|
||||
$1,
|
||||
$2,
|
||||
$3
|
||||
);
|
||||
)";
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user