Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to concurrent tasks #1694

Merged
merged 26 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7f4665e
format
Trial97 Jul 14, 2023
d3dc00d
made concurent task fail
Trial97 Jul 14, 2023
b54e093
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Jul 15, 2023
0880d4d
made sure abort signal is catched in cocurent task
Trial97 Jul 15, 2023
a7e0c9d
connected failed event for some tasks
Trial97 Jul 15, 2023
6cf7c0a
Connected last fail signals
Trial97 Jul 15, 2023
f42514a
Update launcher/tasks/ConcurrentTask.cpp
Trial97 Jul 15, 2023
5981e78
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Aug 5, 2023
24b47e4
Merge branch 'fail_concurrent_task' of github.com:Trial97/PrismLaunch…
Trial97 Aug 5, 2023
42a6c67
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Aug 15, 2023
abbac5c
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Sep 1, 2023
47dbb09
Improvements to concurrent task
Trial97 Oct 6, 2023
8dd6408
removed processEvents from ConcurrentTask
Trial97 Oct 8, 2023
cc66350
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Oct 16, 2023
30f73a0
Merge branch 'fail_concurrent_task' of github.com:Trial97/PrismLaunch…
Trial97 Oct 18, 2023
8d4f508
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Oct 18, 2023
1ac9757
Added back the processEvents
Trial97 Oct 19, 2023
35a8ab2
Removed processEvents
Trial97 Oct 20, 2023
a748f49
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Oct 22, 2023
0ca3873
small fix
Trial97 Oct 22, 2023
2aeb829
removed the concurent task destructor behaivior
Trial97 Oct 22, 2023
04acd27
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Oct 26, 2023
dba5433
fixed some asan stuf
Trial97 Oct 26, 2023
20e0cb4
Merge branch 'develop' of https://github.com/PrismLauncher/PrismLaunc…
Trial97 Nov 23, 2023
2d91bd0
Fixed crash on abort function not initialized
Trial97 Nov 26, 2023
463ff4c
Removed assert permanantly
Trial97 Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion launcher/minecraft/mod/tasks/GetModDependenciesTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ Task::Ptr GetModDependenciesTask::prepareDependencyTask(const ModPlatform::Depen

ResourceAPI::DependencySearchArgs args = { dep, m_version, m_loaderType };
ResourceAPI::DependencySearchCallbacks callbacks;

callbacks.on_fail = [](QString reason, int) {
qCritical() << tr("A network error occurred. Could not load project dependenies:%1").arg(reason);
};
callbacks.on_succeed = [dep, provider, pDep, level, this](auto& doc, [[maybe_unused]] auto& pack) {
try {
QJsonArray arr;
Expand Down
1 change: 1 addition & 0 deletions launcher/modplatform/EnsureMetadataTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ void EnsureMetadataTask::executeTask()
if (m_current_task)
m_current_task.reset();
});
connect(project_task.get(), &Task::failed, this, &EnsureMetadataTask::emitFailed);

m_current_task = project_task;
project_task->start();
Expand Down
2 changes: 2 additions & 0 deletions launcher/modplatform/ResourceAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class ResourceAPI {
};
struct VersionSearchCallbacks {
std::function<void(QJsonDocument&, ModPlatform::IndexedPack)> on_succeed;
std::function<void(QString const& reason, int network_error_code)> on_fail;
};

struct ProjectInfoArgs {
Expand All @@ -118,6 +119,7 @@ class ResourceAPI {

struct DependencySearchCallbacks {
std::function<void(QJsonDocument&, const ModPlatform::Dependency&)> on_succeed;
std::function<void(QString const& reason, int network_error_code)> on_fail;
};

public:
Expand Down
1 change: 0 additions & 1 deletion launcher/modplatform/flame/FileResolvingTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ void Flame::FileResolvingTask::netJobFinished()
connect(m_checkJob.get(), &NetJob::failed, this, [this, step_progress](QString reason) {
step_progress->state = TaskStepState::Failed;
stepProgress(*step_progress);
emitFailed(reason);
});
connect(m_checkJob.get(), &NetJob::stepProgress, this, &FileResolvingTask::propagateStepProgress);
connect(m_checkJob.get(), &NetJob::progress, this, [this, step_progress](qint64 current, qint64 total) {
Expand Down
7 changes: 4 additions & 3 deletions launcher/modplatform/flame/FlameCheckUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool FlameCheckUpdate::abort()
return true;
}

ModPlatform::IndexedPack getProjectInfo(ModPlatform::IndexedVersion& ver_info)
ModPlatform::IndexedPack FlameCheckUpdate::getProjectInfo(ModPlatform::IndexedVersion& ver_info)
{
ModPlatform::IndexedPack pack;

Expand Down Expand Up @@ -57,6 +57,7 @@ ModPlatform::IndexedPack getProjectInfo(ModPlatform::IndexedVersion& ver_info)
}
});

connect(get_project_job, &NetJob::failed, this, &FlameCheckUpdate::emitFailed);
QObject::connect(get_project_job, &NetJob::finished, [&loop, get_project_job] {
get_project_job->deleteLater();
loop.quit();
Expand All @@ -68,7 +69,7 @@ ModPlatform::IndexedPack getProjectInfo(ModPlatform::IndexedVersion& ver_info)
return pack;
}

ModPlatform::IndexedVersion getFileInfo(int addonId, int fileId)
ModPlatform::IndexedVersion FlameCheckUpdate::getFileInfo(int addonId, int fileId)
{
ModPlatform::IndexedVersion ver;

Expand Down Expand Up @@ -100,7 +101,7 @@ ModPlatform::IndexedVersion getFileInfo(int addonId, int fileId)
qDebug() << doc;
}
});

connect(get_file_info_job, &NetJob::failed, this, &FlameCheckUpdate::emitFailed);
QObject::connect(get_file_info_job, &NetJob::finished, [&loop, get_file_info_job] {
get_file_info_job->deleteLater();
loop.quit();
Expand Down
3 changes: 3 additions & 0 deletions launcher/modplatform/flame/FlameCheckUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class FlameCheckUpdate : public CheckUpdateTask {
void executeTask() override;

private:
ModPlatform::IndexedPack getProjectInfo(ModPlatform::IndexedVersion& ver_info);
ModPlatform::IndexedVersion getFileInfo(int addonId, int fileId);

NetJob* m_net_job = nullptr;

bool m_was_aborted = false;
Expand Down
1 change: 1 addition & 0 deletions launcher/modplatform/flame/FlameInstanceCreationTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ bool FlameCreationTask::updateInstance()
m_files_to_remove.append(old_minecraft_dir.absoluteFilePath(relative_path));
}
});
connect(job.get(), &Task::failed, this, [](QString reason) { qCritical() << "Failed to get files: " << reason; });
connect(job.get(), &Task::finished, &loop, &QEventLoop::quit);

m_process_update_file_info_job = job;
Expand Down
1 change: 1 addition & 0 deletions launcher/modplatform/flame/FlamePackExportTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ void FlamePackExportTask::getProjectsInfo()
}
buildZip();
});
connect(projTask.get(), &Task::failed, this, &FlamePackExportTask::emitFailed);
task.reset(projTask);
task->start();
}
Expand Down
13 changes: 13 additions & 0 deletions launcher/modplatform/helpers/NetworkResourceAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ Task::Ptr NetworkResourceAPI::getProjectVersions(VersionSearchArgs&& args, Versi

callbacks.on_succeed(doc, args.pack);
});
QObject::connect(netJob.get(), &NetJob::failed, [&netJob, callbacks](QString reason) {
int network_error_code = -1;
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action && failed_action->m_reply)
network_error_code = failed_action->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

callbacks.on_fail(reason, network_error_code);
});

