From 8f78d1421581c2f7953a589909b149f352e3a971 Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 20 Jun 2024 01:22:45 +0200 Subject: [PATCH] Fix re-connect when process status is considered and Syncthing restarted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When Syncthing restarts itself, its main process is not terminating (as its main process is just a monitoring process). The "Consider process status …" feature was relying on the process being stopped and started again as trigger for trying to re-connect, though. To make it also work when Syncthing restarts itself, this change checks the log for an exiting message of the monitoring process and considers the process running again when the GUI address is logged again. This fixes https://github.com/Martchus/syncthingtray/issues/236. Notifications are not (yet) suppressed. (Not sure how easy it is and how much sense it makes.) --- syncthingwidgets/misc/syncthinglauncher.cpp | 18 +++++++++++++++++- syncthingwidgets/misc/syncthinglauncher.h | 5 ++++- syncthingwidgets/settings/settings.cpp | 2 +- tray/gui/traywidget.cpp | 11 +++++++++++ tray/gui/traywidget.h | 1 + 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/syncthingwidgets/misc/syncthinglauncher.cpp b/syncthingwidgets/misc/syncthinglauncher.cpp index 29a40cd8..d2d5dcb9 100644 --- a/syncthingwidgets/misc/syncthinglauncher.cpp +++ b/syncthingwidgets/misc/syncthinglauncher.cpp @@ -45,6 +45,8 @@ SyncthingLauncher::SyncthingLauncher(QObject *parent) , m_relevantConnection(nullptr) , m_guiListeningUrlSearch("Access the GUI via the following URL: ", "\n\r", std::string_view(), std::bind(&SyncthingLauncher::handleGuiListeningUrlFound, this, std::placeholders::_1, std::placeholders::_2)) + , m_exitSearch("Syncthing exited: ", "\n\r", std::string_view(), + std::bind(&SyncthingLauncher::handleExitFound, this, std::placeholders::_1, std::placeholders::_2)) #ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING , m_libsyncthingLogLevel(LibSyncthing::LogLevel::Info) #endif @@ -332,6 +334,7 @@ void SyncthingLauncher::handleLoggingCallback(LibSyncthing::LogLevel level, cons void SyncthingLauncher::handleOutputAvailable(QByteArray &&data) { m_guiListeningUrlSearch(data.data(), static_cast(data.size())); + m_exitSearch(data.data(), static_cast(data.size())); if (isEmittingOutput()) { emit outputAvailable(data); } else { @@ -339,12 +342,23 @@ void SyncthingLauncher::handleOutputAvailable(QByteArray &&data) } } -void SyncthingLauncher::handleGuiListeningUrlFound(CppUtilities::BufferSearch &, std::string &&searchResult) +void SyncthingLauncher::handleGuiListeningUrlFound(CppUtilities::BufferSearch &search, std::string &&searchResult) { m_guiListeningUrl.setUrl(QString::fromStdString(searchResult)); + std::cerr << EscapeCodes::Phrases::Info << "Syncthing GUI available: " << searchResult << EscapeCodes::Phrases::End; + search.reset(); emit guiUrlChanged(m_guiListeningUrl); } +void SyncthingLauncher::handleExitFound(CppUtilities::BufferSearch &search, std::string &&searchResult) +{ + m_guiListeningUrl.clear(); + std::cerr << EscapeCodes::Phrases::Info << "Syncthing exited: " << searchResult << EscapeCodes::Phrases::End; + emit guiUrlChanged(m_guiListeningUrl); + emit exitLogged(std::move(searchResult)); + search.reset(); +} + void SyncthingLauncher::terminateDueToMeteredConnection() { if (!isRunning()) { @@ -366,6 +380,8 @@ void SyncthingLauncher::runLibSyncthing(const LibSyncthing::RuntimeOptions &runt LibSyncthing::setLoggingCallback(bind(&SyncthingLauncher::handleLoggingCallback, this, _1, _2, _3)); emit runningChanged(true); const auto exitCode = LibSyncthing::runSyncthing(runtimeOptions); + m_guiListeningUrl.clear(); + emit guiUrlChanged(m_guiListeningUrl); emit exited(static_cast(exitCode), exitCode == 0 ? QProcess::NormalExit : QProcess::CrashExit); emit runningChanged(false); } diff --git a/syncthingwidgets/misc/syncthinglauncher.h b/syncthingwidgets/misc/syncthinglauncher.h index 94b361b6..1166ab77 100644 --- a/syncthingwidgets/misc/syncthinglauncher.h +++ b/syncthingwidgets/misc/syncthinglauncher.h @@ -70,6 +70,7 @@ class SYNCTHINGWIDGETS_EXPORT SyncthingLauncher : public QObject { void confirmKill(); void runningChanged(bool isRunning); void outputAvailable(const QByteArray &data); + void exitLogged(const std::string &exitMessage); void exited(int exitCode, QProcess::ExitStatus exitStatus); void errorOccurred(QProcess::ProcessError error); void guiUrlChanged(const QUrl &newUrl); @@ -99,7 +100,8 @@ private Q_SLOTS: void handleLoggingCallback(LibSyncthing::LogLevel, const char *message, std::size_t messageSize); #endif void handleOutputAvailable(QByteArray &&data); - void handleGuiListeningUrlFound(CppUtilities::BufferSearch &bufferSearch, std::string &&searchResult); + void handleGuiListeningUrlFound(CppUtilities::BufferSearch &search, std::string &&searchResult); + void handleExitFound(CppUtilities::BufferSearch &search, std::string &&searchResult); void terminateDueToMeteredConnection(); SyncthingProcess m_process; @@ -110,6 +112,7 @@ private Q_SLOTS: QFuture m_stopFuture; QByteArray m_outputBuffer; CppUtilities::BufferSearch m_guiListeningUrlSearch; + CppUtilities::BufferSearch m_exitSearch; CppUtilities::DateTime m_futureStarted; #ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING LibSyncthing::LogLevel m_libsyncthingLogLevel; diff --git a/syncthingwidgets/settings/settings.cpp b/syncthingwidgets/settings/settings.cpp index 279846cc..2d7618a4 100644 --- a/syncthingwidgets/settings/settings.cpp +++ b/syncthingwidgets/settings/settings.cpp @@ -238,7 +238,7 @@ Launcher::LauncherStatus Launcher::apply( return LauncherStatus{}; } const auto isRelevant = connection.isLocal(); - const auto isRunning = launcher->isRunning(); + const auto isRunning = launcher->isRunning() && !launcher->guiUrl().isEmpty(); const auto consideredForReconnect = considerForReconnect && isRelevant; connectAccordingToSettings( connection, currentConnectionSettings, *launcher, reconnectRequired, considerForReconnect, isRelevant, isRunning, consideredForReconnect); diff --git a/tray/gui/traywidget.cpp b/tray/gui/traywidget.cpp index e044c0f1..e975c4fc 100644 --- a/tray/gui/traywidget.cpp +++ b/tray/gui/traywidget.cpp @@ -222,6 +222,7 @@ TrayWidget::TrayWidget(TrayMenu *parent) connect(m_ui->startStopPushButton, &QPushButton::clicked, this, &TrayWidget::toggleRunning); if (const auto *const launcher = SyncthingLauncher::mainInstance()) { connect(launcher, &SyncthingLauncher::runningChanged, this, &TrayWidget::handleLauncherStatusChanged); + connect(launcher, &SyncthingLauncher::guiUrlChanged, this, &TrayWidget::handleLauncherGuiAddressChanged); } #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD if (const auto *const service = SyncthingService::mainInstance()) { @@ -847,6 +848,16 @@ Settings::Launcher::LauncherStatus TrayWidget::handleLauncherStatusChanged() return launcherStatus; } +Settings::Launcher::LauncherStatus TrayWidget::handleLauncherGuiAddressChanged(const QUrl &guiAddress) +{ + Q_UNUSED(guiAddress) +#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD + return applyLauncherSettings(false, Settings::values().systemd.considerForReconnect, true); +#else + return applyLauncherSettings(false); +#endif +} + Settings::Launcher::LauncherStatus TrayWidget::applyLauncherSettings(bool reconnectRequired, bool skipApplyingToConnection, bool skipStartStopButton) { // update connection diff --git a/tray/gui/traywidget.h b/tray/gui/traywidget.h index f6b55347..6256a53b 100644 --- a/tray/gui/traywidget.h +++ b/tray/gui/traywidget.h @@ -105,6 +105,7 @@ private Q_SLOTS: void updateIconAndTooltip(); void toggleRunning(); Settings::Launcher::LauncherStatus handleLauncherStatusChanged(); + Settings::Launcher::LauncherStatus handleLauncherGuiAddressChanged(const QUrl &guiAddress); Settings::Launcher::LauncherStatus applyLauncherSettings( bool reconnectRequired = false, bool skipApplyingToConnection = false, bool skipStartStopButton = false); #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD