Ref T292, Ref T285 background updater improvements

* less cycles
* message history
* more detailed status messages
This commit is contained in:
Klaus Basan
2018-07-26 21:43:51 +02:00
parent 5a51d06e33
commit 1fd7ad6891
3 changed files with 65 additions and 28 deletions

View File

@@ -41,10 +41,16 @@ namespace BlackCore
m_updateTimer.setInterval(60 * 1000);
if (sApp && sApp->hasWebDataServices())
{
connect(sApp->getWebDataServices()->getDatabaseWriter(), &CDatabaseWriter::publishedModelsSimplified, this, &CBackgroundDataUpdater::onModelsPublished);
connect(sApp->getWebDataServices()->getDatabaseWriter(), &CDatabaseWriter::publishedModelsSimplified, this, &CBackgroundDataUpdater::onModelsPublished, Qt::QueuedConnection);
}
}
CStatusMessageList CBackgroundDataUpdater::getMessageHistory() const
{
QReadLocker l(&m_lockMsg);
return m_messageHistory;
}
void CBackgroundDataUpdater::doWork()
{
if (!this->doWorkCheck()) { return; }
@@ -57,26 +63,30 @@ namespace BlackCore
case 0:
// normally redundant, will be read in other places as well
// new metadata for next comparison
this->addHistory(CLogMessage(this).info("Triggered info reads from DB"));
this->triggerInfoReads();
break;
case 1:
this->addHistory(CLogMessage(this).info("Synchronize DB entities"));
this->syncDbEntity(CEntityFlags::AirlineIcaoEntity);
this->syncDbEntity(CEntityFlags::LiveryEntity);
this->syncDbEntity(CEntityFlags::ModelEntity);
this->syncDbEntity(CEntityFlags::DistributorEntity);
this->syncDbEntity(CEntityFlags::AircraftIcaoEntity);
break;
case 2:
this->addHistory(CLogMessage(this).info("Synchronize %1") << m_modelCaches.getDescription());
this->syncModelOrModelSetCacheWithDbData(m_modelCaches);
break;
case 3:
this->addHistory(CLogMessage(this).info("Synchronize %1") << m_modelSetCaches.getDescription());
this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches);
break;
case 4:
this->syncDbEntity(CEntityFlags::AircraftIcaoEntity);
this->syncDbEntity(CEntityFlags::AirlineIcaoEntity);
break;
default:
break;
}
++m_cycle %= 5;
++m_cycle %= 4;
m_inWork = false;
emit this->consolidating(false);
}
@@ -96,43 +106,51 @@ namespace BlackCore
dbModelsConsidered.latestTimestamp();
if (!latestDbModelsTs.isValid()) { return; }
// newer DB models as cache
const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(cache.getDescription());
if (dbModelsLatestSync.isValid() && latestDbModelsTs <= dbModelsLatestSync) { return; }
m_syncedModelsLatestChange[cache.getDescription()] = latestDbModelsTs;
const CSimulatorInfo sims = cache.simulatorsWithInitializedCache(); // sims ever used
if (sims.isNoSimulator()) { return; }
const CSimulatorInfo simulators = cache.simulatorsWithInitializedCache(); // simulators ever used
if (simulators.isNoSimulator()) { return; }
const CAircraftModelList dbModels = dbModelsConsidered.isEmpty() ?
sApp->getWebDataServices()->getModels() :
dbModelsConsidered;
if (dbModels.isEmpty()) { return; }
const QSet<CSimulatorInfo> simSet = sims.asSingleSimulatorSet();
for (const CSimulatorInfo &singleInfo : simSet)
const QSet<CSimulatorInfo> simulatorsSet = simulators.asSingleSimulatorSet();
for (const CSimulatorInfo &singleSimulator : simulatorsSet)
{
if (!this->doWorkCheck()) { return; }
CAircraftModelList simModels = cache.getSynchronizedCachedModels(singleInfo);
if (simModels.isEmpty()) { continue; }
const CAircraftModelList dbModelsForSim = dbModels.matchesSimulator(singleInfo);
if (dbModelsForSim.isEmpty()) { continue; }
CAircraftModelList simulatorModels = cache.getSynchronizedCachedModels(singleSimulator);
if (simulatorModels.isEmpty()) { continue; }
const CAircraftModelList dbModelsForSimulator = dbModels.matchesSimulator(singleSimulator);
if (dbModelsForSimulator.isEmpty()) { continue; }
// time consuming part
const int c = CDatabaseUtils::consolidateModelsWithDbData(dbModelsForSim, simModels, true);
const int c = CDatabaseUtils::consolidateModelsWithDbData(dbModelsForSimulator, simulatorModels, true);
if (c > 0)
{
CLogMessage(this).info("Consolidated %1 models for '%2'") << c << singleInfo.convertToQString();
const CStatusMessage m = cache.setCachedModels(simModels, singleInfo);
this->addHistory(CLogMessage(this).info("Consolidated %1 models for '%2'") << c << singleSimulator.convertToQString());
const CStatusMessage m = cache.setCachedModels(simulatorModels, singleSimulator);
CLogMessage::preformatted(m);
this->addHistory(m);
}
else
{
CLogMessage(this).info("Synchronized, no changes for '%1'") << singleInfo.convertToQString();
this->addHistory(CLogMessage(this).info("Synchronized, no changes for '%1'") << singleSimulator.convertToQString());
}
if (simulatorsSet.size() > 1)
{
// just give the system some time to relax, consolidation is time consuming
if (!this->doWorkCheck()) { return; }
CEventLoop::processEventsFor(1000);
}
if (simSet.size() > 1) { CEventLoop::processEventsFor(5000); } // just give the system some time to relax, consolidation is time consuming
}
}
void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity) const
void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity)
{
if (!this->doWorkCheck()) { return; }
const QDateTime latestCacheTs = sApp->getWebDataServices()->getCacheTimestamp(entity);
@@ -141,18 +159,18 @@ namespace BlackCore
if (!latestDbTs.isValid()) { return; }
if (latestDbTs <= latestCacheTs)
{
CLogMessage(this).info("No auto sync with DB, entity '%1', DB ts: %2 cache ts: %3") << CEntityFlags::flagToString(entity) << latestDbTs.toString(Qt::ISODate) << latestCacheTs.toString(Qt::ISODate);
this->addHistory(CLogMessage(this).info("No auto sync with DB, entity '%1', DB ts: %2 cache ts: %3") << CEntityFlags::flagToString(entity) << latestDbTs.toString(Qt::ISODate) << latestCacheTs.toString(Qt::ISODate));
return;
}
CLogMessage(this).info("Triggering read of '%1' since '%2'") << CEntityFlags::flagToString(entity) << latestCacheTs.toString(Qt::ISODate);
this->addHistory(CLogMessage(this).info("Triggering read of '%1' since '%2'") << CEntityFlags::flagToString(entity) << latestCacheTs.toString(Qt::ISODate));
sApp->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::ModelEntity, latestCacheTs);
}
bool CBackgroundDataUpdater::doWorkCheck() const
{
if (!sApp || sApp->isShuttingDown() || !sApp->hasWebDataServices()) { return false; }
if (!isEnabled()) { return false; }
if (!this->isEnabled()) { return false; }
return true;
}
@@ -168,5 +186,11 @@ namespace BlackCore
this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches, modelsPublished);
emit this->consolidating(false);
}
void CBackgroundDataUpdater::addHistory(const CStatusMessage &msg)
{
QWriteLocker l(&m_lockMsg);
m_messageHistory.push_frontMaxElements(msg, 100); // latest
}
} // ns
} // ns

