From 55ff2b03528be38718b109b60573861df66c956f Mon Sep 17 00:00:00 2001 From: Manu Date: Wed, 21 Nov 2018 22:17:48 +0800 Subject: [PATCH] Ensure only one app instance is running. Fixes #27 --- src/vorta/__main__.py | 2 +- src/vorta/application.py | 21 ++++++++++++++++++++- tests/test_repo.py | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/vorta/__main__.py b/src/vorta/__main__.py index b8913b82..3d5466d2 100644 --- a/src/vorta/__main__.py +++ b/src/vorta/__main__.py @@ -20,7 +20,7 @@ def main(): sqlite_db = peewee.SqliteDatabase(os.path.join(SETTINGS_DIR, 'settings.db')) vorta.models.init_db(sqlite_db) - app = VortaApp(sys.argv) + app = VortaApp(sys.argv, single_app=True) app.updater = vorta.updater.get_updater() sys.exit(app.exec_()) diff --git a/src/vorta/application.py b/src/vorta/application.py index 8194e8c6..f0002610 100644 --- a/src/vorta/application.py +++ b/src/vorta/application.py @@ -1,3 +1,7 @@ +import os +import sys +import fcntl + from PyQt5 import QtCore from PyQt5.QtWidgets import QApplication from PyQt5.QtGui import QIcon @@ -8,6 +12,7 @@ from .borg.create import BorgCreateThread from .views.main_window import MainWindow from .utils import get_asset +from vorta.config import SETTINGS_DIR class VortaApp(QApplication): @@ -23,7 +28,21 @@ class VortaApp(QApplication): backup_cancelled_event = QtCore.pyqtSignal() backup_log_event = QtCore.pyqtSignal(str) - def __init__(self, args): + def __init__(self, args, single_app=False): + + # Ensure only one app instance is running. + # From https://stackoverflow.com/questions/220525/ + # ensure-a-single-instance-of-an-application-in-linux#221159 + if single_app: + pid_file = os.path.join(SETTINGS_DIR, 'vorta.pid') + lockfile = open(pid_file, 'w+') + try: + fcntl.lockf(lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) + self.lockfile = lockfile + except OSError: + print('An instance of Vorta is already running.') + sys.exit(1) + super().__init__(args) self.setQuitOnLastWindowClosed(False) self.scheduler = VortaScheduler(self) diff --git a/tests/test_repo.py b/tests/test_repo.py index 13527e76..3e226063 100644 --- a/tests/test_repo.py +++ b/tests/test_repo.py @@ -51,7 +51,7 @@ def test_create(app_with_repo, borg_json_output, mocker, qtbot): mocker.patch.object(vorta.borg.borg_thread, 'Popen', return_value=popen_result) qtbot.mouseClick(main.createStartBtn, QtCore.Qt.LeftButton) - qtbot.waitUntil(lambda: main.createProgressText.text().startswith('INFO: Remote')) + qtbot.waitUntil(lambda: main.createProgressText.text().startswith('Backup finished.')) qtbot.waitUntil(lambda: main.createStartBtn.isEnabled()) assert EventLogModel.select().count() == 1 assert SnapshotModel.select().count() == 1