From 7225a7b7198f6da23d824986fd47a1aef80d7a04 Mon Sep 17 00:00:00 2001 From: Reverier-Xu Date: Mon, 11 Nov 2024 01:14:04 +0800 Subject: [PATCH] :bug: add daemon restart policy --- desktop/daemon.cc | 42 ++++++++++++++++++++++++++---------------- desktop/daemon.h | 2 ++ desktop/pool.cc | 3 ++- desktop/ui.cc | 15 ++++++++------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/desktop/daemon.cc b/desktop/daemon.cc index e694af7..1bb209e 100644 --- a/desktop/daemon.cc +++ b/desktop/daemon.cc @@ -30,27 +30,14 @@ quint16 getAvailablePort(quint16 prefered) { } Daemon::Daemon(QObject* parent) : QObject(parent) { - refreshAvailableAddresses(); - auto daemon_path = QCoreApplication::applicationDirPath() + "/wsrx"; -#ifdef Q_OS_WIN - daemon_path += ".exe"; -#endif + m_logs = new LogList(this); m_links = new LinkList(this); - setApiPort(getAvailablePort(apiPort())); + m_links->setLogs(m_logs); - auto args = QStringList{"daemon", "-l", "true", "-p", QString::asprintf("%d", apiPort()), "--heartbeat", "3"}; m_daemon = new QProcess(this); - m_daemon->start(daemon_path, args); - if (!m_daemon->waitForStarted()) { - qWarning() << "Daemon is not started correctly."; - m_logs->appendLog( - Log(QDateTime::currentDateTime().toString(Qt::ISODate), EventLevel::ERROR, - tr("Failed to start daemon: ") + m_daemon->errorString() + " " + m_daemon->readAllStandardError(), - "wsrx::desktop::connector")); - } - m_links->setLogs(m_logs); + m_refreshTimer = new QTimer(this); m_refreshTimer->setInterval(30 * 1000); m_refreshTimer->start(); @@ -75,6 +62,24 @@ Daemon::Daemon(QObject* parent) : QObject(parent) { m_network = new QNetworkAccessManager(this); } +void Daemon::launch() { + auto daemon_path = QCoreApplication::applicationDirPath() + "/wsrx"; +#ifdef Q_OS_WIN + daemon_path += ".exe"; +#endif + setApiPort(getAvailablePort(apiPort())); + auto args = QStringList{"daemon", "-l", "true", "-p", QString::asprintf("%d", apiPort()), "--heartbeat", "3"}; + m_daemon->start(daemon_path, args); + if (!m_daemon->waitForStarted()) { + qWarning() << "Daemon is not started correctly."; + m_logs->appendLog( + Log(QDateTime::currentDateTime().toString(Qt::ISODate), EventLevel::ERROR, + tr("Failed to start daemon: ") + m_daemon->errorString() + " " + m_daemon->readAllStandardError(), + "wsrx::desktop::connector")); + } + refreshAvailableAddresses(); +} + Daemon::~Daemon() { #ifdef Q_OS_WIN m_daemon->kill(); @@ -221,6 +226,11 @@ void Daemon::syncPool() { } void Daemon::heartbeat() { + if (m_daemon->state() != QProcess::Running) { + qWarning() << "Daemon is not running, try restart it."; + launch(); + return; + } auto request = QNetworkRequest(service("heartbeat")); auto reply = m_network->get(request); connect(reply, &QNetworkReply::finished, this, [=]() { diff --git a/desktop/daemon.h b/desktop/daemon.h index 3d037ef..27a18f7 100644 --- a/desktop/daemon.h +++ b/desktop/daemon.h @@ -56,6 +56,8 @@ class Daemon : public QObject { QUrl service(const QString& name) const; + void launch(); + public slots: Q_INVOKABLE void exportLogs(const QUrl& path) const; diff --git a/desktop/pool.cc b/desktop/pool.cc index 8939e43..3089807 100644 --- a/desktop/pool.cc +++ b/desktop/pool.cc @@ -8,6 +8,7 @@ #include #include "log.h" +using namespace Qt::StringLiterals; Link::Link(const QString& from, const QString& to, LinkStatus status, quint32 latency) : m_from(from), m_to(to), m_status(status), m_latency(latency) {} @@ -202,7 +203,7 @@ void LinkList::refreshStatus() { if (reply->error() != QNetworkReply::NoError) { if (m_logs) { m_logs->appendLog(Log(QDateTime::currentDateTimeUtc().toString(Qt::ISODate), EventLevel::ERROR, - reply->errorString(), u"wsrx::desktop::pool"_qs)); + reply->errorString(), u"wsrx::desktop::pool"_s)); } setData(index(i), LinkStatus::DEAD, StatusRole); } else { diff --git a/desktop/ui.cc b/desktop/ui.cc index 81f091a..72d55be 100644 --- a/desktop/ui.cc +++ b/desktop/ui.cc @@ -2,14 +2,14 @@ #include "variables.h" #include +#include +#include #include #include #include -#include #include +#include #include -#include -#include #include #include #include @@ -28,6 +28,8 @@ #include "log.h" #include "pool.h" +using namespace Qt::StringLiterals; + Ui* Ui::m_instance = nullptr; #ifdef Q_OS_UNIX @@ -82,7 +84,7 @@ Ui::Ui(QObject* parent) : QObject(parent) { m_uiEngine->rootContext()->setContextProperty("websites", m_websites); m_uiEngine->retranslate(); m_uiComponent = new QQmlComponent(m_uiEngine, this); - m_uiComponent->loadUrl(QUrl(u"qrc:/ui/Main.qml"_qs)); + m_uiComponent->loadUrl(QUrl(u"qrc:/ui/Main.qml"_s)); m_window = qobject_cast(m_uiComponent->create()); m_networkManager = new QNetworkAccessManager(this); setNewVersion(""); @@ -131,13 +133,12 @@ Q_INVOKABLE void Ui::onSecondaryInstanceMessageReceived(quint32 instanceId, cons m_daemon->requestConnect(link, "127.0.0.1", 0); } -Q_INVOKABLE void Ui::onSecondaryInstanceStarted() { - m_window->show(); -} +Q_INVOKABLE void Ui::onSecondaryInstanceStarted() { m_window->show(); } void Ui::show() { if (m_uiComponent->isError()) qWarning() << m_uiComponent->errors(); m_window->show(); + m_daemon->launch(); } bool Ui::runningInTray() const { return m_runningInTray; }