Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,29 @@ Result<void> waitForDBusPeerReady(const QString &service,
{
LINGLONG_TRACE("wait for dbus peer ready");

auto peer = linglong::api::dbus::v1::DBusPeer(service, path, connection);
auto reply = peer.Ping();
reply.waitForFinished();
if (!reply.isValid()) {
return LINGLONG_ERR(reply.error().message().toStdString());
}
using namespace std::chrono_literals;

return LINGLONG_OK;
constexpr auto perCallTimeout = 5s;
constexpr auto retryInterval = 1s;
constexpr auto totalDeadline = 90s;

const auto startedAt = std::chrono::steady_clock::now();
while (true) {
auto peer = linglong::api::dbus::v1::DBusPeer(service, path, connection);
peer.setTimeout(static_cast<int>(std::chrono::milliseconds(perCallTimeout).count()));

auto reply = peer.Ping();
Comment on lines +130 to +141

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Recreating the DBusPeer object and setting its timeout on every single iteration of the loop is inefficient and unnecessary. We can instantiate peer and set its timeout once outside the loop. Additionally, defining perCallTimeout directly in milliseconds (5000ms) simplifies the setTimeout call by avoiding the need for explicit duration conversion.

    using namespace std::chrono_literals;

    constexpr auto perCallTimeout = 5000ms;
    constexpr auto retryInterval = 1s;
    constexpr auto totalDeadline = 90s;

    auto peer = linglong::api::dbus::v1::DBusPeer(service, path, connection);
    peer.setTimeout(static_cast<int>(perCallTimeout.count()));

    const auto startedAt = std::chrono::steady_clock::now();
    while (true) {
        auto reply = peer.Ping();

reply.waitForFinished();
if (reply.isValid()) {
return LINGLONG_OK;
}

if (std::chrono::steady_clock::now() - startedAt >= totalDeadline) {
return LINGLONG_ERR(reply.error().message().toStdString());
}

std::this_thread::sleep_for(retryInterval);
}
}

std::vector<std::string> getAutoModuleList() noexcept
Expand Down Expand Up @@ -967,7 +982,10 @@ Cli::initializeDBusPackageManager()
"/org/deepin/linglong/PackageManager1",
pkgManConn);
if (!peerReady) {
return LINGLONG_ERR("Failed to activate org.deepin.linglong.PackageManager1", peerReady);
return LINGLONG_ERR("Failed to activate org.deepin.linglong.PackageManager1; check the "
"daemon with 'systemctl status org.deepin.linglong.PackageManager' and "
"'journalctl -u org.deepin.linglong.PackageManager -e'",
peerReady);
}

return std::make_unique<api::dbus::v1::PackageManager>("org.deepin.linglong.PackageManager1",
Expand Down
Loading