mysql ping while loop thitend

pull/126/head
moneroexamples 5 years ago
parent 7f78ac3d0a
commit 32e4173591

@ -52,11 +52,11 @@
"viewkey" : "53c5850e895122574c53a4f952c726be3fe22bcd2b08f4bfed8946d887cc950b"
}
},
"refresh_block_status_every_seconds" : 5,
"refresh_block_status_every_seconds" : 10,
"blocks_search_lookahead" : 200,
"search_thread_life_in_seconds" : 120,
"max_number_of_blocks_to_import" : 132000,
"mysql_ping_every_seconds" : 5,
"mysql_ping_every_seconds" : 300,
"ssl" :
{
"enable" : false,

@ -26,7 +26,7 @@ public:
{
std::lock_guard<std::mutex> lk(m);
s_shouldExit = true;
OMINFO << "Request to finish the openmonero recieved";
OMINFO << "Request to finish the openmonero received";
cv.notify_one();
}
@ -40,6 +40,8 @@ bool ExitHandler::s_shouldExit {false};
std::mutex ExitHandler::m;
std::condition_variable ExitHandler::cv;
int
main(int ac, const char* av[])
{
@ -55,11 +57,20 @@ if (*help_opt)
return EXIT_SUCCESS;
}
auto monero_log_level =
*(opts.get_option<size_t>("monero-log-level"));
if (monero_log_level < 1 || monero_log_level > 4)
{
cerr << "monero-log-level,m option must be between 1 and 4!\n";
return EXIT_SUCCESS;
}
// setup monero logger
mlog_configure(mlog_get_default_log_path(""), true);
mlog_set_log("1");
mlog_set_log(std::to_string(monero_log_level).c_str());
string log_file = *(opts.get_option<string>("log-file"));
auto log_file = *(opts.get_option<string>("log-file"));
// setup a logger for Open Monero
@ -113,7 +124,7 @@ nlohmann::json config_json = bc_setup.get_config();
//cast port number in string to uint16
uint16_t app_port = boost::lexical_cast<uint16_t>(*port_opt);
auto app_port = boost::lexical_cast<uint16_t>(*port_opt);
// set mysql/mariadb connection details
xmreg::MySqlConnector::url = config_json["database"]["url"];
@ -270,8 +281,8 @@ else
OMINFO << "Start the service at http://127.0.0.1:" << app_port;
}
//ExitHandler exitHandler(service);
// intercept basic termination requests,
// including Ctrl+c
ExitHandler exitHandler;
signal(SIGABRT, ExitHandler::exitHandler);
@ -279,7 +290,6 @@ signal(SIGTERM, ExitHandler::exitHandler);
signal(SIGINT, ExitHandler::exitHandler);
// main restbed thread. this is where
// restbed will be running and handling
// requests
@ -291,7 +301,7 @@ std::thread restbed_service(
});
// we are going to loop here for as long
// we are going to whait here for as long
// as control+c hasn't been pressed
{
std::unique_lock<std::mutex> lk(ExitHandler::m);
@ -301,18 +311,18 @@ std::thread restbed_service(
//////////////////////////////////////////////
// Try to greacfully stop all threads/services
// Try to gracefully stop all threads/services
//////////////////////////////////////////////
OMINFO << "Stopping restbed service.";
service.stop();
restbed_service.join();
OMINFO << "Stoppping blockchain_monitoring_thread. Please wait.";
OMINFO << "Stopping blockchain_monitoring_thread. Please wait.";
current_bc_status->stop();
blockchain_monitoring_thread.join();
OMINFO << "Stoppping mysql_ping. Please wait.";
OMINFO << "Stopping mysql_ping. Please wait.";
mysql_ping.stop();
mysql_ping_thread.join();

@ -37,6 +37,9 @@ namespace xmreg
("config-file,c", value<string>()
->default_value("./config/config.json"),
"Config file path.")
("monero-log-level,m", value<size_t>()
->default_value(1),
"Monero log level 1-4, default is 1.")
("log-file,l", value<string>()
->default_value("./openmonero.log"),
"Name and path to log file. -l \"\" to disable log file.");

@ -7,22 +7,49 @@
#include "MysqlPing.h"
#include <algorithm>
namespace xmreg
{
MysqlPing::MysqlPing(
std::shared_ptr<MySqlConnector> _conn,
seconds _ping_time)
: conn {_conn}, ping_time {_ping_time}
seconds _ping_time, seconds _sleep_time)
: conn {_conn}, ping_time {_ping_time},
thread_sleep_time {_sleep_time}
{}
void
MysqlPing::operator()()
{
// the while loop below is going to execute
// faster than ping_time specified. The reason
// is so that we can exit it in a timely manner
// when keep_looping becomes false
uint64_t between_ping_counter {0};
// we are going to ping mysql every
// no_of_loops_between_pings iterations
// of the while loop
uint64_t no_of_loops_between_pings
= std::max<uint64_t>(1,
ping_time.count()/thread_sleep_time.count());
//cout << "no_of_loops_between_pings: "
// << no_of_loops_between_pings << endl;
while (keep_looping)
{
std::this_thread::sleep_for(chrono::seconds(ping_time));
std::this_thread::sleep_for(thread_sleep_time);
if (between_ping_counter++ < no_of_loops_between_pings)
{
continue;
}
between_ping_counter = 0;
if (auto c = conn.lock())
{

@ -24,7 +24,8 @@ public:
enum class StopReason {NotYetStopped, PingFailed, PointerExpired};
MysqlPing(std::shared_ptr<MySqlConnector> _conn,
seconds _ping_time = 300s);
seconds _ping_time = 300s,
seconds _sleep_time = 5s);
void operator()();
void stop() {keep_looping = false;}
@ -35,9 +36,12 @@ public:
MysqlPing(MysqlPing&&) = default;
MysqlPing& operator=(MysqlPing&&) = default;
~MysqlPing() = default;
private:
std::weak_ptr<MySqlConnector> conn;
seconds ping_time; // in seconds
seconds ping_time {300};
seconds thread_sleep_time {5};
atomic<bool> keep_looping {true};
atomic<uint64_t> counter {0};
atomic<StopReason> why_stoped {StopReason::NotYetStopped};

Loading…
Cancel
Save