-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cpp
executable file
·149 lines (126 loc) · 6.93 KB
/
Controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Created by imelker on 06.03.2021.
//
#include "app.h"
#include <algorithm>
#include <chrono>
#include <so_5/send_functions.hpp>
#include <so_5/disp/adv_thread_pool/pub.hpp>
#include <so_5/disp/active_obj/pub.hpp>
#include <so_5/disp/prio_one_thread/strictly_ordered/pub.hpp>
#include "nlohmann/json.hpp"
#include "Utils.h"
#include "SysSignal.h"
#include "ThreadName.h"
#include "Logger.h"
#include "LoggerFactory.h"
#include "db/DBConnectionLock.h"
#include "irc/IRCConnectionConfig.h"
#include "Controller.h"
using json = nlohmann::json;
Controller::Controller(const context_t& ctx, so_5::mbox_t http, Config &config,
std::shared_ptr<DBController> db, std::shared_ptr<Logger> logger)
: so_5::agent_t(ctx),
config(config),
logger(std::move(logger)),
db(std::move(db)),
http(std::move(http)) {
}
Controller::~Controller() {
logger->logTrace("Controller end of controller");
};
void Controller::so_define_agent() {
so_subscribe_self().event([this](mhood_t<Controller::ShutdownCheck>) {
if (SysSignal::serviceTerminated()) {
so_deregister_agent_coop_normally();
}
});
so_subscribe(http).event([this](mhood_t<hreq::app::shutdown> req) {
json body = json::object();
body["result"] = "Terminating";
so_5::send<hreq::resp>(http, std::move(req->req), std::move(req->send), 200, body.dump());
SysSignal::setServiceTerminated(true);
});
so_subscribe(http).event([this](mhood_t<hreq::app::version> req) {
json body = json::object();
body["version"] = APP_NAME " " APP_VERSION " " APP_GIT_DATE " (" APP_GIT_HASH ")";
so_5::send<hreq::resp>(http, std::move(req->req), std::move(req->send), 200, body.dump());
});
}
void Controller::so_evt_start() {
set_thread_name("controller");
so_5::introduce_child_coop(*this, [&] (so_5::coop_t &coop) {
auto listener = so_environment().create_mbox();
statsCollector = makeStatsCollector(coop, listener);
storage = makeStorage(coop, listener, statsCollector->so_direct_mbox());
botsEnvironment = makeBotsEnvironment(coop, listener, statsCollector->so_direct_mbox());
msgProcessor = makeMessageProcessor(coop, listener /*as publisher*/, statsCollector->so_direct_mbox());
ircController = makeIRCController(coop, statsCollector->so_direct_mbox());
botsEnvironment->setMessageSender(ircController->so_direct_mbox());
botsEnvironment->setBotLogger(storage->so_direct_mbox());
});
shutdownCheckTimer = so_5::send_periodic<Controller::ShutdownCheck>(so_direct_mbox(),
std::chrono::seconds(1),
std::chrono::seconds(1));
}
void Controller::so_evt_finish() {
// TODO notify about it
}
StatsCollector *Controller::makeStatsCollector(so_5::coop_t &coop, const so_5::mbox_t &listener) {
//auto statsDisp = so_5::disp::prio_one_thread::strictly_ordered::make_dispatcher(so_environment());
auto statsDisp = so_5::disp::active_obj::make_dispatcher(so_environment(), "stats_collector");
return coop.make_agent_with_binder<StatsCollector>(statsDisp.binder(),
listener, http, logger, db);
}
Storage * Controller::makeStorage(so_5::coop_t &coop, const so_5::mbox_t &listener, const so_5::mbox_t &stats) {
CHConnectionConfig chCfg;
chCfg.host = config[CLICKHOUSE]["host"].value_or("localhost");
chCfg.port = config[CLICKHOUSE]["port"].value_or(5432);
chCfg.dbname = config[CLICKHOUSE]["dbname"].value_or("postgres");
chCfg.user = config[CLICKHOUSE]["user"].value_or("postgres");
chCfg.pass = config[CLICKHOUSE]["password"].value_or("postgres");
chCfg.secure = config[CLICKHOUSE]["secure"].value_or(false);
chCfg.verify = config[CLICKHOUSE]["verify"].value_or(true);
chCfg.sendRetries = config[CLICKHOUSE]["send_retries"].value_or(5);
int batchSize = config[CLICKHOUSE]["batch_size"].value_or(1000);
unsigned int chConns = config[CLICKHOUSE]["connections"].value_or(1);
unsigned int botLogFlushDelay = config[CLICKHOUSE]["bot_log_flush_delay"].value_or(1);
unsigned int messagesFlushDelay = config[CLICKHOUSE]["messages_flush_delay"].value_or(1);
auto chLogger = LoggerFactory::create(LoggerFactory::config(config, CLICKHOUSE));
auto chDisp = so_5::disp::active_obj::make_dispatcher(so_environment(), "storage");
return coop.make_agent_with_binder<Storage>(chDisp.binder(),
listener, stats, std::move(chCfg), chConns,
batchSize, messagesFlushDelay, botLogFlushDelay,
chLogger);
}
BotsEnvironment *Controller::makeBotsEnvironment(so_5::coop_t &coop,
const so_5::mbox_t &listener,
const so_5::mbox_t &/*stats*/) {
unsigned int botThreads = config[BOT]["threads"].value_or(1);
auto botsLogger = LoggerFactory::create(LoggerFactory::config(config, BOT));
auto botsDisp = so_5::disp::active_obj::make_dispatcher(so_environment(), "bots_environment");
return coop.make_agent_with_binder<BotsEnvironment>(botsDisp.binder(),
listener, http, botThreads, db, botsLogger);
}
MessageProcessor *Controller::makeMessageProcessor(so_5::coop_t &coop,
const so_5::mbox_t &publisher,
const so_5::mbox_t &/*stats*/) {
MessageProcessorConfig procCfg;
procCfg.languageRecognition = config[MSG]["language_recognition"].value_or(false);
unsigned int procThreads = config[MSG]["threads"].value_or(2);
auto procPool = so_5::disp::adv_thread_pool::make_dispatcher(so_environment(), "message_processor", procThreads);
auto procPoolParams = so_5::disp::adv_thread_pool::bind_params_t{};
return coop.make_agent_with_binder<MessageProcessor>(procPool.binder(procPoolParams),
publisher, std::move(procCfg), this->logger);
}
IRCController *Controller::makeIRCController(so_5::coop_t &coop, const so_5::mbox_t &stats) {
IRCConnectionConfig ircConfig;
ircConfig.host = config[IRC]["host"].value_or("irc.chat.twitch.tv");
ircConfig.port = config[IRC]["port"].value_or(6667);
ircConfig.threads = config[IRC]["threads"].value_or(1);
auto ircLogger = LoggerFactory::create(LoggerFactory::config(config, IRC));
auto ircDisp = so_5::disp::active_obj::make_dispatcher(so_environment(), "irc_controller");
return coop.make_agent_with_binder<IRCController>(ircDisp.binder(),
msgProcessor->so_direct_mbox(), stats,
http, ircConfig, db, ircLogger);
}