2023-04-03 03:40:32 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
* Copyright (c) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2022-06-12 17:07:11 +01:00
|
|
|
#include "ConcurrentTask.h"
|
|
|
|
|
2022-07-24 20:19:25 +01:00
|
|
|
#include <QCoreApplication>
|
2023-04-03 05:51:07 +01:00
|
|
|
#include <QDebug>
|
2023-03-31 07:50:29 +01:00
|
|
|
#include "tasks/Task.h"
|
2022-06-12 17:07:11 +01:00
|
|
|
|
|
|
|
ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent)
|
|
|
|
: Task(parent), m_name(task_name), m_total_max_size(max_concurrent)
|
2023-04-03 05:51:07 +01:00
|
|
|
{
|
|
|
|
setObjectName(task_name);
|
|
|
|
}
|
2022-06-12 17:07:11 +01:00
|
|
|
|
|
|
|
ConcurrentTask::~ConcurrentTask()
|
|
|
|
{
|
|
|
|
for (auto task : m_queue) {
|
|
|
|
if (task)
|
|
|
|
task->deleteLater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 07:50:29 +01:00
|
|
|
auto ConcurrentTask::getStepProgress() const -> TaskStepProgressList
|
2022-06-12 17:07:11 +01:00
|
|
|
{
|
2023-03-31 07:50:29 +01:00
|
|
|
return m_task_progress.values();
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::addTask(Task::Ptr task)
|
|
|
|
{
|
2022-12-15 16:46:51 +00:00
|
|
|
m_queue.append(task);
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::executeTask()
|
|
|
|
{
|
2023-05-05 22:07:10 +01:00
|
|
|
// Start one task, startNext handles starting the up to the m_total_max_size
|
2023-03-30 19:22:55 +01:00
|
|
|
// while tracking the number currently being done
|
|
|
|
QMetaObject::invokeMethod(this, &ConcurrentTask::startNext, Qt::QueuedConnection);
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConcurrentTask::abort()
|
|
|
|
{
|
2022-07-24 21:16:14 +01:00
|
|
|
m_queue.clear();
|
|
|
|
m_aborted = true;
|
|
|
|
|
2022-06-12 17:07:11 +01:00
|
|
|
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-24 21:16:14 +01:00
|
|
|
bool suceedeed = true;
|
2022-06-12 17:07:11 +01:00
|
|
|
|
2022-07-24 21:16:14 +01:00
|
|
|
QMutableHashIterator<Task*, Task::Ptr> doing_iter(m_doing);
|
|
|
|
while (doing_iter.hasNext()) {
|
|
|
|
auto task = doing_iter.next();
|
|
|
|
suceedeed &= (task.value())->abort();
|
|
|
|
}
|
2022-06-12 17:07:11 +01:00
|
|
|
|
2022-07-24 21:16:14 +01:00
|
|
|
if (suceedeed)
|
2022-06-12 17:07:11 +01:00
|
|
|
emitAborted();
|
2022-07-24 21:16:14 +01:00
|
|
|
else
|
|
|
|
emitFailed(tr("Failed to abort all running tasks."));
|
2022-06-12 17:07:11 +01:00
|
|
|
|
2022-07-24 21:16:14 +01:00
|
|
|
return suceedeed;
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
2022-12-15 16:45:50 +00:00
|
|
|
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 17:07:11 +01:00
|
|
|
void ConcurrentTask::startNext()
|
|
|
|
{
|
|
|
|
if (m_aborted || m_doing.count() > m_total_max_size)
|
|
|
|
return;
|
|
|
|
|
2022-07-22 05:19:56 +01:00
|
|
|
if (m_queue.isEmpty() && m_doing.isEmpty() && !wasSuccessful()) {
|
2022-06-12 17:07:11 +01:00
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_queue.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Task::Ptr next = m_queue.dequeue();
|
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
connect(next.get(), &Task::succeeded, this, [this, next]() { subTaskSucceeded(next); });
|
2022-06-12 17:07:11 +01:00
|
|
|
connect(next.get(), &Task::failed, this, [this, next](QString msg) { subTaskFailed(next, msg); });
|
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
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); });
|
2023-04-08 03:44:57 +01:00
|
|
|
connect(next.get(), &Task::stepProgress, this, [this, next](TaskStepProgress const& tp) { subTaskStepProgress(next, tp); });
|
2022-06-12 17:07:11 +01:00
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total) { subTaskProgress(next, current, total); });
|
2022-06-12 17:07:11 +01:00
|
|
|
|
|
|
|
m_doing.insert(next.get(), next);
|
2023-04-07 21:01:45 +01:00
|
|
|
auto task_progress = std::make_shared<TaskStepProgress>(TaskStepProgress({ next->getUid() }));
|
|
|
|
m_task_progress.insert(next->getUid(), task_progress);
|
2022-06-12 17:07:11 +01:00
|
|
|
|
|
|
|
updateState();
|
2023-04-07 21:01:45 +01:00
|
|
|
updateStepProgress(*task_progress.get(), Operation::ADDED);
|
|
|
|
|
2022-06-12 17:07:11 +01:00
|
|
|
|
2023-01-17 19:08:50 +00:00
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
2022-12-15 16:46:51 +00:00
|
|
|
QMetaObject::invokeMethod(next.get(), &Task::start, Qt::QueuedConnection);
|
2022-07-24 20:19:25 +01:00
|
|
|
|
2022-12-15 16:46:51 +00:00
|
|
|
// Allow going up the number of concurrent tasks in case of tasks being added in the middle of a running task.
|
2023-01-24 18:44:12 +00:00
|
|
|
int num_starts = qMin(m_queue.size(), m_total_max_size - m_doing.size());
|
2022-12-15 16:46:51 +00:00
|
|
|
for (int i = 0; i < num_starts; i++)
|
|
|
|
QMetaObject::invokeMethod(this, &ConcurrentTask::startNext, Qt::QueuedConnection);
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::subTaskSucceeded(Task::Ptr task)
|
|
|
|
{
|
|
|
|
m_done.insert(task.get(), task);
|
2023-03-30 19:22:55 +01:00
|
|
|
m_succeeded.insert(task.get(), task);
|
|
|
|
|
2022-06-12 17:07:11 +01:00
|
|
|
m_doing.remove(task.get());
|
2023-04-03 05:51:07 +01:00
|
|
|
auto task_progress = m_task_progress.value(task->getUid());
|
|
|
|
task_progress->state = TaskStepState::Succeeded;
|
2022-06-12 17:07:11 +01:00
|
|
|
|
|
|
|
disconnect(task.get(), 0, this, 0);
|
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
emit stepProgress(*task_progress.get());
|
2022-06-12 17:07:11 +01:00
|
|
|
updateState();
|
2023-04-07 21:01:45 +01:00
|
|
|
updateStepProgress(*task_progress.get(), Operation::REMOVED);
|
2022-06-12 17:07:11 +01:00
|
|
|
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());
|
2023-04-03 05:51:07 +01:00
|
|
|
|
|
|
|
auto task_progress = m_task_progress.value(task->getUid());
|
|
|
|
task_progress->state = TaskStepState::Failed;
|
2022-06-12 17:07:11 +01:00
|
|
|
|
|
|
|
disconnect(task.get(), 0, this, 0);
|
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
emit stepProgress(*task_progress.get());
|
2022-06-12 17:07:11 +01:00
|
|
|
updateState();
|
2023-04-07 21:01:45 +01:00
|
|
|
updateStepProgress(*task_progress.get(), Operation::REMOVED);
|
2022-06-12 17:07:11 +01:00
|
|
|
startNext();
|
|
|
|
}
|
|
|
|
|
2023-03-30 19:22:55 +01:00
|
|
|
void ConcurrentTask::subTaskStatus(Task::Ptr task, const QString& msg)
|
|
|
|
{
|
2023-04-03 05:51:07 +01:00
|
|
|
auto task_progress = m_task_progress.value(task->getUid());
|
|
|
|
task_progress->status = msg;
|
|
|
|
task_progress->state = TaskStepState::Running;
|
|
|
|
|
|
|
|
emit stepProgress(*task_progress.get());
|
2023-04-09 23:10:49 +01:00
|
|
|
|
|
|
|
if (totalSize() == 1) {
|
|
|
|
setStatus(msg);
|
|
|
|
}
|
2023-04-01 03:25:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::subTaskDetails(Task::Ptr task, const QString& msg)
|
|
|
|
{
|
2023-04-03 05:51:07 +01:00
|
|
|
auto task_progress = m_task_progress.value(task->getUid());
|
|
|
|
task_progress->details = msg;
|
|
|
|
task_progress->state = TaskStepState::Running;
|
|
|
|
|
|
|
|
emit stepProgress(*task_progress.get());
|
2023-04-09 23:10:49 +01:00
|
|
|
|
|
|
|
if (totalSize() == 1) {
|
|
|
|
setDetails(msg);
|
|
|
|
}
|
2023-03-30 19:22:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 total)
|
2022-06-12 17:07:11 +01:00
|
|
|
{
|
2023-04-03 05:51:07 +01:00
|
|
|
auto task_progress = m_task_progress.value(task->getUid());
|
2023-03-30 19:22:55 +01:00
|
|
|
|
2023-04-07 21:01:45 +01:00
|
|
|
task_progress->old_current = task_progress->current;
|
|
|
|
task_progress->old_total = task_progress->old_total;
|
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
task_progress->current = current;
|
|
|
|
task_progress->total = total;
|
|
|
|
task_progress->state = TaskStepState::Running;
|
|
|
|
|
|
|
|
emit stepProgress(*task_progress.get());
|
2023-04-07 21:01:45 +01:00
|
|
|
updateStepProgress(*task_progress.get(), Operation::CHANGED);
|
2023-03-30 19:22:55 +01:00
|
|
|
updateState();
|
2023-04-09 23:10:49 +01:00
|
|
|
|
|
|
|
if (totalSize() == 1) {
|
|
|
|
setProgress(task_progress->current, task_progress->total);
|
|
|
|
}
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
2023-04-08 03:44:57 +01:00
|
|
|
void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_progress)
|
2022-06-12 17:07:11 +01:00
|
|
|
{
|
2023-04-07 21:01:45 +01:00
|
|
|
Operation op = Operation::ADDED;
|
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
if (!m_task_progress.contains(task_progress.uid)) {
|
|
|
|
m_task_progress.insert(task_progress.uid, std::make_shared<TaskStepProgress>(task_progress));
|
2023-04-07 21:01:45 +01:00
|
|
|
op = Operation::ADDED;
|
2023-04-08 03:44:57 +01:00
|
|
|
emit stepProgress(task_progress);
|
|
|
|
updateStepProgress(task_progress, op);
|
2023-04-03 05:51:07 +01:00
|
|
|
} else {
|
|
|
|
auto tp = m_task_progress.value(task_progress.uid);
|
2023-04-07 21:01:45 +01:00
|
|
|
|
|
|
|
tp->old_current = tp->current;
|
|
|
|
tp->old_total = tp->total;
|
|
|
|
|
2023-04-03 05:51:07 +01:00
|
|
|
tp->current = task_progress.current;
|
|
|
|
tp->total = task_progress.total;
|
|
|
|
tp->status = task_progress.status;
|
|
|
|
tp->details = task_progress.details;
|
2023-04-07 21:01:45 +01:00
|
|
|
|
|
|
|
op = Operation::CHANGED;
|
2023-04-08 03:44:57 +01:00
|
|
|
emit stepProgress(*tp.get());
|
|
|
|
updateStepProgress(*tp.get(), op);
|
2023-03-30 19:22:55 +01:00
|
|
|
}
|
2023-04-07 21:01:45 +01:00
|
|
|
|
2023-03-30 19:22:55 +01:00
|
|
|
}
|
|
|
|
|
2023-04-07 21:01:45 +01:00
|
|
|
void ConcurrentTask::updateStepProgress(TaskStepProgress const& changed_progress, Operation op)
|
2023-03-30 19:22:55 +01:00
|
|
|
{
|
2023-04-07 21:01:45 +01:00
|
|
|
|
|
|
|
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;
|
2023-04-03 05:51:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConcurrentTask::updateState()
|
|
|
|
{
|
2023-03-30 19:22:55 +01:00
|
|
|
if (totalSize() > 1) {
|
|
|
|
setProgress(m_done.count(), totalSize());
|
2023-04-03 05:51:07 +01:00
|
|
|
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())));
|
2023-03-30 19:22:55 +01:00
|
|
|
} else {
|
|
|
|
setProgress(m_stepProgress, m_stepTotalProgress);
|
2023-05-05 22:07:10 +01:00
|
|
|
QString status = tr("Please wait...");
|
2023-03-30 19:22:55 +01:00
|
|
|
if (m_queue.size() > 0) {
|
2023-05-05 22:07:10 +01:00
|
|
|
status = tr("Waiting for a task to start...");
|
2023-03-30 19:22:55 +01:00
|
|
|
} else if (m_doing.size() > 0) {
|
|
|
|
status = tr("Executing 1 task:");
|
|
|
|
} else if (m_done.size() > 0) {
|
|
|
|
status = tr("Task finished.");
|
|
|
|
}
|
|
|
|
setStatus(status);
|
|
|
|
}
|
2022-06-12 17:07:11 +01:00
|
|
|
}
|