View File

@@ -16,8 +16,10 @@
#include "blackcore/blackcoreexport.h"
#include "blackmisc/simulation/data/modelcaches.h"
#include "blackmisc/network/entityflags.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/worker.h"
#include <QMap>
#include <QReadWriteLock>
namespace BlackCore
{
@@ -35,6 +37,10 @@ namespace BlackCore
//! Constructor
CBackgroundDataUpdater(QObject *owner);
//! The message history
//! \threadsafe
BlackMisc::CStatusMessageList getMessageHistory() const;
signals:
//! Consolidation
void consolidating(bool running);
@@ -43,10 +49,12 @@ namespace BlackCore
int m_cycle = 0; //!< cycle
bool m_inWork = false; //!< indicates a running update
bool m_updatePublishedModels = true; //!< update when models have been updated
mutable QReadWriteLock m_lockMsg; //!< lock snapshot
BlackMisc::Simulation::Data::CModelCaches m_modelCaches { false, this }; //!< caches
BlackMisc::Simulation::Data::CModelCaches m_modelCaches { false, this }; //!< caches
BlackMisc::Simulation::Data::CModelSetCaches m_modelSetCaches { false, this }; //!< caches
QMap<QString, QDateTime> m_syncedModelsLatestChange; //! timestamp per cache
QMap<QString, QDateTime> m_syncedModelsLatestChange; //! timestamp per cache when last synced
BlackMisc::CStatusMessageList m_messageHistory;
//! Do the update checks
void doWork();
@@ -55,17 +63,22 @@ namespace BlackCore
void triggerInfoReads();
//! Sync the model cache, normally model set or simulator models cache
//! \remark if no DB models are passed all DB models are used
void syncModelOrModelSetCacheWithDbData(BlackMisc::Simulation::Data::IMultiSimulatorModelCaches &cache,
const BlackMisc::Simulation::CAircraftModelList &dbModelsConsidered = {});
//! Sync DB entity
void syncDbEntity(BlackMisc::Network::CEntityFlags::Entity entity) const;
void syncDbEntity(BlackMisc::Network::CEntityFlags::Entity entity);
//! Still enabled etc.
bool doWorkCheck() const;
//! Models have been published
void onModelsPublished(const BlackMisc::Simulation::CAircraftModelList &modelsPublished, bool directWrite);
//! Add history message
//! \threadsafe
void addHistory(const BlackMisc::CStatusMessage &msg);
};
} // ns
} // ns

View File

@@ -96,10 +96,10 @@ namespace BlackGui
static bool isValid(const int &valueInSeconds) { return valueInSeconds == -1 || (valueInSeconds >= minSecs() && valueInSeconds <= maxSecs()); }
//! \copydoc BlackCore::TSettingTrait::defaultValue
static const int &defaultValue() { static const int i = 240; return i; }
static const int &defaultValue() { static const int i = 60; return i; }
//! Minimum
static int minSecs() { return 60; }
static int minSecs() { return 30; }
//! Maximum
static int maxSecs() { return 3600; }