return netJob;
}
Expand Down Expand Up @@ -146,6 +153,12 @@ Task::Ptr NetworkResourceAPI::getDependencyVersion(DependencySearchArgs&& args,

callbacks.on_succeed(doc, args.dependency);
});
QObject::connect(netJob.get(), &NetJob::failed, [&netJob, callbacks](QString reason) {
int network_error_code = -1;
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action && failed_action->m_reply)
network_error_code = failed_action->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

callbacks.on_fail(reason, network_error_code);
});
return netJob;
}
16 changes: 6 additions & 10 deletions launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,15 @@ void ModrinthCheckUpdate::executeTask()
auto response = std::make_shared<QByteArray>();
auto job = api.latestVersions(hashes, best_hash_type, m_game_versions, m_loaders, response);

QEventLoop lock;

connect(job.get(), &Task::succeeded, this, [this, response, &mappings, best_hash_type, job] {
connect(job.get(), &Task::succeeded, this, [this, response, mappings, best_hash_type, job] {
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from ModrinthCheckUpdate at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;

failed(parse_error.errorString());
emitFailed(parse_error.errorString());
return;
}

Expand Down Expand Up @@ -167,19 +165,17 @@ void ModrinthCheckUpdate::executeTask()
m_deps.append(std::make_shared<GetModDependenciesTask::PackDependency>(pack, project_ver));
}
} catch (Json::JsonException& e) {
failed(e.cause() + " : " + e.what());
emitFailed(e.cause() + " : " + e.what());
return;
}
emitSucceeded();
});

