Re: [notmuch] [PATCH] Calls to notmuch get queued and executed asynchronously.

Subject: Re: [notmuch] [PATCH] Calls to notmuch get queued and executed asynchronously.

Date: Wed, 24 Feb 2010 14:28:16 -0500



From: James Vasile

Sleep between retrying asynchronous notmuch commands and check for
notmuch-process before firing off a new one

If the db is locked when notmuch tries to write to it, an error is
reported to the client (in this case, notmuch.el).  Instead of
accepting that error, wait a small amount of time and then try again.

Also, don't start multiple asynchronous processes.
 notmuch.el |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/notmuch.el b/notmuch.el
index 7fc63e9..31e89b9 100644
--- a/notmuch.el
+++ b/notmuch.el
@@ -1402,9 +1402,14 @@ args is a list of arguments to notmuch.  ex: (\"tag\" \"+list\"
 Calls to notmuch are queued and called asynchronously."
   (setq notmuch-asynch-queue (append notmuch-asynch-queue (list args)))
-  (when (= (length notmuch-asynch-queue) 1)
+  (when (and (= (length notmuch-asynch-queue) 1)
+	     (not (get-process "notmuch-process")))
     (apply 'notmuch-call-notmuch-process-asynch (pop notmuch-asynch-queue))))
+(defun notmuch-asynch-sleep-sentinel (process event)
+  "After we sleep, try a command from the notmuch queue"
+  (apply 'notmuch-call-notmuch-process-asynch (pop notmuch-asynch-queue)))
 (defun notmuch-call-notmuch-process-asynch-sentinel (process event)
   "Handle the exit of a notmuch asynch process.
@@ -1416,11 +1421,16 @@ command, try it again."
     (if (= (process-exit-status process) 0)
 	(kill-buffer (buffer-name (process-buffer process)))
 	(if (search-forward "Unable to acquire database write lock" nil t)
-	    (apply 'notmuch-call-notmuch-process-asynch (cdr (process-command process)))
+	    (progn
+	      (push (cdr (process-command process)) notmuch-asynch-queue)
+	      (set-process-sentinel 
+	       (start-process "notmuch-sleep" nil "sleep" "3")
+	       'notmuch-asynch-sleep-sentinel))
 	    (error (format "%s: %s" (join-string-list (process-command process))
   (apply 'notmuch-call-notmuch-process-asynch (pop notmuch-asynch-queue)))
 (defun notmuch-call-notmuch-process (&rest args)
   "Synchronously invoke \"notmuch\" with the given list of arguments.