GODT-1671: Implement Quit & Restart mechanism

This commit is contained in:
Romain LE JEUNE
2022-07-28 16:39:56 +02:00
committed by Jakub
parent f44d1c4b9d
commit 22a8aab151
26 changed files with 1255 additions and 701 deletions

View File

@ -34,7 +34,7 @@ QString const exeSuffix = ".exe";
QString const exeSuffix;
#endif
QString const exeName = "bridge" + exeSuffix; ///< The bridge executable file name.
QString const exeName = "proton-bridge" + exeSuffix; ///< The bridge executable file name.*
}
@ -55,9 +55,10 @@ QString BridgeMonitor::locateBridgeExe()
/// \param[in] exePath The path of the Bridge executable.
/// \param[in] parent The parent object of the worker.
//****************************************************************************************************************************************************
BridgeMonitor::BridgeMonitor(QString const &exePath, QObject *parent)
BridgeMonitor::BridgeMonitor(QString const &exePath, QStringList const &args, QObject *parent)
: Worker(parent)
, exePath_(exePath)
, args_(args)
{
QFileInfo fileInfo(exePath);
if (!fileInfo.exists())
@ -77,16 +78,23 @@ void BridgeMonitor::run()
emit started();
QProcess p;
p.start(exePath_, QStringList());
p.start(exePath_, args_);
p.waitForStarted();
status_.running = true;
status_.pid = p.processId();
while (!p.waitForFinished(100))
{
// we discard output from bridge, it's logged to file on bridge side.
p.readAllStandardError();
p.readAllStandardOutput();
}
emit processExited(p.exitCode());
status_.running = false;
status_.returnCode = p.exitCode();
emit processExited(status_.returnCode );
emit finished();
}
catch (Exception const &e)
@ -94,3 +102,11 @@ void BridgeMonitor::run()
emit error(e.qwhat());
}
}
//****************************************************************************************************************************************************
/// \return status of the monitored process
//****************************************************************************************************************************************************
const BridgeMonitor::MonitorStatus& BridgeMonitor::getStatus()
{
return status_;
}

View File

@ -33,8 +33,14 @@ class BridgeMonitor: public bridgepp::Worker
public: // static member functions
static QString locateBridgeExe(); ///< Try to find the bridge executable path.
struct MonitorStatus {
bool running = false;
int returnCode = 0;
qint64 pid = 0;
};
public: // member functions.
BridgeMonitor(QString const& exePath, QObject *parent); ///< Default constructor.
BridgeMonitor(QString const& exePath, QStringList const &args, QObject *parent); ///< Default constructor.
BridgeMonitor(BridgeMonitor const&) = delete; ///< Disabled copy-constructor.
BridgeMonitor(BridgeMonitor&&) = delete; ///< Disabled assignment copy-constructor.
~BridgeMonitor() override = default; ///< Destructor.
@ -42,11 +48,14 @@ public: // member functions.
BridgeMonitor& operator=(BridgeMonitor&&) = delete; ///< Disabled move assignment operator.
void run() override; ///< Run the worker.
const MonitorStatus& getStatus();
signals:
void processExited(int code); ///< Slot for the exiting of the process
void processExited(int code); ///< Slot for the exiting of the process.
private: // data members
QString const exePath_; ///< The path to the bridge executable.
QStringList args_; ///< arguments to be passed to the brigde.
MonitorStatus status_; ///< Status of the monitoring.
};

View File

