Ref T118, fixed / improved watchdog

* deleteLater for reply
* allow to check via HTTP (avoid wrong positive checks)
This commit is contained in:
Klaus Basan
2017-10-25 19:19:06 +02:00
parent 4166982954
commit 8473cd7eb4
2 changed files with 34 additions and 9 deletions

View File

@@ -42,7 +42,7 @@ namespace BlackCore
{
m_dbAccessible = accessible;
m_internetAccessible = m_internetAccessible && m_networkAccessible;
QTimer::singleShot(0, &m_updateTimer, [this] { this->m_updateTimer.start(); }); // restart
QTimer::singleShot(0, &m_updateTimer, [this] { m_updateTimer.start(); }); // restart
}
bool CNetworkWatchdog::hasWorkingSharedUrl() const
@@ -91,8 +91,25 @@ namespace BlackCore
const bool wasDbAvailable = m_dbAccessible;
const bool wasInternetAvailable = m_internetAccessible;
const bool networkAccess = m_networkAccessible;
const bool canConnectDb = m_checkDbAccessibility && networkAccess &&
CNetworkUtils::canConnect(dbTestUrl()); // running in background worker
bool canConnectDb = m_checkDbAccessibility && networkAccess &&
CNetworkUtils::canConnect(dbTestUrl()); // running here in background worker
if (m_checkDbAccessibility && m_doDetailedCheck && canConnectDb)
{
// test against real HTTP response
const bool lastHttpSuccess = m_lastClientPingSuccess;
if (lastHttpSuccess && m_checkCount % 10 == 0)
{
// seems to be OK, from time to time ping
this->pingDbClientService(PingStarted);
}
else if (!lastHttpSuccess && m_checkCount % 3 == 0)
{
// not OK, retry more frequently
this->pingDbClientService(PingStarted, true);
}
canConnectDb = lastHttpSuccess;
}
bool canConnectInternet = canConnectDb;
bool checkInternetAccess = !canConnectDb;
@@ -140,12 +157,12 @@ namespace BlackCore
m_internetAccessible = networkAccess && canConnectInternet;
// signals
m_checkCount++;
this->triggerChangedSignals(wasDbAvailable, wasInternetAvailable);
}
while (false);
m_updateTimer.start(); // restart
m_checkCount++;
m_checkInProgress = false;
}
@@ -182,9 +199,9 @@ namespace BlackCore
this->pingDbClientService(PingCompleteShutdown);
}
void CNetworkWatchdog::pingDbClientService(CNetworkWatchdog::PingType type)
void CNetworkWatchdog::pingDbClientService(CNetworkWatchdog::PingType type, bool force)
{
if (!this->isSwiftDbAccessible()) { return; }
if (!force && !this->isSwiftDbAccessible()) { return; }
if (!sApp) { return; }
const CGlobalSetup gs = sApp->getGlobalSetup();
if (!gs.wasLoaded()) { return; }
@@ -202,9 +219,11 @@ namespace BlackCore
void CNetworkWatchdog::replyPingClientService(QNetworkReply *nwReply)
{
QScopedPointer<QNetworkReply> nw(nwReply); // delete reply
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nw(nwReply); // delete reply
const bool ok = (nw->error() == QNetworkReply::NoError);
nw->close();
if (!sApp || sApp->isShuttingDown()) { return; }
const bool ok = nw->error() == QNetworkReply::NoError;
m_lastClientPingSuccess = ok;
this->setDbAccessibility(ok);
}

View File

@@ -58,6 +58,10 @@ namespace BlackCore
//! \threadsafe
void setCheckSharedUrl(bool check) { m_checkSharedUrl = check; }
//! Do a detailed check via HTTP
//! \threadsafe
void setDoDetailedCheck(bool check) { m_doDetailedCheck = check; }
//! Internet available?
//! \threadsafe
bool isInternetAccessible() const { return m_internetAccessible; }
@@ -98,7 +102,7 @@ namespace BlackCore
Q_DECLARE_FLAGS(PingType, PingTypeFlag)
//! Ping the DB server, fire and forget (no feedback etc)
void pingDbClientService(PingType type = PingUnspecific);
void pingDbClientService(PingType type = PingUnspecific, bool force = false);
//! URL referring to the DB
//! \remark depends on BlackCore::Application::getGlobalSetup()
@@ -139,9 +143,11 @@ namespace BlackCore
//! \remark depends on BlackCore::Application::getGlobalSetup()
static BlackMisc::Network::CUrl workingSharedUrlFromSetup();
std::atomic_bool m_doDetailedCheck { true };
std::atomic_bool m_networkAccessible { true };
std::atomic_bool m_internetAccessible { true };
std::atomic_bool m_dbAccessible { true };
std::atomic_bool m_lastClientPingSuccess { true };
std::atomic_bool m_checkDbAccessibility { true };
std::atomic_bool m_checkSharedUrl { true };
std::atomic_bool m_checkInProgress { false }; //!< a check is currently in progress