6 #include <QElapsedTimer>
18 using namespace swift::misc::network;
19 using namespace swift::misc::simulation;
20 using namespace swift::misc::simulation::data;
26 const QStringList &CBackgroundDataUpdater::getLogCategories()
28 static const QStringList cats(
33 CBackgroundDataUpdater::CBackgroundDataUpdater(QObject *owner) :
CContinuousWorker(owner,
"Background data updater")
35 connect(&
m_updateTimer, &QTimer::timeout,
this, &CBackgroundDataUpdater::doWork);
40 &CBackgroundDataUpdater::onModelsPublished, Qt::QueuedConnection);
46 QReadLocker l(&m_lockMsg);
47 return m_messageHistory;
50 void CBackgroundDataUpdater::doWork()
52 if (!this->doWorkCheck()) {
return; }
53 if (m_inWork) {
return; }
58 this->addHistory(
CLogMessage(
this).info(u
"Background consolidation cycle %1") << cycle);
65 this->addHistory(
CLogMessage(
this).info(u
"Triggered info reads from DB"));
66 this->triggerInfoReads();
69 this->addHistory(
CLogMessage(
this).info(u
"Synchronize DB entities"));
70 this->syncDbEntity(CEntityFlags::AirlineIcaoEntity);
71 this->syncDbEntity(CEntityFlags::LiveryEntity);
72 this->syncDbEntity(CEntityFlags::ModelEntity);
73 this->syncDbEntity(CEntityFlags::DistributorEntity);
74 this->syncDbEntity(CEntityFlags::AircraftIcaoEntity);
77 this->addHistory(
CLogMessage(
this).info(u
"Synchronize %1") << this->modelCaches(
true).getDescription());
78 this->syncModelOrModelSetCacheWithDbData(
true);
81 this->addHistory(
CLogMessage(
this).info(u
"Synchronize %1") << this->modelCaches(
false).getDescription());
82 this->syncModelOrModelSetCacheWithDbData(
false);
93 void CBackgroundDataUpdater::triggerInfoReads()
95 if (!this->doWorkCheck()) {
return; }
100 void CBackgroundDataUpdater::syncModelOrModelSetCacheWithDbData(
bool modelSetFlag,
103 if (!this->doWorkCheck()) {
return; }
105 const QDateTime latestDbModelsTs =
108 if (!latestDbModelsTs.isValid()) {
return; }
111 const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(modelCaches.
getDescription());
112 if (dbModelsLatestSync.isValid() && latestDbModelsTs <= dbModelsLatestSync) {
return; }
115 m_syncedModelsLatestChange[description] = latestDbModelsTs;
121 if (dbModels.
isEmpty()) {
return; }
126 if (!this->doWorkCheck()) {
return; }
128 if (simulatorModels.
isEmpty()) {
continue; }
131 if (dbModelsForSimulator.
isEmpty()) {
continue; }
138 const int msElapsed = time.elapsed();
139 this->addHistory(
CLogMessage(
this).info(u
"Consolidated %1 models (%2) for '%3' in %4ms")
140 << c << description << singleSimulator.convertToQString() << msElapsed);
146 this->addHistory(
CLogMessage(
this).info(u
"Synchronized, no changes for '%1'")
147 << singleSimulator.convertToQString());
150 if (simulatorsSet.size() > 1)
153 if (!this->doWorkCheck()) {
return; }
155 eventLoop.exec(1000);
160 void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity)
162 if (!this->doWorkCheck()) {
return; }
164 if (!latestCacheTs.isValid()) {
return; }
167 const QString entityStr = CEntityFlags::entitiesToString(entity);
168 const QString latestCacheTsStr = latestCacheTs.toString(Qt::ISODate);
170 if (!latestDbTs.isValid()) {
return; }
171 if (latestDbTs <= latestCacheTs)
175 u
"Background updater (%1), no auto synchronization with DB, entity '%2', DB ts: %3 cache ts: %4")
177 << latestCacheTsStr);
181 this->addHistory(
CLogMessage(
this).info(u
"Background updater (%1) triggering read of '%2' since '%3'")
186 bool CBackgroundDataUpdater::doWorkCheck()
const
189 if (!this->
isEnabled()) {
return false; }
193 void CBackgroundDataUpdater::onModelsPublished(
const CAircraftModelList &modelsPublished,
bool directWrite)
195 if (!this->doWorkCheck()) {
return; }
196 if (modelsPublished.
isEmpty()) {
return; }
197 if (!m_updatePublishedModels) {
return; }
198 if (!directWrite) {
return; }
199 if (m_inWork) {
return; }
202 this->syncModelOrModelSetCacheWithDbData(
true, modelsPublished);
203 this->syncModelOrModelSetCacheWithDbData(
false, modelsPublished);
209 if (modelSetFlag) {
return m_modelSets; }
210 return m_modelCaches;
213 void CBackgroundDataUpdater::addHistory(
const CStatusMessage &msg)
215 QWriteLocker l(&m_lockMsg);
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
bool hasWebDataServices() const
Web data services available?
bool isShuttingDown() const
Is application shutting down?
CWebDataServices * getWebDataServices() const
Get the web data services.
db::CDatabaseWriter * getDatabaseWriter() const
DB writer class.
QDateTime getLatestDbEntityTimestamp(swift::misc::network::CEntityFlags::Entity entity) const
Corresponding DB timestamp if applicable.
swift::misc::simulation::CAircraftModelList getModels() const
Models.
QDateTime getCacheTimestamp(swift::misc::network::CEntityFlags::Entity entity) const
Corresponding cache timestamp if applicable.
swift::misc::network::CEntityFlags::Entity triggerLoadingDirectlyFromDb(swift::misc::network::CEntityFlags::Entity whatToRead, const QDateTime &newerThan=QDateTime())
Trigger reload from DB, loads the DB data and bypasses the caches checks and info objects.
void triggerReadOfSharedInfoObjects()
Trigger read of shared info objects.
void triggerReadOfDbInfoObjects()
Trigger read of DB info objects.
void consolidating(bool running)
Consolidation.
swift::misc::CStatusMessageList getMessageHistory() const
The message history.
static int consolidateModelsWithDbData(swift::misc::simulation::CAircraftModelList &models, bool force)
Consolidate models with DB data.
void publishedModelsSimplified(const swift::misc::simulation::CAircraftModelList &modelsPublished, bool directWrite)
Published models, simplified version of publishedModels.
Base class for a long-lived worker object which lives in its own thread.
bool isEnabled() const
Enabled (running)?
QTimer m_updateTimer
timer which can be used by implementing classes
Utility class which blocks until a signal is emitted or timeout reached.
static const QString & worker()
Background task.
static const QString & modelSetCache()
Model set cache.
static const QString & modelCache()
Model cache.
Class for emitting a log message.
static void preformatted(const CStatusMessage &statusMessage)
Sends a verbatim, preformatted message to the log.
void push_frontMaxElements(const T &value, int maxElements)
Insert as first element by keep maxElements.
bool isEmpty() const
Synonym for empty.
Streamable status message, e.g.
Status messages, e.g. from Core -> GUI.
static QString currentThreadInfo()
Info about current thread, for debug messages.
QDateTime latestTimestamp() const
Latest timestamp.
Value object encapsulating a list of aircraft models.
CAircraftModelList matchesSimulator(const CSimulatorInfo &simulator) const
Find for given simulator.
Simple hardcoded info about the corresponding simulator.
QSet< CSimulatorInfo > asSingleSimulatorSet() const
As a set of single simulator info objects.
bool isNoSimulator() const
No simulator?
Cache for multiple simulators specified by CSimulatorInfo.
CSimulatorInfo simulatorsWithInitializedCache() const
Initialized caches for which simulator?
virtual QString getDescription() const =0
Descriptive text.
CAircraftModelList getSynchronizedCachedModels(const CSimulatorInfo &simulator)
Models.
virtual CStatusMessage setCachedModels(const CAircraftModelList &models, const CSimulatorInfo &simulator)=0
Set cached models.
Classes interacting with the swift database (aka "datastore").
Backend services of the swift project, like dealing with the network or the simulators.
Free functions in swift::misc.