2022-06-12 21:37:11 +05:30
|
|
|
#include "ConcurrentTask.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
2022-07-25 00:49:25 +05:30
|
|
|
#include <QCoreApplication>
|
2022-06-12 21:37:11 +05:30
|
|
|
|
|
|
|
ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent)
|
|
|
|
: Task(parent), m_name(task_name), m_total_max_size(max_concurrent)
|
2022-07-25 00:49:25 +05:30
|
|
|
{ setObjectName(task_name); }
|
2022-06-12 21:37:11 +05:30
|
|
|
|
|
|
|
ConcurrentTask::~ConcurrentTask()
|
|
|
|
{
|
|
|
|
for (auto task : m_queue) {
|
|
|
|
if (task)
|
|
|
|
task->deleteLater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ConcurrentTask::getStepProgress() const -> qint64
|
|
|
|
{
|
|
|
|
return m_stepProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ConcurrentTask::getStepTotalProgress() const -> qint64
|
|
|
|
{
|
|
|
|
return m_stepTotalProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::addTask(Task::Ptr task)
|
|
|
|
{
|
2022-12-15 22:16:51 +05:30
|
|
|
m_queue.append(task);
|
2022-06-12 21:37:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::executeTask()
|
|
|
|
{
|
2022-09-07 20:41:42 +05:30
|
|
|
// Start the least amount of tasks needed, but at least one
|
2022-12-17 21:25:03 +05:30
|
|
|
int num_starts = qMax(1, qMin(m_total_max_size, m_queue.size()));
|
2022-07-22 08:58:08 +05:30
|
|
|
for (int i = 0; i < num_starts; i++) {
|
2022-07-25 00:49:25 +05:30
|
|
|
QMetaObject::invokeMethod(this, &ConcurrentTask::startNext, Qt::QueuedConnection);
|
|
|
|
}
|
2022-06-12 21:37:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool ConcurrentTask::abort()
|
|
|
|
{
|
2022-07-25 01:46:14 +05:30
|
|
|
m_queue.clear();
|
|
|
|
m_aborted = true;
|
|
|
|
|
2022-06-12 21:37:11 +05:30
|
|
|
if (m_doing.isEmpty()) {
|
|
|
|
// Don't call emitAborted() here, we want to bypass the 'is the task running' check
|
|
|
|
emit aborted();
|
|
|
|
emit finished();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-25 01:46:14 +05:30
|
|
|
bool suceedeed = true;
|
2022-06-12 21:37:11 +05:30
|
|
|
|
2022-07-25 01:46:14 +05:30
|
|
|
QMutableHashIterator<Task*, Task::Ptr> doing_iter(m_doing);
|
|
|
|
while (doing_iter.hasNext()) {
|
|
|
|
auto task = doing_iter.next();
|
|
|
|
suceedeed &= (task.value())->abort();
|
|
|
|
}
|
2022-06-12 21:37:11 +05:30
|
|
|
|
2022-07-25 01:46:14 +05:30
|
|
|
if (suceedeed)
|
2022-06-12 21:37:11 +05:30
|
|
|
emitAborted();
|
2022-07-25 01:46:14 +05:30
|
|
|
else
|
|
|
|
emitFailed(tr("Failed to abort all running tasks."));
|
2022-06-12 21:37:11 +05:30
|
|
|
|
2022-07-25 01:46:14 +05:30
|
|
|
return suceedeed;
|
2022-06-12 21:37:11 +05:30
|
|
|
}
|
|
|
|
|
2022-12-15 22:15:50 +05:30
|
|
|
void ConcurrentTask::clear()
|
|
|
|
{
|
|
|
|
Q_ASSERT(!isRunning());
|
|
|
|
|
|
|
|
m_done.clear();
|
|
|
|
m_failed.clear();
|
|
|
|
m_queue.clear();
|
|
|
|
|
|
|
|
m_aborted = false;
|
|
|
|
|
|
|
|
m_progress = 0;
|
|
|
|
m_stepProgress = 0;
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:37:11 +05:30
|
|
|
void ConcurrentTask::startNext()
|
|
|
|
{
|
|
|
|
if (m_aborted || m_doing.count() > m_total_max_size)
|
|
|
|
return;
|
|
|
|
|
2022-07-22 09:49:56 +05:30
|
|
|
if (m_queue.isEmpty() && m_doing.isEmpty() && !wasSuccessful()) {
|
2022-06-12 21:37:11 +05:30
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_queue.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Task::Ptr next = m_queue.dequeue();
|
|
|
|
|
|
|
|
connect(next.get(), &Task::succeeded, this, [this, next] { subTaskSucceeded(next); });
|
|
|
|
connect(next.get(), &Task::failed, this, [this, next](QString msg) { subTaskFailed(next, msg); });
|
|
|
|
|
|
|
|
connect(next.get(), &Task::status, this, &ConcurrentTask::subTaskStatus);
|
|
|
|
connect(next.get(), &Task::stepStatus, this, &ConcurrentTask::subTaskStatus);
|
|
|
|
|
|
|
|
connect(next.get(), &Task::progress, this, &ConcurrentTask::subTaskProgress);
|
|
|
|
|
|
|
|
m_doing.insert(next.get(), next);
|
|
|
|
|
|
|
|
setStepStatus(next->isMultiStep() ? next->getStepStatus() : next->getStatus());
|
|
|
|
updateState();
|
|
|
|
|
2023-01-18 00:38:50 +05:30
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
2022-12-15 22:16:51 +05:30
|
|
|
QMetaObject::invokeMethod(next.get(), &Task::start, Qt::QueuedConnection);
|
2022-07-25 00:49:25 +05:30
|
|
|
|
2022-12-15 22:16:51 +05:30
|
|
|
// Allow going up the number of concurrent tasks in case of tasks being added in the middle of a running task.
|
|
|
|
int num_starts = m_total_max_size - m_doing.size();
|
|
|
|
for (int i = 0; i < num_starts; i++)
|
|
|
|
QMetaObject::invokeMethod(this, &ConcurrentTask::startNext, Qt::QueuedConnection);
|
2022-06-12 21:37:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::subTaskSucceeded(Task::Ptr task)
|
|
|
|
{
|
|
|
|
m_done.insert(task.get(), task);
|
|
|
|
m_doing.remove(task.get());
|
|
|
|
|
|
|
|
disconnect(task.get(), 0, this, 0);
|
|
|
|
|
|
|
|
updateState();
|
|
|
|
|
|
|
|
startNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::subTaskFailed(Task::Ptr task, const QString& msg)
|
|
|
|
{
|
|
|
|
m_done.insert(task.get(), task);
|
|
|
|
m_failed.insert(task.get(), task);
|
|
|
|
|
|
|
|
m_doing.remove(task.get());
|
|
|
|
|
|
|
|
disconnect(task.get(), 0, this, 0);
|
|
|
|
|
|
|
|
updateState();
|
|
|
|
|
|
|
|
startNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::subTaskStatus(const QString& msg)
|
|
|
|
{
|
|
|
|
setStepStatus(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::subTaskProgress(qint64 current, qint64 total)
|
|
|
|
{
|
|
|
|
m_stepProgress = current;
|
|
|
|
m_stepTotalProgress = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::updateState()
|
|
|
|
{
|
2022-12-17 21:25:03 +05:30
|
|
|
setProgress(m_done.count(), totalSize());
|
2022-06-12 21:37:11 +05:30
|
|
|
setStatus(tr("Executing %1 task(s) (%2 out of %3 are done)")
|
2022-12-17 21:25:03 +05:30
|
|
|
.arg(QString::number(m_doing.count()), QString::number(m_done.count()), QString::number(totalSize())));
|
2022-06-12 21:37:11 +05:30
|
|
|
}
|