Skip to content

Commit

Permalink
🐛 add daemon restart policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Reverier-Xu committed Nov 10, 2024
1 parent 450ffc8 commit 7225a7b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
42 changes: 26 additions & 16 deletions desktop/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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, [=]() {
Expand Down
2 changes: 2 additions & 0 deletions desktop/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion desktop/pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QNetworkReply>

#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) {}
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions desktop/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#include "variables.h"

#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLocale>
#include <QMutex>
#include <QMutexLocker>
#include <QNetworkInterface>
#include <QNetworkAccessManager>
#include <QNetworkInterface>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQmlEngine>
Expand All @@ -28,6 +28,8 @@
#include "log.h"
#include "pool.h"

using namespace Qt::StringLiterals;

Ui* Ui::m_instance = nullptr;

#ifdef Q_OS_UNIX
Expand Down Expand Up @@ -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<QQuickWindow*>(m_uiComponent->create());
m_networkManager = new QNetworkAccessManager(this);
setNewVersion("");
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit 7225a7b

Please sign in to comment.