Ref T105, modified background updater

* removed gracefulShutdown
* removed isShuttingDown
This commit is contained in:
Klaus Basan
2017-07-09 01:26:08 +02:00
committed by Mathew Sutcliffe
parent 7f3643fb0e
commit 2fbd2c6382
3 changed files with 19 additions and 72 deletions

View File

@@ -37,6 +37,7 @@ namespace BlackCore
CContinuousWorker(owner, "Background data updater")
{
connect(&m_updateTimer, &QTimer::timeout, this, &CBackgroundDataUpdater::doWork);
m_updateTimer.setObjectName(getName());
}
void CBackgroundDataUpdater::initialize()
@@ -47,42 +48,6 @@ namespace BlackCore
void CBackgroundDataUpdater::cleanup()
{
m_updateTimer.stop();
m_shutdown = true;
m_enabled = false;
}
CBackgroundDataUpdater::~CBackgroundDataUpdater()
{
gracefulShutdown();
}
bool CBackgroundDataUpdater::isShuttingDown() const
{
if (!sApp) { return true; } // sApp object is gone, whole system shutdown
if (this->m_shutdown) { return true; } // marked as shutdown
if (this->isAbandoned()) { return true; } // worker abandoned
return false;
}
bool CBackgroundDataUpdater::isEnabled() const
{
return m_enabled;
}
void CBackgroundDataUpdater::gracefulShutdown()
{
m_shutdown = true;
m_enabled = false;
if (!CThreadUtils::isCurrentThreadObjectThread(this))
{
std::promise<void> promise;
doIfFinishedElse([&promise] { promise.set_value(); }, [&promise, this]
{
this->then([&promise] { promise.set_value(); });
this->abandon();
});
promise.get_future().wait();
}
}
void CBackgroundDataUpdater::startUpdating(int updateTimeSecs)
@@ -94,22 +59,21 @@ namespace BlackCore
return;
}
m_enabled = updateTimeSecs > 0;
if (updateTimeSecs < 0)
{
m_enabled = false;
setEnabled(false);
QTimer::singleShot(0, &m_updateTimer, &QTimer::stop);
}
else
{
m_enabled = true;
setEnabled(true);
m_updateTimer.start(1000 * updateTimeSecs);
}
}
void CBackgroundDataUpdater::doWork()
{
if (!this->entryCheck()) { return; }
if (!this->doWorkCheck()) { return; }
m_inWork = true;
const int cycle = m_cycle;
@@ -143,14 +107,14 @@ namespace BlackCore
void CBackgroundDataUpdater::triggerInfoReads()
{
if (!this->entryCheck()) { return; }
if (!this->doWorkCheck()) { return; }
sApp->getWebDataServices()->triggerReadOfDbInfoObjects();
sApp->getWebDataServices()->triggerReadOfSharedInfoObjects();
}
void CBackgroundDataUpdater::syncModelOrModelSetCacheWithDbData(Simulation::Data::IMultiSimulatorModelCaches &cache)
{
if (!this->entryCheck()) { return; }
if (!this->doWorkCheck()) { return; }
const QDateTime cacheTs = sApp->getWebDataServices()->getCacheTimestamp(CEntityFlags::ModelEntity);
if (!cacheTs.isValid()) { return; }
@@ -169,7 +133,7 @@ namespace BlackCore
const QSet<CSimulatorInfo> simSet = sims.asSingleSimulatorSet();
for (const CSimulatorInfo &singleInfo : simSet)
{
if (this->isShuttingDown()) { return; }
if (!this->doWorkCheck()) { return; }
CAircraftModelList simModels = cache.getSynchronizedCachedModels(singleInfo);
if (simModels.isEmpty()) { continue; }
const CAircraftModelList dbModelsForSim = dbModels.matchesSimulator(singleInfo);
@@ -193,7 +157,7 @@ namespace BlackCore
void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity) const
{
if (!this->entryCheck()) { return; }
if (!this->doWorkCheck()) { return; }
const QDateTime latestCacheTs = sApp->getWebDataServices()->getCacheTimestamp(entity);
if (!latestCacheTs.isValid()) { return; }
const QDateTime latestDbTs = sApp->getWebDataServices()->getLatestDbEntityTimestamp(entity);
@@ -208,11 +172,10 @@ namespace BlackCore
sApp->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::ModelEntity, latestCacheTs);
}
bool CBackgroundDataUpdater::entryCheck() const
bool CBackgroundDataUpdater::doWorkCheck() const
{
if (!sApp || !sApp->hasWebDataServices()) { return false; }
if (isShuttingDown()) { return false; }
if (!m_enabled) { return false; }
if (!isEnabled()) { return false; }
return true;
}
} // ns

View File

@@ -35,36 +35,20 @@ namespace BlackCore
//! Constructor
CBackgroundDataUpdater(QObject *owner);
//! Destructor
virtual ~CBackgroundDataUpdater();
//! Enable updates
void startUpdating(int updateTimeSecs);
protected:
//! \copydoc BlackMisc::CContinuousWorker::initialize
virtual void initialize() override;
//! \copydoc BlackMisc::CContinuousWorker::cleanup
virtual void cleanup() override;
//! Is shutting down?
//! \threadsafe
bool isShuttingDown() const;
//! Enabled (running)?
//! \threadsafe
bool isEnabled() const;
//! Graceful shutdown
//! \threadsafe
void gracefulShutdown();
//! Enable updates
void startUpdating(int updateTimeSecs);
private:
std::atomic<bool> m_shutdown { false }; //!< marker it is shutting down
std::atomic<bool> m_enabled { false }; //!< marker it is enabled
int m_cycle = 0; //!< cycle
bool m_inWork = false; //!< indicates a running update
QTimer m_updateTimer { this };
int m_cycle = 0; //!< cycle
bool m_inWork = false; //!< indicates a running update
QTimer m_updateTimer { this };
BlackMisc::Simulation::Data::CModelCaches m_modelCaches { false, this }; //!< caches
BlackMisc::Simulation::Data::CModelSetCaches m_modelSetCaches { false, this }; //!< caches
@@ -83,7 +67,7 @@ namespace BlackCore
void syncDbEntity(BlackMisc::Network::CEntityFlags::Entity entity) const;
//! Still enabled etc.
bool entryCheck() const;
bool doWorkCheck() const;
};
} // ns
} // ns

View File

@@ -149,7 +149,7 @@ void CSwiftData::performGracefulShutdown()
{
if (this->m_updater)
{
this->m_updater->gracefulShutdown();
this->m_updater->abandonAndWait();
}
}
@@ -161,7 +161,7 @@ void CSwiftData::consolidationSettingChanged()
if (m_updater)
{
ui->comp_MainInfoArea->getDataSettingsComponent()->setBackgroundUpdater(nullptr);
m_updater->gracefulShutdown();
m_updater->abandonAndWait();
m_updater = nullptr;
}
}