Merge lp:~phablet-team/history-service/single_instance_using_lock_file into lp:history-service/staging

Proposed by Gustavo Pichorim Boiko
Status: Merged
Approved by: Tiago Salem Herrmann
Approved revision: 277
Merged at revision: 262
Proposed branch: lp:~phablet-team/history-service/single_instance_using_lock_file
Merge into: lp:history-service/staging
Prerequisite: lp:~phablet-team/history-service/optimize_dbus_traffic
Diff against target: 47 lines (+16/-5)
2 files modified
cmake/modules/GenerateTest.cmake (+1/-0)
daemon/main.cpp (+15/-5)
To merge this branch: bzr merge lp:~phablet-team/history-service/single_instance_using_lock_file
Reviewer Review Type Date Requested Status
Tiago Salem Herrmann (community) Approve
Review via email: mp+318949@code.launchpad.net

Commit message

Use a QLockFile to ensure there will be only one instance of the daemon per user.
As we now delay the registration on dbus, sometimes we ended up having two instances of the daeon running (because of dbus activation). This change makes sure that won't happen.

Description of the change

Use a QLockFile to ensure there will be only one instance of the daemon per user.
As we now delay the registration on dbus, sometimes we ended up having two instances of the daeon running (because of dbus activation). This change makes sure that won't happen.

To post a comment you must log in.
274. By Gustavo Pichorim Boiko

Make sure tests use different lock files so that they can run simultaneously

275. By Tiago Salem Herrmann

merge parent branch

276. By Tiago Salem Herrmann

merge parent branch

277. By Gustavo Pichorim Boiko

History-daemon is not a system service, so store a lock somewhere in the user's home
directory to allow running one instance per user.

Revision history for this message
Tiago Salem Herrmann (tiagosh) wrote :

Looks good to me.
good to have this finally resolved.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cmake/modules/GenerateTest.cmake'
2--- cmake/modules/GenerateTest.cmake 2015-09-28 14:26:09 +0000
3+++ cmake/modules/GenerateTest.cmake 2017-03-09 22:20:18 +0000
4@@ -79,6 +79,7 @@
5 set(ARG_ENVIRONMENT HOME=${TMPDIR}
6 HISTORY_PLUGIN_PATH=${CMAKE_BINARY_DIR}/plugins/sqlite
7 HISTORY_SQLITE_DBPATH=:memory:
8+ HISTORY_LOCK_FILE=${TMPDIR}/history-service.lock
9 MC_ACCOUNT_DIR=${TMPDIR}
10 MC_MANAGER_DIR=${TMPDIR})
11 endif ()
12
13=== modified file 'daemon/main.cpp'
14--- daemon/main.cpp 2016-03-30 14:03:29 +0000
15+++ daemon/main.cpp 2017-03-09 22:20:18 +0000
16@@ -20,16 +20,26 @@
17 */
18
19 #include "historydaemon.h"
20+#include <QLockFile>
21+#include <QDir>
22
23 bool checkApplicationRunning()
24 {
25- bool result = false;
26- QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered(History::DBusService);
27- if (reply.isValid()) {
28- result = reply.value();
29+ QString lockPath = qgetenv("HISTORY_LOCK_FILE");
30+ if (lockPath.isEmpty()) {
31+ lockPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
32+ QDir dir(lockPath);
33+ if (!dir.exists("history-service") && !dir.mkpath("history-service")) {
34+ qCritical() << "Failed to create dir";
35+ // in case we fail to create the lock, better not even start the application
36+ return true;
37+ }
38+ dir.cd("history-service");
39+ lockPath = dir.absoluteFilePath("history-daemon.lock");
40 }
41
42- return result;
43+ static QLockFile *lockFile = new QLockFile(lockPath);
44+ return !lockFile->tryLock();
45 }
46 int main(int argc, char **argv)
47 {

Subscribers

People subscribed via source and target branches

to all changes: