1
0
Fork 0
mirror of https://github.com/borgbase/vorta synced 2024-12-22 15:57:34 +00:00

Ensure only one app instance is running. Fixes #27

This commit is contained in:
Manu 2018-11-21 22:17:48 +08:00
parent 3f7249fb44
commit 55ff2b0352
3 changed files with 22 additions and 3 deletions

View file

@ -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_())

View file

@ -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)

View file

@ -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