xmreg::MysqlPing functor added and its tests as well

pull/93/merge
moneroexamples 6 years ago
parent ca7bef08a7
commit 122e52e724

@ -2,6 +2,7 @@
#include "src/MicroCore.h"
#include "src/YourMoneroRequests.h"
#include "src/ThreadRAII.h"
#include "src/MysqlPing.h"
#include <iostream>
#include <memory>
@ -219,23 +220,10 @@ catch(std::exception const& e)
// something periodically even while the rest of the program has nothing to do."
// from: https://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#connopts
//
auto conn = mysql_accounts->get_connection();
xmreg::ThreadRAII mysql_ping_thread(
std::thread([&conn]
{
while (true)
{
std::this_thread::sleep_for(chrono::seconds(7200)); // 2 hours
if (!conn->ping())
{
cerr << "Pinging mysql failed. stoping mysql pinging thread. \n";
break;
}
cout << "Mysql ping successful. \n" ;
}
}), xmreg::ThreadRAII::DtorAction::detach);
std::thread(xmreg::MysqlPing {mysql_accounts->get_connection()}),
xmreg::ThreadRAII::DtorAction::detach);
// create REST JSON API services
xmreg::YourMoneroRequests open_monero(mysql_accounts, current_bc_status);

@ -16,7 +16,8 @@ set(SOURCE_FILES
OutputInputIdentification.cpp
version.h.in
BlockchainSetup.cpp
ThreadRAII.cpp)
ThreadRAII.cpp
MysqlPing.cpp)
# make static library called libmyxrm
# that we are going to link to

@ -219,12 +219,14 @@ public:
try
{
conn->check_if_connected();
StoreQueryResult sr = query.store();
if (!sr.empty())
return sr[0][0];
}
catch (std::exception& e)
catch (std::exception const& e)
{
MYSQL_EXCEPTION_MSG(e);
}

@ -0,0 +1,46 @@
//
// Created by mwo on 12/07/18.
//
#include "MysqlPing.h"
namespace xmreg
{
MysqlPing::MysqlPing(
std::shared_ptr<MySqlConnector> _conn,
uint64_t _ping_time)
: conn {_conn}, ping_time {_ping_time}
{}
void
MysqlPing::operator()()
{
while (keep_looping)
{
std::this_thread::sleep_for(chrono::seconds(ping_time));
if (auto c = conn.lock())
{
if (!c->ping())
{
cerr << "Pinging mysql failed. Stoping mysql pinging thread. \n";
why_stoped = StopReason::PingFailed;
break;
}
cout << "Mysql ping successful. \n" ;
}
else
{
cerr << "std::weak_ptr<MySqlConnector> conn expired! \n";
why_stoped = StopReason::PointerExpired;
break;
}
++counter;
}
}
}

@ -0,0 +1,40 @@
//
// Created by mwo on 12/07/18.
//
#ifndef OPENMONERO_MYSQLPING_H
#define OPENMONERO_MYSQLPING_H
#include "MySqlConnector.h"
#include <memory>
#include <thread>
#include <atomic>
namespace xmreg
{
class MysqlPing
{
public:
enum class StopReason {NotYetStopped, PingFailed, PointerExpired};
MysqlPing(std::shared_ptr<MySqlConnector> _conn, uint64_t _ping_time = 7200);
void operator()();
void stop() {keep_looping = false;}
uint64_t get_counter() const {return counter;}
StopReason get_stop_reason() const {return why_stoped;};
private:
std::weak_ptr<MySqlConnector> conn;
uint64_t ping_time; // in seconds
atomic<bool> keep_looping {true};
atomic<uint64_t> counter {0};
atomic<StopReason> why_stoped {StopReason::NotYetStopped};
};
}
#endif //OPENMONERO_MYSQLPING_H

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save