@ -34,6 +34,7 @@ else()
message(STATUS "Bridge version is ${BRIDGE_APP_VERSION}")
endif()
configure_file(Version.h.in ${CMAKE_SOURCE_DIR}/Version.h)
if (APPLE) # On macOS, we have some Objective-C++ code in DockIcon to deal with the dock icon.
enable_language(OBJC OBJCXX)
endif()
@ -90,7 +91,7 @@ add_executable(bridge-gui
QMLBackend.cpp QMLBackend.h
UserList.cpp UserList.h
${DOCK_ICON_SRC_FILE} DockIcon/DockIcon.h
)
UserDirectories.h)
target_precompile_headers(bridge-gui PRIVATE Pch.h)
target_include_directories(bridge-gui PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -0,0 +1,24 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
#ifndef BRIDGE_GUI_CONFIG_H
#define BRIDGE_GUI_CONFIG_H
#cmakedefine ATTACH_MODE @ATTACH_MODE@
#endif // BRIDGE_GUI_CONFIG_H

View File

@ -83,7 +83,7 @@ void QMLBackend::connectGrpcEvents()
// app events
connect(client, &GRPCClient::internetStatus, this, [&](bool isOn) { if (isOn) emit internetOn(); else emit internetOff(); });
connect(client, &GRPCClient::toggleAutostartFinished, this, &QMLBackend::toggleAutostartFinished);
connect(client, &GRPCClient::resetFinished, this, &QMLBackend::resetFinished);
connect(client, &GRPCClient::resetFinished, this, &QMLBackend::onResetFinished);
connect(client, &GRPCClient::reportBugFinished, this, &QMLBackend::reportBugFinished);
connect(client, &GRPCClient::reportBugSuccess, this, &QMLBackend::bugReportSendSuccess);
connect(client, &GRPCClient::reportBugError, this, &QMLBackend::bugReportSendError);
@ -220,7 +220,12 @@ void QMLBackend::quit()
void QMLBackend::restart()
{
app().grpc().restart();
app().log().error("RESTART is not implemented"); /// \todo GODT-1671 implement restart.
app().grpc().quit();
}
void QMLBackend::forceLauncher(QString launcher)
{
app().grpc().forceLauncher(launcher);
}
@ -333,3 +338,12 @@ void QMLBackend::triggerReset()
{
app().grpc().triggerReset();
}
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
void QMLBackend::onResetFinished()
{
emit resetFinished();
this->restart();
}

View File

@ -150,11 +150,13 @@ public slots: // slot for signals received from QML -> To be forwarded to Bridge
void guiReady(); // _ func() `slot:"guiReady"`
void quit(); // _ func() `slot:"quit"`
void restart(); // _ func() `slot:"restart"`
void forceLauncher(QString launcher); // _ func() `slot:"forceLauncher"`
void checkUpdates(); // _ func() `slot:"checkUpdates"`
void installUpdate(); // _ func() `slot:"installUpdate"`
void triggerReset(); // _ func() `slot:"triggerReset"`
void reportBug(QString const &description, QString const& address, QString const &emailClient, bool includeLogs) {
app().grpc().reportBug(description, address, emailClient, includeLogs); } // _ func(description, address, emailClient string, includeLogs bool) `slot:"reportBug"`
void onResetFinished();
signals: // Signals received from the Go backend, to be forwarded to QML
void toggleAutostartFinished(); // _ func() `signal:"toggleAutostartFinished"`

View File

@ -0,0 +1,79 @@
//
// Created by romain on 01/08/22.
//
#ifndef PROTON_BRIDGE_GUI_USERDIRECTORIES_H
#define PROTON_BRIDGE_GUI_USERDIRECTORIES_H
#include <bridgepp/Exception/Exception.h>
using namespace bridgepp;
namespace UserDirectories {
QString const configFolder = "protonmail/bridge";
//****************************************************************************************************************************************************
/// \return user configuration directory used by bridge (based on Golang OS/File's UserConfigDir).
//****************************************************************************************************************************************************
static const QString UserConfigDir()
{
QString dir;
#ifdef Q_OS_WIN
dir = qgetenv ("AppData");
if (dir.isEmpty())
throw Exception("%AppData% is not defined.");
#elif defined(Q_OS_IOS) || defined(Q_OS_DARWIN)
dir = qgetenv ("HOME");
if (dir.isEmpty())
throw Exception("$HOME is not defined.");
dir += "/Library/Application Support";
#else
dir = qgetenv ("XDG_CONFIG_HOME");
if (dir.isEmpty())
dir = qgetenv ("HOME");
if (dir.isEmpty())
throw Exception("neither $XDG_CONFIG_HOME nor $HOME are defined");
dir += "/.config";
#endif
QString folder = dir + "/" + configFolder;
QDir().mkpath(folder);
return folder;
}
//****************************************************************************************************************************************************
/// \return user configuration directory used by bridge (based on Golang OS/File's UserCacheDir).
//****************************************************************************************************************************************************
static const QString UserCacheDir()
{
QString dir;
#ifdef Q_OS_WIN
dir = qgetenv ("LocalAppData");
if (dir.isEmpty())
throw Exception("%LocalAppData% is not defined.");
#elif defined(Q_OS_IOS) || defined(Q_OS_DARWIN)
dir = qgetenv ("HOME");
if (dir.isEmpty())
throw Exception("$HOME is not defined.");
dir += "/Library/Caches";
#else
dir = qgetenv ("XDG_CACHE_HOME");
if (dir.isEmpty())
dir = qgetenv ("HOME");
if (dir.isEmpty())
throw Exception("neither XDG_CACHE_HOME nor $HOME are defined");
dir += "/.cache";
#endif
QString folder = dir + "/" + configFolder;
QDir().mkpath(folder);
return folder;
}
};
#endif //PROTON_BRIDGE_GUI_USERDIRECTORIES_H

View File

@ -19,6 +19,7 @@
#include "QMLBackend.h"
#include "BridgeMonitor.h"
#include "Version.h"
#include "UserDirectories.h"
#include <bridgepp/Log/Log.h>
#include <bridgepp/Exception/Exception.h>
@ -26,6 +27,13 @@
using namespace bridgepp;
namespace
{
QString const launcherFlag = "--launcher"; ///< launcher flag parameter used for bridge.
QString const bridgeLock = "bridge-gui.lock"; ///< file name used for the lock file.
}
//****************************************************************************************************************************************************
/// // initialize the Qt application.
//****************************************************************************************************************************************************
@ -84,56 +92,94 @@ QQmlComponent *createRootQmlComponent(QQmlApplicationEngine &engine)
//****************************************************************************************************************************************************
/// \param[in] exePath The path of the Bridge executable. If empty, the function will try to locate the bridge application.
/// \param[in] lock The lock file to be checked.
/// \return True if the lock can be taken, false otherwize.
//****************************************************************************************************************************************************
void launchBridge(QString const &exePath)
bool checkSingleInstance(QLockFile &lock)
{
lock.setStaleLockTime(0);
if (!lock.tryLock())
{
qint64 pid;
QString hostname, appName, details;
if (lock.getLockInfo(&pid, &hostname, &appName))
details = QString("(PID : %1 - Host : %2 - App : %3)").arg(pid).arg(hostname, appName);
app().log().error(QString("Instance already exists %1 %2").arg(lock.fileName(), details));
return false;
}
else
{
app().log().info(QString("lock file created %1").arg(lock.fileName()));
}
return true;
}
//****************************************************************************************************************************************************
/// \param [in] argc number of arguments passed to the application.
/// \param [in] argv list of arguments passed to the application.
/// \param [out] args list of arguments passed to the application as a QStringList.
/// \param [out] launcher launcher used in argument, forced to self application if not specify.
/// \param[out] outAttach The value for the 'attach' command-line parameter.
//****************************************************************************************************************************************************
void parseArguments(int argc, char *argv[], QStringList& args, QString& launcher, bool &outAttach) {
bool flagFound = false;
launcher = QString::fromLocal8Bit(argv[0]);
// for unknown reasons, on Windows QCoreApplication::arguments() frequently returns an empty list, which is incorrect, so we rebuild the argument
// list from the original argc and argv values.
for (int i = 1; i < argc; i++) {
QString const &arg = QString::fromLocal8Bit(argv[i]);
// we can't use QCommandLineParser here since it will fails on unknown options.
// Arguments may contain some bridge flags.
if (arg == launcherFlag)
{
args.append(arg);
launcher = QString::fromLocal8Bit(argv[++i]);
args.append(launcher);
flagFound = true;
}
#ifdef QT_DEBUG
else if (arg == "--attach" || arg == "-a")
{
// we don't keep the attach mode within the args since we don't need it for Bridge.
outAttach = true;
}
#endif
else
{
args.append(arg);
}
}
if (!flagFound)
{
// add bridge-gui as launcher
args.append(launcherFlag);
args.append(launcher);
}
}
//****************************************************************************************************************************************************
/// \param [in] args list of arguments to pass to bridge.
//****************************************************************************************************************************************************
void launchBridge(QStringList const &args)
{
UPOverseer& overseer = app().bridgeOverseer();
overseer.reset();
QString bridgeExePath = exePath;
if (exePath.isEmpty())
bridgeExePath = BridgeMonitor::locateBridgeExe();
const QString bridgeExePath = BridgeMonitor::locateBridgeExe();
if (bridgeExePath.isEmpty())
throw Exception("Could not locate the bridge executable path");
else
app().log().debug(QString("Bridge executable path: %1").arg(QDir::toNativeSeparators(bridgeExePath)));
overseer = std::make_unique<Overseer>(new BridgeMonitor(bridgeExePath, nullptr), nullptr);
overseer = std::make_unique<Overseer>(new BridgeMonitor(bridgeExePath, args, nullptr), nullptr);
overseer->startWorker(true);
}
//****************************************************************************************************************************************************
/// \param[in] argc The number of command-line arguments.
/// \param[in] argv The list of command line arguments.
/// \param[out] outAttach The value for the 'attach' command-line parameter.
/// \param[out] outExePath The value for the 'bridge-exe-path' command-line parameter.
//****************************************************************************************************************************************************
void parseArguments(int argc, char **argv, bool &outAttach, QString &outExePath)
{
// for unknown reasons, on Windows QCoreApplication::arguments() frequently returns an empty list, which is incorrect, so we rebuild the argument
// list from the original argc and argv values.
QStringList args;
for (int i = 0; i < argc; i++)
args.append(QString::fromLocal8Bit(argv[i]));
// We do not want to 'advertise' the following switches, so we do not offer a '-h/--help' option.
// we have not yet connected to Bridge, we do not know the application version number, so we do not offer a -v/--version switch.
QCommandLineParser parser;
parser.setApplicationDescription("Proton Mail Bridge");
QCommandLineOption attachOption(QStringList() << "attach" << "a", "attach to an existing bridge process");
parser.addOption(attachOption);
QCommandLineOption exePathOption(QStringList() << "bridge-exe-path" << "b", "bridge executable path", "path", QString());
parser.addOption(exePathOption);
parser.process(args);
outAttach = parser.isSet(attachOption);
outExePath = parser.value(exePathOption);
}
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
@ -166,14 +212,19 @@ int main(int argc, char *argv[])
QGuiApplication guiApp(argc, argv);
initQtApplication();
bool attach = false;
QString exePath;
parseArguments(argc, argv, attach, exePath);
Log &log = initLog();
QLockFile lock(UserDirectories::UserCacheDir() + "/" + bridgeLock);
if (!checkSingleInstance(lock))
return EXIT_FAILURE;
QStringList args;
QString launcher;
bool attach = false;
parseArguments(argc, argv, args, launcher, attach);
if (!attach)
launchBridge(exePath);
launchBridge(args);
app().backend().init();
@ -185,15 +236,34 @@ int main(int argc, char *argv[])
BridgeMonitor *bridgeMonitor = app().bridgeMonitor();
bool bridgeExited = false;
bool startError = false;
QMetaObject::Connection connection;
if (bridgeMonitor)
connection = QObject::connect(bridgeMonitor, &BridgeMonitor::processExited, [&](int returnCode) {
// GODT-1671 We need to find a 'safe' way to check if Bridge crashed and restart instead of just quitting. Is returnCode enough?
bridgeExited = true;// clazy:exclude=lambda-in-connect
qGuiApp->exit(returnCode);
});
{
const BridgeMonitor::MonitorStatus& status = bridgeMonitor->getStatus();
if (!status.running && !attach)
{
// BridgeMonitor already stopped meaning we are attached to an orphan Bridge.
// Restart the full process to be sure there is no more bridge orphans
app().log().error("Found orphan bridge, need to restart.");
app().backend().forceLauncher(launcher);
app().backend().restart();
bridgeExited = true;
startError = true;
}
else
{
app().log().debug(QString("Monitoring Bridge PID : %1").arg(status.pid));
connection = QObject::connect(bridgeMonitor, &BridgeMonitor::processExited, [&](int returnCode) {
bridgeExited = true;// clazy:exclude=lambda-in-connect
qGuiApp->exit(returnCode);
});
}
}
int const result = QGuiApplication::exec();
int result = 0;
if (!startError)
result = QGuiApplication::exec();
QObject::disconnect(connection);
app().grpc().stopEventStream();
@ -204,7 +274,8 @@ int main(int argc, char *argv[])
if (!bridgeExited)
closeBridgeApp();
// release the lock file
lock.unlock();
return result;
}
catch (Exception const &e)

View File

@ -193,7 +193,7 @@ QtObject {
onTriggered: {
Qt.openUrlExternally(Backend.landingPageLink)
root.updateManualError.active = false
root.backend.quit()
Backend.quit()
}
},
Action {