From f2c6c3fae295f7470e77fc9b552df0beca35becd Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Mon, 26 Apr 2021 16:58:22 +0200 Subject: [PATCH] Avoid race condition when adding feed When adding a feed and simultaneously starting a feed update, a race condition could happen where the feed update would catch up with the feed adding, and start adding and marking old episodes as new before the original addFeed method would reach them. --- src/fetcher.cpp | 72 ++++++++++++++++++++++++++----------------------- src/fetcher.h | 2 ++ 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/fetcher.cpp b/src/fetcher.cpp index 5417898a..715d1d88 100644 --- a/src/fetcher.cpp +++ b/src/fetcher.cpp @@ -37,6 +37,45 @@ Fetcher::Fetcher() } void Fetcher::fetch(const QString &url) +{ + QStringList urls(url); + fetch(urls); +} + +void Fetcher::fetch(const QStringList &urls) +{ + if (m_updating) return; // update is already running, do nothing + + m_updating = true; + m_updateProgress = 0; + m_updateTotal = urls.count(); + connect(this, &Fetcher::updateProgressChanged, this, &Fetcher::updateMonitor); + Q_EMIT updatingChanged(m_updating); + Q_EMIT updateProgressChanged(m_updateProgress); + Q_EMIT updateTotalChanged(m_updateTotal); + + for (int i=0; i 0) + fetch(urls); + else + return; // no feeds in database +} + +void Fetcher::retrieveFeed(const QString &url) { qDebug() << "Starting to fetch" << url; @@ -62,39 +101,6 @@ void Fetcher::fetch(const QString &url) }); } -void Fetcher::fetch(const QStringList &urls) -{ - if (m_updating) return; // update is already running, do nothing - - m_updating = true; - m_updateProgress = 0; - m_updateTotal = urls.count(); - connect(this, &Fetcher::updateProgressChanged, this, &Fetcher::updateMonitor); - Q_EMIT updatingChanged(m_updating); - Q_EMIT updateProgressChanged(m_updateProgress); - Q_EMIT updateTotalChanged(m_updateTotal); - - for (int i=0; i 0) - fetch(urls); - else - return; // no feeds in database -} - void Fetcher::updateMonitor(int progress) { //qDebug() << "Update monitor" << progress << "/" << m_updateTotal; diff --git a/src/fetcher.h b/src/fetcher.h index e5d01353..835c7200 100644 --- a/src/fetcher.h +++ b/src/fetcher.h @@ -27,6 +27,7 @@ public: static Fetcher _instance; return _instance; } + Q_INVOKABLE void fetch(const QString &url); Q_INVOKABLE void fetch(const QStringList &urls); Q_INVOKABLE void fetchAll(); @@ -56,6 +57,7 @@ private Q_SLOTS: private: Fetcher(); + void retrieveFeed(const QString &url); void processFeed(Syndication::FeedPtr feed, const QString &url); bool processEntry(Syndication::ItemPtr entry, const QString &url, const bool &isNewFeed); // returns true if this is a new entry; false if it already existed void processAuthor(const QString &url, const QString &entryId, const QString &authorName, const QString &authorUri, const QString &authorEmail);