fix(mu4e): cooperative lock file watching

Some time ago I noticed the cooperative lock file management wasn't
working as I remember. I forget what exactly I was thinking, but
basically I've poked at the code until it seems to work better.
This commit is contained in:
TEC 2022-09-20 22:52:51 +08:00 committed by Henrik Lissner
parent a6f0bf3123
commit d3839197f1

View file

@ -56,10 +56,11 @@ Else, write to this process' PID to the lock file"
(delete-file +mu4e-lock-request-file) (delete-file +mu4e-lock-request-file)
(call-process "touch" nil nil nil +mu4e-lock-request-file) (call-process "touch" nil nil nil +mu4e-lock-request-file)
(funcall orig-fun callback) (funcall orig-fun callback)
(setq +mu4e-lock--request-watcher (when +mu4e-lock--request-watcher
(file-notify-add-watch +mu4e-lock-request-file (file-notify-rm-watch +mu4e-lock--request-watcher))
'(change) (file-notify-add-watch +mu4e-lock-request-file
#'+mu4e-lock-request)))) '(change)
#'+mu4e-lock--request)))
(defvar +mu4e-lock--file-watcher nil) (defvar +mu4e-lock--file-watcher nil)
(defvar +mu4e-lock--file-just-deleted nil) (defvar +mu4e-lock--file-just-deleted nil)
@ -71,23 +72,24 @@ Else, write to this process' PID to the lock file"
(setq +mu4e-lock--file-watcher (setq +mu4e-lock--file-watcher
(file-notify-add-watch +mu4e-lock-file (file-notify-add-watch +mu4e-lock-file
'(change) '(change)
#'+mu4e-lock-file-updated))) #'+mu4e-lock--file-updated)))
(defun +mu4e-lock-request (event) (defun +mu4e-lock--request (event)
"Handle another process requesting the Mu4e lock." "Handle another process requesting the Mu4e lock."
(when (equal (nth 1 event) 'created) (when (eq (nth 1 event) 'created)
(when +mu4e-lock-relaxed (when +mu4e-lock-relaxed
(mu4e--stop) (mu4e--stop)
(file-notify-rm-watch +mu4e-lock--file-watcher) (file-notify-rm-watch +mu4e-lock--file-watcher)
(file-notify-rm-watch +mu4e-lock--request-watcher)
(message "Someone else wants to use Mu4e, releasing lock") (message "Someone else wants to use Mu4e, releasing lock")
(delete-file +mu4e-lock-file) (delete-file +mu4e-lock-file)
(run-at-time 0.2 nil #'+mu4e-lock-add-watcher)) (run-at-time 0.2 nil #'+mu4e-lock-add-watcher))
(delete-file +mu4e-lock-request-file))) (delete-file +mu4e-lock-request-file)))
(defun +mu4e-lock-file-updated (event) (defun +mu4e-lock--file-updated (event)
(if +mu4e-lock--file-just-deleted (if +mu4e-lock--file-just-deleted
(+mu4e-lock-add-watcher) (+mu4e-lock-add-watcher)
(when (equal (nth 1 event) 'deleted) (when (eq (nth 1 event) 'deleted)
(setq +mu4e-lock--file-just-deleted t) (setq +mu4e-lock--file-just-deleted t)
(when (and +mu4e-lock-greedy (+mu4e-lock-available t)) (when (and +mu4e-lock-greedy (+mu4e-lock-available t))
(message "Noticed Mu4e lock was available, grabbed it") (message "Noticed Mu4e lock was available, grabbed it")