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)
34 :
CContinuousWorker(owner,
"Background data updater"), m_updateTimer(this,
"Background data updater")
36 connect(&m_updateTimer, &misc::CThreadedTimer::timeout,
this, &CBackgroundDataUpdater::doWork);
40 &CBackgroundDataUpdater::onModelsPublished, Qt::QueuedConnection);
46 QReadLocker l(&m_lockMsg);
47 return m_messageHistory;
52 Q_ASSERT_X(this->
hasStarted(), Q_FUNC_INFO,
"Worker not started yet");
56 void CBackgroundDataUpdater::doWork()
58 if (!this->doWorkCheck()) {
return; }
59 if (m_inWork) {
return; }
64 this->addHistory(
CLogMessage(
this).info(u
"Background consolidation cycle %1") << cycle);
71 this->addHistory(
CLogMessage(
this).info(u
"Triggered info reads from DB"));
72 this->triggerInfoReads();
75 this->addHistory(
CLogMessage(
this).info(u
"Synchronize DB entities"));
76 this->syncDbEntity(CEntityFlags::AirlineIcaoEntity);
77 this->syncDbEntity(CEntityFlags::LiveryEntity);
78 this->syncDbEntity(CEntityFlags::ModelEntity);
79 this->syncDbEntity(CEntityFlags::DistributorEntity);
80 this->syncDbEntity(CEntityFlags::AircraftIcaoEntity);
83 this->addHistory(
CLogMessage(
this).info(u
"Synchronize %1") << this->modelCaches(
true).getDescription());
84 this->syncModelOrModelSetCacheWithDbData(
true);
87 this->addHistory(
CLogMessage(
this).info(u
"Synchronize %1") << this->modelCaches(
false).getDescription());
88 this->syncModelOrModelSetCacheWithDbData(
false);
99 void CBackgroundDataUpdater::triggerInfoReads()
101 if (!this->doWorkCheck()) {
return; }
106 void CBackgroundDataUpdater::syncModelOrModelSetCacheWithDbData(
bool modelSetFlag,
109 if (!this->doWorkCheck()) {
return; }
111 const QDateTime latestDbModelsTs =
114 if (!latestDbModelsTs.isValid()) {
return; }
117 const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(modelCaches.
getDescription());
118 if (dbModelsLatestSync.isValid() && latestDbModelsTs <= dbModelsLatestSync) {
return; }
121 m_syncedModelsLatestChange[description] = latestDbModelsTs;
127 if (dbModels.
isEmpty()) {
return; }
132 if (!this->doWorkCheck()) {
return; }
134 if (simulatorModels.
isEmpty()) {
continue; }
137 if (dbModelsForSimulator.
isEmpty()) {
continue; }
144 const qint64 msElapsed = time.elapsed();
145 this->addHistory(
CLogMessage(
this).info(u
"Consolidated %1 models (%2) for '%3' in %4ms")
146 << c << description << singleSimulator.convertToQString() << msElapsed);
152 this->addHistory(
CLogMessage(
this).info(u
"Synchronized, no changes for '%1'")
153 << singleSimulator.convertToQString());
156 if (simulatorsSet.size() > 1)
159 if (!this->doWorkCheck()) {
return; }
161 eventLoop.exec(1000);
166 void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity)
168 if (!this->doWorkCheck()) {
return; }
170 if (!latestCacheTs.isValid()) {
return; }
173 const QString entityStr = CEntityFlags::entitiesToString(entity);
174 const QString latestCacheTsStr = latestCacheTs.toString(Qt::ISODate);
176 if (!latestDbTs.isValid()) {
return; }
177 if (latestDbTs <= latestCacheTs)
181 u
"Background updater (%1), no auto synchronization with DB, entity '%2', DB ts: %3 cache ts: %4")
183 << latestCacheTsStr);
187 this->addHistory(
CLogMessage(
this).info(u
"Background updater (%1) triggering read of '%2' since '%3'")
192 bool CBackgroundDataUpdater::doWorkCheck()
const
195 if (!this->
isEnabled()) {
return false; }
199 void CBackgroundDataUpdater::onModelsPublished(
const CAircraftModelList &modelsPublished,
bool directWrite)
201 if (!this->doWorkCheck()) {
return; }
202 if (modelsPublished.
isEmpty()) {
return; }
203 if (!m_updatePublishedModels) {
return; }
204 if (!directWrite) {
return; }
205 if (m_inWork) {
return; }
208 this->syncModelOrModelSetCacheWithDbData(
true, modelsPublished);
209 this->syncModelOrModelSetCacheWithDbData(
false, modelsPublished);
215 if (modelSetFlag) {
return m_modelSets; }
216 return m_modelCaches;
219 void CBackgroundDataUpdater::addHistory(
const CStatusMessage &msg)
221 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.
void startUpdating(std::chrono::milliseconds ms)
Start the updating timer.
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)?
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.
void startTimer(std::chrono::milliseconds ms)
Start updating (start timer)
bool hasStarted() const
True if the worker has started.
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.