refactor: make MultipleOptionsTask inherit from ConcurrentTask too

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-07-21 22:40:06 -03:00
parent e899699918
commit 87a0482b8b
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 19 additions and 42 deletions

View File

@ -2,47 +2,26 @@
#include <QDebug> #include <QDebug>
MultipleOptionsTask::MultipleOptionsTask(QObject* parent, const QString& task_name) : SequentialTask(parent, task_name) {} MultipleOptionsTask::MultipleOptionsTask(QObject* parent, const QString& task_name) : ConcurrentTask(parent, task_name) {}
void MultipleOptionsTask::startNext() void MultipleOptionsTask::startNext()
{ {
Task* previous = nullptr; if (m_done.size() != m_failed.size()) {
if (m_currentIndex != -1) {
previous = m_queue[m_currentIndex].get();
disconnect(previous, 0, this, 0);
}
m_currentIndex++;
if ((previous && previous->wasSuccessful())) {
emitSucceeded(); emitSucceeded();
return; return;
} }
Task::Ptr next = m_queue[m_currentIndex]; if (m_queue.isEmpty()) {
emitFailed(tr("All attempts have failed!"));
connect(next.get(), &Task::failed, this, &MultipleOptionsTask::subTaskFailed); qWarning() << "All attempts have failed!";
connect(next.get(), &Task::succeeded, this, &MultipleOptionsTask::startNext);
connect(next.get(), &Task::status, this, &MultipleOptionsTask::subTaskStatus);
connect(next.get(), &Task::stepStatus, this, &MultipleOptionsTask::subTaskStatus);
connect(next.get(), &Task::progress, this, &MultipleOptionsTask::subTaskProgress);
qDebug() << QString("Making attemp %1 out of %2").arg(m_currentIndex + 1).arg(m_queue.size());
setStatus(tr("Making attempt #%1 out of %2").arg(m_currentIndex + 1).arg(m_queue.size()));
setStepStatus(next->isMultiStep() ? next->getStepStatus() : next->getStatus());
next->start();
}
void MultipleOptionsTask::subTaskFailed(QString const& reason)
{
qDebug() << QString("Failed attempt #%1 of %2. Reason: %3").arg(m_currentIndex + 1).arg(m_queue.size()).arg(reason);
if(m_currentIndex < m_queue.size() - 1) {
startNext();
return; return;
} }
qWarning() << QString("All attempts have failed!"); ConcurrentTask::startNext();
emitFailed(); }
void MultipleOptionsTask::updateState()
{
setProgress(m_done.count(), m_total_size);
setStatus(tr("Attempting task %1 out of %2").arg(QString::number(m_doing.count() + m_done.count()), QString::number(m_total_size)));
} }

View File

@ -1,19 +1,17 @@
#pragma once #pragma once
#include "SequentialTask.h" #include "ConcurrentTask.h"
/* This task type will attempt to do run each of it's subtasks in sequence, /* This task type will attempt to do run each of it's subtasks in sequence,
* until one of them succeeds. When that happens, the remaining tasks will not run. * until one of them succeeds. When that happens, the remaining tasks will not run.
* */ * */
class MultipleOptionsTask : public SequentialTask class MultipleOptionsTask : public ConcurrentTask {
{
Q_OBJECT Q_OBJECT
public: public:
explicit MultipleOptionsTask(QObject* parent = nullptr, const QString& task_name = ""); explicit MultipleOptionsTask(QObject* parent = nullptr, const QString& task_name = "");
virtual ~MultipleOptionsTask() = default; ~MultipleOptionsTask() override = default;
private private slots:
slots:
void startNext() override; void startNext() override;
void subTaskFailed(const QString &msg) override; void updateState() override;
}; };