Skip to content

Commit

Permalink
Fix re-connect when process status is considered and Syncthing restarted
Browse files Browse the repository at this point in the history
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 #236.

Notifications are not (yet) suppressed. (Not sure how easy it is and how
much sense it makes.)
  • Loading branch information
Martchus committed Jun 19, 2024
1 parent ce92ccf commit 8f78d14
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
18 changes: 17 additions & 1 deletion syncthingwidgets/misc/syncthinglauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -332,19 +334,31 @@ void SyncthingLauncher::handleLoggingCallback(LibSyncthing::LogLevel level, cons
void SyncthingLauncher::handleOutputAvailable(QByteArray &&data)
{
m_guiListeningUrlSearch(data.data(), static_cast<std::size_t>(data.size()));
m_exitSearch(data.data(), static_cast<std::size_t>(data.size()));
if (isEmittingOutput()) {
emit outputAvailable(data);
} else {
m_outputBuffer += 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()) {
Expand All @@ -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<int>(exitCode), exitCode == 0 ? QProcess::NormalExit : QProcess::CrashExit);
emit runningChanged(false);
}
Expand Down
5 changes: 4 additions & 1 deletion syncthingwidgets/misc/syncthinglauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -110,6 +112,7 @@ private Q_SLOTS:
QFuture<void> 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;
Expand Down
2 changes: 1 addition & 1 deletion syncthingwidgets/settings/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tray/gui/traywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tray/gui/traywidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f78d14

Please sign in to comment.