connect(job.get(), &Task::finished, &lock, &QEventLoop::quit);
connect(job.get(), &Task::failed, this, &ModrinthCheckUpdate::emitFailed);

setStatus(tr("Waiting for the API response from Modrinth..."));
setProgress(1, 3);

m_net_job = qSharedPointerObjectCast<NetJob, Task>(job);
job->start();

lock.exec();

emitSucceeded();
}
18 changes: 8 additions & 10 deletions launcher/net/NetJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/

#include "NetJob.h"
#include "tasks/ConcurrentTask.h"
#if defined(LAUNCHER_APPLICATION)
#include "Application.h"
#endif
Expand All @@ -56,18 +57,15 @@ auto NetJob::addNetAction(NetAction::Ptr action) -> bool
return true;
}

void NetJob::startNext()
void NetJob::executeNextSubTask()
{
if (m_queue.isEmpty() && m_doing.isEmpty()) {
// We're finished, check for failures and retry if we can (up to 3 times)
if (!m_failed.isEmpty() && m_try < 3) {
m_try += 1;
while (!m_failed.isEmpty())
m_queue.enqueue(m_failed.take(*m_failed.keyBegin()));
}
// We're finished, check for failures and retry if we can (up to 3 times)
if (isRunning() && m_queue.isEmpty() && m_doing.isEmpty() && !m_failed.isEmpty() && m_try < 3) {
m_try += 1;
while (!m_failed.isEmpty())
m_queue.enqueue(m_failed.take(*m_failed.keyBegin()));
}

ConcurrentTask::startNext();
ConcurrentTask::executeNextSubTask();
}

auto NetJob::size() const -> int
Expand Down
5 changes: 3 additions & 2 deletions launcher/net/NetJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class NetJob : public ConcurrentTask {
explicit NetJob(QString job_name, shared_qobject_ptr<QNetworkAccessManager> network);
~NetJob() override = default;

void startNext() override;

auto size() const -> int;

auto canAbort() const -> bool override;
Expand All @@ -69,6 +67,9 @@ class NetJob : public ConcurrentTask {
// Qt can't handle auto at the start for some reason?
bool abort() override;

protected slots:
void executeNextSubTask() override;

protected:
void updateState() override;

Expand Down
4 changes: 2 additions & 2 deletions launcher/screenshots/ImgurUpload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ void ImgurUpload::init()

QNetworkReply* ImgurUpload::getReply(QNetworkRequest& request)
{
auto file = new QFile(m_fileInfo.absoluteFilePath());
auto file = new QFile(m_fileInfo.absoluteFilePath(), this);

if (!file->open(QFile::ReadOnly)) {
emitFailed();
return nullptr;
}

QHttpMultiPart* multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpMultiPart* multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
file->setParent(multipart);
QHttpPart filePart;
filePart.setBodyDevice(file);
Expand Down
75 changes: 33 additions & 42 deletions launcher/tasks/ConcurrentTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*/
#include "ConcurrentTask.h"

#include <QCoreApplication>
#include <QDebug>
#include "tasks/Task.h"

Expand All @@ -47,9 +46,9 @@ ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concu

ConcurrentTask::~ConcurrentTask()
{
for (auto task : m_queue) {
for (auto task : m_doing) {
if (task)
task->deleteLater();
task->disconnect(this);
}
}

Expand All @@ -65,15 +64,13 @@ void ConcurrentTask::addTask(Task::Ptr task)

void ConcurrentTask::executeTask()
{
// Start one task, startNext handles starting the up to the m_total_max_size
// while tracking the number currently being done
QMetaObject::invokeMethod(this, &ConcurrentTask::startNext, Qt::QueuedConnection);
for (auto i = 0; i < m_total_max_size; i++)
QMetaObject::invokeMethod(this, &ConcurrentTask::executeNextSubTask, Qt::QueuedConnection);
}

bool ConcurrentTask::abort()
{
m_queue.clear();
m_aborted = true;

if (m_doing.isEmpty()) {
// Don't call emitAborted() here, we want to bypass the 'is the task running' check
Expand Down Expand Up @@ -108,29 +105,36 @@ void ConcurrentTask::clear()
m_failed.clear();
m_queue.clear();

m_aborted = false;

m_progress = 0;
m_stepProgress = 0;
}

void ConcurrentTask::startNext()
void ConcurrentTask::executeNextSubTask()
{
if (m_aborted || m_doing.count() > m_total_max_size)
if (!isRunning()) {
return;

if (m_queue.isEmpty() && m_doing.isEmpty() && !wasSuccessful()) {
emitSucceeded();
}
if (m_doing.count() >= m_total_max_size) {
return;
}

if (m_queue.isEmpty())
if (m_queue.isEmpty()) {
if (m_doing.isEmpty()) {
if (m_failed.isEmpty())
emitSucceeded();
else
emitFailed(tr("One or more subtasks failed"));
}
return;
}

Task::Ptr next = m_queue.dequeue();
startSubTask(m_queue.dequeue());
}

void ConcurrentTask::startSubTask(Task::Ptr next)
{
connect(next.get(), &Task::succeeded, this, [this, next]() { subTaskSucceeded(next); });
connect(next.get(), &Task::failed, this, [this, next](QString msg) { subTaskFailed(next, msg); });
// this should never happen but if it does, it's better to fail the task than get stuck
connect(next.get(), &Task::aborted, this, [this, next] { subTaskFailed(next, "Aborted"); });

connect(next.get(), &Task::status, this, [this, next](QString msg) { subTaskStatus(next, msg); });
Expand All @@ -140,55 +144,42 @@ void ConcurrentTask::startNext()
connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total) { subTaskProgress(next, current, total); });

m_doing.insert(next.get(), next);
qsizetype num_starts = qMin(m_queue.size(), m_total_max_size - m_doing.size());

auto task_progress = std::make_shared<TaskStepProgress>(next->getUid());
m_task_progress.insert(next->getUid(), task_progress);

updateState();
updateStepProgress(*task_progress.get(), Operation::ADDED);

QCoreApplication::processEvents();

QMetaObject::invokeMethod(next.get(), &Task::start, Qt::QueuedConnection);

// Allow going up the number of concurrent tasks in case of tasks being added in the middle of a running task.
for (int i = 0; i < num_starts; i++)
QMetaObject::invokeMethod(this, &ConcurrentTask::startNext, Qt::QueuedConnection);
}

void ConcurrentTask::subTaskSucceeded(Task::Ptr task)
void ConcurrentTask::subTaskFinished(Task::Ptr task, TaskStepState state)
{
m_done.insert(task.get(), task);
m_succeeded.insert(task.get(), task);
(state == TaskStepState::Succeeded ? m_succeeded : m_failed).insert(task.get(), task);

m_doing.remove(task.get());

auto task_progress = m_task_progress.value(task->getUid());
task_progress->state = TaskStepState::Succeeded;
task_progress->state = state;

disconnect(task.get(), 0, this, 0);

emit stepProgress(*task_progress);
updateState();
updateStepProgress(*task_progress, Operation::REMOVED);
startNext();
QMetaObject::invokeMethod(this, &ConcurrentTask::executeNextSubTask, Qt::QueuedConnection);
}

void ConcurrentTask::subTaskFailed(Task::Ptr task, [[maybe_unused]] const QString& msg)
void ConcurrentTask::subTaskSucceeded(Task::Ptr task)
{
m_done.insert(task.get(), task);
m_failed.insert(task.get(), task);

m_doing.remove(task.get());

auto task_progress = m_task_progress.value(task->getUid());
task_progress->state = TaskStepState::Failed;

disconnect(task.get(), 0, this, 0);
subTaskFinished(task, TaskStepState::Succeeded);
}

emit stepProgress(*task_progress);
updateState();
updateStepProgress(*task_progress, Operation::REMOVED);
startNext();
void ConcurrentTask::subTaskFailed(Task::Ptr task, [[maybe_unused]] const QString& msg)
{
subTaskFinished(task, TaskStepState::Failed);
}

void ConcurrentTask::subTaskStatus(Task::Ptr task, const QString& msg)
Expand Down