Merge pull request #2789 from Trial97/fix_concurrent_task

Remove all the slowdown code from CocurrentTask
This commit is contained in:
timoreo 2024-09-13 21:10:30 +02:00 committed by GitHub
commit f46b73acb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 67 deletions

View File

@ -38,8 +38,7 @@
#include <QDebug>
#include "tasks/Task.h"
ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent)
: Task(parent), m_name(task_name), m_total_max_size(max_concurrent)
ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent) : Task(parent), m_total_max_size(max_concurrent)
{
setObjectName(task_name);
}
@ -104,9 +103,9 @@ void ConcurrentTask::clear()
m_done.clear();
m_failed.clear();
m_queue.clear();
m_task_progress.clear();
m_progress = 0;
m_stepProgress = 0;
}
void ConcurrentTask::executeNextSubTask()
@ -139,7 +138,7 @@ void ConcurrentTask::startSubTask(Task::Ptr next)
connect(next.get(), &Task::status, this, [this, next](QString msg) { subTaskStatus(next, msg); });
connect(next.get(), &Task::details, this, [this, next](QString msg) { subTaskDetails(next, msg); });
connect(next.get(), &Task::stepProgress, this, [this, next](TaskStepProgress const& tp) { subTaskStepProgress(next, tp); });
connect(next.get(), &Task::stepProgress, this, &ConcurrentTask::stepProgress);
connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total) { subTaskProgress(next, current, total); });
@ -149,7 +148,6 @@ void ConcurrentTask::startSubTask(Task::Ptr next)
m_task_progress.insert(next->getUid(), task_progress);
updateState();
updateStepProgress(*task_progress.get(), Operation::ADDED);
QMetaObject::invokeMethod(next.get(), &Task::start, Qt::QueuedConnection);
}
@ -161,14 +159,14 @@ void ConcurrentTask::subTaskFinished(Task::Ptr task, TaskStepState state)
m_doing.remove(task.get());
auto task_progress = m_task_progress.value(task->getUid());
task_progress->state = state;
auto task_progress = *m_task_progress.value(task->getUid());
task_progress.state = state;
m_task_progress.remove(task->getUid());
disconnect(task.get(), 0, this, 0);
emit stepProgress(*task_progress);
emit stepProgress(task_progress);
updateState();
updateStepProgress(*task_progress, Operation::REMOVED);
QMetaObject::invokeMethod(this, &ConcurrentTask::executeNextSubTask, Qt::QueuedConnection);
}
@ -215,7 +213,6 @@ void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 tota
task_progress->update(current, total);
emit stepProgress(*task_progress);
updateStepProgress(*task_progress, Operation::CHANGED);
updateState();
if (totalSize() == 1) {
@ -223,52 +220,6 @@ void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 tota
}
}
void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_progress)
{
Operation op = Operation::ADDED;
if (!m_task_progress.contains(task_progress.uid)) {
m_task_progress.insert(task_progress.uid, std::make_shared<TaskStepProgress>(task_progress));
op = Operation::ADDED;
emit stepProgress(task_progress);
updateStepProgress(task_progress, op);
} else {
auto tp = m_task_progress.value(task_progress.uid);
tp->old_current = tp->current;
tp->old_total = tp->total;
tp->current = task_progress.current;
tp->total = task_progress.total;
tp->status = task_progress.status;
tp->details = task_progress.details;
op = Operation::CHANGED;
emit stepProgress(*tp.get());
updateStepProgress(*tp.get(), op);
}
}
void ConcurrentTask::updateStepProgress(TaskStepProgress const& changed_progress, Operation op)
{
switch (op) {
case Operation::ADDED:
m_stepProgress += changed_progress.current;
m_stepTotalProgress += changed_progress.total;
break;
case Operation::REMOVED:
m_stepProgress -= changed_progress.current;
m_stepTotalProgress -= changed_progress.total;
break;
case Operation::CHANGED:
m_stepProgress -= changed_progress.old_current;
m_stepTotalProgress -= changed_progress.old_total;
m_stepProgress += changed_progress.current;
m_stepTotalProgress += changed_progress.total;
break;
}
}
void ConcurrentTask::updateState()
{
if (totalSize() > 1) {
@ -276,7 +227,6 @@ void ConcurrentTask::updateState()
setStatus(tr("Executing %1 task(s) (%2 out of %3 are done)")
.arg(QString::number(m_doing.count()), QString::number(m_done.count()), QString::number(totalSize())));
} else {
setProgress(m_stepProgress, m_stepTotalProgress);
QString status = tr("Please wait...");
if (m_queue.size() > 0) {
status = tr("Waiting for a task to start...");

View File

@ -80,23 +80,16 @@ class ConcurrentTask : public Task {
void subTaskStatus(Task::Ptr task, const QString& msg);
void subTaskDetails(Task::Ptr task, const QString& msg);
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
void subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_step_progress);
protected:
// NOTE: This is not thread-safe.
[[nodiscard]] unsigned int totalSize() const { return static_cast<unsigned int>(m_queue.size() + m_doing.size() + m_done.size()); }
enum class Operation { ADDED, REMOVED, CHANGED };
void updateStepProgress(TaskStepProgress const& changed_progress, Operation);
virtual void updateState();
void startSubTask(Task::Ptr task);
protected:
QString m_name;
QString m_step_status;
QQueue<Task::Ptr> m_queue;
QHash<Task*, Task::Ptr> m_doing;
@ -107,7 +100,4 @@ class ConcurrentTask : public Task {
QHash<QUuid, std::shared_ptr<TaskStepProgress>> m_task_progress;
int m_total_max_size;
qint64 m_stepProgress = 0;
qint64 m_stepTotalProgress = 100;
};