mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-17 19:05:31 +08:00
Ref T149, utility functions for pending URLs
* Do not call read twice in info reader * Threadsafe utility functions
This commit is contained in:
committed by
Mathew Sutcliffe
parent
52caf795d1
commit
0cf308450e
@@ -113,7 +113,15 @@ namespace BlackCore
|
|||||||
void CInfoDataReader::read()
|
void CInfoDataReader::read()
|
||||||
{
|
{
|
||||||
if (!this->doWorkCheck()) { return; }
|
if (!this->doWorkCheck()) { return; }
|
||||||
|
|
||||||
const CUrl url(this->getInfoObjectsUrl());
|
const CUrl url(this->getInfoObjectsUrl());
|
||||||
|
const CUrlLogList urlLogList(this->getUrlLogList()); // thread safe copy
|
||||||
|
|
||||||
|
if (urlLogList.hasPending())
|
||||||
|
{
|
||||||
|
CLogMessage(this).info("Info data reading still pending, summary: '%1'") << urlLogList.getSummary();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!url.isEmpty())
|
if (!url.isEmpty())
|
||||||
{
|
{
|
||||||
this->getFromNetworkAndLog(url, { this, &CInfoDataReader::parseInfoObjectsData});
|
this->getFromNetworkAndLog(url, { this, &CInfoDataReader::parseInfoObjectsData});
|
||||||
|
|||||||
@@ -89,6 +89,18 @@ namespace BlackCore
|
|||||||
QTimer::singleShot(0, &m_updateTimer, &QTimer::stop);
|
QTimer::singleShot(0, &m_updateTimer, &QTimer::stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CThreadedReader::hasPendingUrls() const
|
||||||
|
{
|
||||||
|
QReadLocker l(&m_lock);
|
||||||
|
return m_urlReadLog.hasPending();
|
||||||
|
}
|
||||||
|
|
||||||
|
CUrlLogList CThreadedReader::getUrlLogList() const
|
||||||
|
{
|
||||||
|
QReadLocker l(&m_lock);
|
||||||
|
return m_urlReadLog;
|
||||||
|
}
|
||||||
|
|
||||||
bool CThreadedReader::didContentChange(const QString &content, int startPosition)
|
bool CThreadedReader::didContentChange(const QString &content, int startPosition)
|
||||||
{
|
{
|
||||||
uint oldHash = 0;
|
uint oldHash = 0;
|
||||||
@@ -162,7 +174,16 @@ namespace BlackCore
|
|||||||
QNetworkReply *CThreadedReader::getFromNetworkAndLog(const CUrl &url, const BlackMisc::CSlot<void (QNetworkReply *)> &callback)
|
QNetworkReply *CThreadedReader::getFromNetworkAndLog(const CUrl &url, const BlackMisc::CSlot<void (QNetworkReply *)> &callback)
|
||||||
{
|
{
|
||||||
// returned QNetworkReply normally nullptr since QAM is in different thread
|
// returned QNetworkReply normally nullptr since QAM is in different thread
|
||||||
|
QWriteLocker wl(&m_lock);
|
||||||
|
const CUrlLogList outdatedPendingUrls = m_urlReadLog.findOutdatedPending(OutdatedPendingCallMs);
|
||||||
|
if (!outdatedPendingUrls.isEmpty())
|
||||||
|
{
|
||||||
|
CLogMessage(this).warning("Detected outdated pending calls: '%1'") << outdatedPendingUrls.toQString(true);
|
||||||
|
m_urlReadLog.removeOlderThanNowMinusOffset(OutdatedPendingCallMs); // clean up
|
||||||
|
}
|
||||||
|
|
||||||
const int id = m_urlReadLog.addPendingUrl(url);
|
const int id = m_urlReadLog.addPendingUrl(url);
|
||||||
|
wl.unlock();
|
||||||
return sApp->getFromNetwork(url, id, callback);
|
return sApp->getFromNetwork(url, id, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,6 +83,14 @@ namespace BlackCore
|
|||||||
//! \remark needs to be done before started in different thread
|
//! \remark needs to be done before started in different thread
|
||||||
void markAsUsedInUnitTest() { m_unitTest = true; }
|
void markAsUsedInUnitTest() { m_unitTest = true; }
|
||||||
|
|
||||||
|
//! Has pending URLs?
|
||||||
|
//! \threadsafe
|
||||||
|
bool hasPendingUrls() const;
|
||||||
|
|
||||||
|
//! Get the URL log list
|
||||||
|
//! \threadsafe
|
||||||
|
BlackMisc::Network::CUrlLogList getUrlLogList() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
mutable QReadWriteLock m_lock { QReadWriteLock::Recursive }; //!< lock which can be used from the derived classes
|
mutable QReadWriteLock m_lock { QReadWriteLock::Recursive }; //!< lock which can be used from the derived classes
|
||||||
|
|
||||||
@@ -106,9 +114,11 @@ namespace BlackCore
|
|||||||
virtual void doWorkImpl() {}
|
virtual void doWorkImpl() {}
|
||||||
|
|
||||||
//! Still enabled etc.?
|
//! Still enabled etc.?
|
||||||
|
//! \threadsafe under normal conditions
|
||||||
bool doWorkCheck() const;
|
bool doWorkCheck() const;
|
||||||
|
|
||||||
//! Get request from network, and log with m_urlReadLog
|
//! Get request from network, and log with m_urlReadLog
|
||||||
|
//! \threadsafe read log access is thread safe
|
||||||
QNetworkReply *getFromNetworkAndLog(const BlackMisc::Network::CUrl &url, const BlackMisc::CSlot<void(QNetworkReply *)> &callback);
|
QNetworkReply *getFromNetworkAndLog(const BlackMisc::Network::CUrl &url, const BlackMisc::CSlot<void(QNetworkReply *)> &callback);
|
||||||
|
|
||||||
//! Network reply received, mark in m_urlReadLog
|
//! Network reply received, mark in m_urlReadLog
|
||||||
@@ -123,6 +133,8 @@ namespace BlackCore
|
|||||||
//! Trigger doWorkImpl
|
//! Trigger doWorkImpl
|
||||||
void doWork();
|
void doWork();
|
||||||
|
|
||||||
|
static constexpr int OutdatedPendingCallMs = 30 * 1000; //!< when is a call considered "outdated"
|
||||||
|
|
||||||
int m_initialTime = -1; //!< Initial start delay
|
int m_initialTime = -1; //!< Initial start delay
|
||||||
int m_periodicTime = -1; //!< Periodic time after which the task is repeated
|
int m_periodicTime = -1; //!< Periodic time after which the task is repeated
|
||||||
QDateTime m_updateTimestamp; //!< when file/resource was read
|
QDateTime m_updateTimestamp; //!< when file/resource was read
|
||||||
|
|||||||
@@ -41,6 +41,12 @@ namespace BlackMisc
|
|||||||
return this->findBy(&CUrlLog::isPending, true);
|
return this->findBy(&CUrlLog::isPending, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CUrlLogList CUrlLogList::findOutdatedPending(int outdatedOffsetMs) const
|
||||||
|
{
|
||||||
|
if (this->isEmpty()) { return CUrlLogList(); }
|
||||||
|
return this->findPending().findBeforeNowMinusOffset(outdatedOffsetMs);
|
||||||
|
}
|
||||||
|
|
||||||
CUrlLogList CUrlLogList::findErrors() const
|
CUrlLogList CUrlLogList::findErrors() const
|
||||||
{
|
{
|
||||||
return this->findBy(&CUrlLog::isPending, false, &CUrlLog::isSuccess, false);
|
return this->findBy(&CUrlLog::isPending, false, &CUrlLog::isSuccess, false);
|
||||||
|
|||||||
@@ -48,6 +48,9 @@ namespace BlackMisc
|
|||||||
//! Find pending log entries
|
//! Find pending log entries
|
||||||
CUrlLogList findPending() const;
|
CUrlLogList findPending() const;
|
||||||
|
|
||||||
|
//! Find outdated pending log entries
|
||||||
|
CUrlLogList findOutdatedPending(int outdatedOffsetMs) const;
|
||||||
|
|
||||||
//! Find log entries with errors (not pending)
|
//! Find log entries with errors (not pending)
|
||||||
CUrlLogList findErrors() const;
|
CUrlLogList findErrors() const;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user