Skip to content

Commit

Permalink
fix: Change the way to check if the next task should be started
Browse files Browse the repository at this point in the history
Signed-off-by: ComixHe <[email protected]>
  • Loading branch information
ComixHe authored and dengbo11 committed Jan 8, 2025
1 parent 54330be commit f8aa524
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
40 changes: 12 additions & 28 deletions libs/linglong/src/linglong/package_manager/package_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,37 +176,25 @@ void PackageTask::reportError(linglong::utils::error::Error &&err) noexcept

void PackageTask::Cancel() noexcept
{
if (g_cancellable_is_cancelled(m_cancelFlag) == TRUE) {
if (m_state == static_cast<int>(linglong::api::types::v1::State::Canceled)) {
return;
}

qInfo() << "task " << taskID() << "has been canceled by user";
g_cancellable_cancel(m_cancelFlag);

const auto &id = taskID();
auto oldState = state();
qInfo() << "task " << id << "has been canceled by user";
updateState(linglong::api::types::v1::State::Canceled,
QString{ "task %1 has been canceled by user" }.arg(id));
if (oldState == linglong::api::types::v1::State::Queued) {
auto *ptr = parent();
if (ptr == nullptr) { // temporary task
return;
}

Q_EMIT qobject_cast<PackageTaskQueue *>(ptr)->taskDone(id);
if (m_cancelFlag == nullptr || g_cancellable_is_cancelled(m_cancelFlag) == TRUE) {
return;
}

g_cancellable_cancel(m_cancelFlag);
}

utils::error::Result<void> PackageTask::run() noexcept
void PackageTask::run() noexcept
{
LINGLONG_TRACE("run task");
if (m_state != static_cast<int>(linglong::api::types::v1::State::Queued)) {
qInfo() << "task" << taskID() << " is not in queued state" << static_cast<int>(state());
return LINGLONG_ERR("task is not in queued state");
}

m_job(*this);
return LINGLONG_OK;
}

PackageTaskQueue::PackageTaskQueue(QObject *parent)
Expand All @@ -221,17 +209,13 @@ PackageTaskQueue::PackageTaskQueue(QObject *parent)
return;
}

if (m_taskQueue.size() > 1) {
auto &task = m_taskQueue.front();
if (task.state() != linglong::api::types::v1::State::Queued) {
qDebug() << "other task is running, wait for it done";
return;
}

auto &task = m_taskQueue.front();
auto ret = task.run();
if (!ret) {
qWarning() << ret.error();
}

task.run();
Q_EMIT taskDone(task.taskID());
},
Qt::QueuedConnection);
Expand All @@ -250,7 +234,7 @@ PackageTaskQueue::PackageTaskQueue(QObject *parent)

// if queued task is done, only remove it from queue
// otherwise, remove it and start next task
bool isQueuedDone = task == m_taskQueue.begin();
bool isQueuedDone = task->state() == linglong::api::types::v1::State::Queued;
Q_EMIT qobject_cast<PackageManager *>(this->parent())
->TaskRemoved(QDBusObjectPath{ task->taskObjectPath() },
static_cast<int>(task->state()),
Expand All @@ -259,7 +243,7 @@ PackageTaskQueue::PackageTaskQueue(QObject *parent)
task->getPercentage());
m_taskQueue.erase(task);

if (isQueuedDone) {
if (!isQueuedDone) {
Q_EMIT startTask();
}
});
Expand Down
2 changes: 1 addition & 1 deletion libs/linglong/src/linglong/package_manager/package_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class PackageTask : public QObject, protected QDBusContext

auto cancellable() noexcept { return m_cancelFlag; }

utils::error::Result<void> run() noexcept;
void run() noexcept;

[[nodiscard]] double getPercentage() const noexcept
{
Expand Down

0 comments on commit f8aa524

Please sign in to comment.