GODT-2205: use lock file in bridge-gui to detect orphan bridge.

This commit is contained in:
Xavier Michelon
2023-01-10 18:18:05 +01:00
parent 761b98f02f
commit 0f0f8b3461

View File

@ -50,7 +50,9 @@ QString const exeSuffix = ".exe";
QString const exeSuffix; QString const exeSuffix;
#endif #endif
QString const bridgeLock = "bridge-v3-gui.lock"; ///< file name used for the lock file.
QString const bridgeLock = "bridge-v3.lock"; ///< The file name used for the bridge-gui lock file.
QString const bridgeGUILock = "bridge-v3-gui.lock"; ///< The file name used for the bridge-gui lock file.
QString const exeName = "bridge" + exeSuffix; ///< The bridge executable file name.* QString const exeName = "bridge" + exeSuffix; ///< The bridge executable file name.*
qint64 const grpcServiceConfigWaitDelayMs = 180000; ///< The wait delay for the gRPC config file in milliseconds. qint64 const grpcServiceConfigWaitDelayMs = 180000; ///< The wait delay for the gRPC config file in milliseconds.
@ -205,21 +207,17 @@ QUrl getApiUrl() {
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
/// \return The URL for the focus endpoint of the bridge API URL. /// \brief Check if bridge is running.
//**************************************************************************************************************************************************** ///
QUrl getFocusUrl() { /// The check is performed by trying to create a lock file for bridge. Priority is to avoid false positive, so we only return true if the locking
QUrl url = getApiUrl(); /// attempt failed because the file is locked. Any other error (PermissionError, UnknownError) do not lead to considering bridge is running.
url.setPath("/focus"); /// \note QLockFile removes the lock file in it destructor. So it's removed on function exit if acquired.
return url; ///
}
//****************************************************************************************************************************************************
/// \return true if an instance of bridge is already running. /// \return true if an instance of bridge is already running.
//**************************************************************************************************************************************************** //****************************************************************************************************************************************************
bool isBridgeRunning() { bool isBridgeRunning() {
FocusGRPCClient client; QLockFile lockFile(QDir(userCacheDir()).absoluteFilePath(bridgeLock));
return client.connectToServer(500); // we time out after 1 second and consider no other instance is running; return (!lockFile.tryLock()) && (lockFile.error() == QLockFile::LockFailedError);
} }
@ -319,7 +317,7 @@ int main(int argc, char *argv[]) {
Log &log = initLog(); Log &log = initLog();
QLockFile lock(bridgepp::userCacheDir() + "/" + bridgeLock); QLockFile lock(bridgepp::userCacheDir() + "/" + bridgeGUILock);
if (!checkSingleInstance(lock)) { if (!checkSingleInstance(lock)) {
focusOtherInstance(); focusOtherInstance();
return EXIT_FAILURE; return EXIT_FAILURE;