First experiment - working parallels.

This commit is contained in:
Martin Rotter 2016-05-31 07:19:22 +02:00
parent 0b3d2bc8bc
commit 84c2a2b2a2
5 changed files with 91 additions and 26 deletions

View File

@ -61,8 +61,8 @@ project(rssguard)
set(APP_NAME "RSS Guard")
set(APP_LOW_NAME "rssguard")
set(APP_VERSION "3.2.5")
set(FILE_VERSION "3,2,5,0")
set(APP_VERSION "3.3.0")
set(FILE_VERSION "3,3,0,0")
set(APP_AUTHOR "Martin Rotter")
set(APP_URL "http://bitbucket.org/skunkos/rssguard")
set(APP_URL_ISSUES "http://bitbucket.org/skunkos/rssguard/issues")

View File

@ -23,9 +23,10 @@
#include <QThread>
#include <QDebug>
#include <QMetaType>
#include <QThreadPool>
FeedDownloader::FeedDownloader(QObject *parent) : QObject(parent), m_isUpdateRunning(false), m_stopUpdate(false) {
FeedDownloader::FeedDownloader(QObject *parent) : QObject(parent), m_results(FeedDownloadResults()), m_isUpdateRunning(false), m_stopUpdate(false) {
qRegisterMetaType<FeedDownloadResults>("FeedDownloadResults");
}
@ -45,30 +46,26 @@ void FeedDownloader::updateFeeds(const QList<Feed*> &feeds) {
m_isUpdateRunning = true;
m_stopUpdate = false;
m_results.clear();
m_feedsUpdating = 0;
m_feedsToUpdate = feeds.size();
m_totalFeedsCount = m_feedsToUpdate;
// Job starts now.
emit started();
FeedDownloadResults results;
for (int i = 0, total = feeds.size(); i < total; i++) {
for (int i = 0, total = m_totalFeedsCount; i < total; i++) {
if (m_stopUpdate) {
qDebug("Stopping batch feed update now.");
break;
}
int updated_messages = feeds.at(i)->update();
if (updated_messages > 0) {
results.appendUpdatedFeed(QPair<QString,int>(feeds.at(i)->title(), updated_messages));
}
qDebug("Made progress in feed updates: %d/%d (id of feed is %d).", i + 1, total, feeds.at(i)->id());
emit progress(feeds.at(i), i + 1, total);
}
m_feedsToUpdate = 0;
if (m_feedsUpdating <= 0) {
qDebug().nospace() << "Finished feed updates in thread: \'" << QThread::currentThreadId() << "\'.";
results.sort();
m_results.sort();
// Make sure that there is not "stop" action pending.
m_isUpdateRunning = false;
@ -78,13 +75,58 @@ void FeedDownloader::updateFeeds(const QList<Feed*> &feeds) {
// NOTE: This means that now "update lock" can be unlocked
// and feeds can be added/edited/deleted and application
// can eventually quit.
emit finished(results);
emit finished(m_results);
}
break;
}
connect(feeds.at(i), SIGNAL(updated(int)), this, SLOT(oneFeedUpdateFinished(int)),
(Qt::ConnectionType) (Qt::UniqueConnection | Qt::AutoConnection));
QThreadPool::globalInstance()->start(feeds.at(i));
m_feedsUpdating++;
m_feedsToUpdate--;
}
}
void FeedDownloader::stopRunningUpdate() {
m_stopUpdate = true;
}
void FeedDownloader::oneFeedUpdateFinished(int updated_messages) {
Feed *feed = qobject_cast<Feed*>(sender());
disconnect(feed, SIGNAL(updated(int)),
this, SLOT(oneFeedUpdateFinished(int)));
if (updated_messages > 0) {
m_results.appendUpdatedFeed(QPair<QString,int>(feed->title(), updated_messages));
}
qDebug("Made progress in feed updates, total feeds count %d (id of feed is %d).",
m_totalFeedsCount, feed->id());
emit progress(feed, m_totalFeedsCount - m_feedsToUpdate, m_totalFeedsCount);
m_feedsUpdating--;
if (m_feedsToUpdate == 0 && m_feedsUpdating == 0) {
qDebug().nospace() << "Finished feed updates in thread: \'" << QThread::currentThreadId() << "\'.";
m_results.sort();
// Make sure that there is not "stop" action pending.
m_isUpdateRunning = false;
m_stopUpdate = false;
// Update of feeds has finished.
// NOTE: This means that now "update lock" can be unlocked
// and feeds can be added/edited/deleted and application
// can eventually quit.
emit finished(m_results);
}
}
FeedDownloadResults::FeedDownloadResults() : m_updatedFeeds(QList<QPair<QString,int> >()) {
}

View File

@ -38,6 +38,10 @@ class FeedDownloadResults {
static bool lessThan(const QPair<QString,int> &lhs, const QPair<QString,int> &rhs);
inline void clear() {
m_updatedFeeds.clear();
}
private:
// QString represents title if the feed, int represents count of newly downloaded messages.
QList<QPair<QString,int> > m_updatedFeeds;
@ -65,6 +69,9 @@ class FeedDownloader : public QObject {
// Stops running update.
void stopRunningUpdate();
private slots:
void oneFeedUpdateFinished(int updated_messages);
signals:
// Emitted if feed updates started.
void started();
@ -80,6 +87,10 @@ class FeedDownloader : public QObject {
void progress(const Feed *feed, int current, int total);
private:
FeedDownloadResults m_results;
int m_feedsToUpdate;
int m_feedsUpdating;
int m_totalFeedsCount;
bool m_isUpdateRunning;
bool m_stopUpdate;
};

View File

@ -29,6 +29,7 @@ Feed::Feed(RootItem *parent)
m_autoUpdateInitialInterval(DEFAULT_AUTO_UPDATE_INTERVAL), m_autoUpdateRemainingInterval(DEFAULT_AUTO_UPDATE_INTERVAL),
m_totalCount(0), m_unreadCount(0) {
setKind(RootItemKind::Feed);
setAutoDelete(false);
}
Feed::~Feed() {
@ -129,6 +130,10 @@ void Feed::updateCounts(bool including_total_count) {
setCountOfUnreadMessages(DatabaseQueries::getMessageCountsForFeed(database, customId(), account_id, false));
}
void Feed::run() {
emit updated(update());
}
int Feed::updateMessages(const QList<Message> &messages) {
int custom_id = customId();
int account_id = getParentServiceRoot()->accountId();

View File

@ -23,10 +23,11 @@
#include "core/message.h"
#include <QVariant>
#include <QRunnable>
// Base class for "feed" nodes.
class Feed : public RootItem {
class Feed : public RootItem, public QRunnable {
Q_OBJECT
public:
@ -96,12 +97,18 @@ class Feed : public RootItem {
void updateCounts(bool including_total_count);
// Runs update in thread (thread pooled).
void run();
protected:
virtual QList<Message> obtainNewMessages() = 0;
private:
int updateMessages(const QList<Message> &messages);
signals:
void updated(int updated_messages);
private:
QString m_url;
Status m_status;