nmhive.py: Commit tag changes to nmbug
authorW. Trevor King <wking@tremily.us>
Sun, 21 Sep 2014 21:36:07 +0000 (14:36 -0700)
committerW. Trevor King <wking@tremily.us>
Sun, 21 Sep 2014 21:36:07 +0000 (14:36 -0700)
There's a bit of a race here: we need to close the database before
committing so that the change has been flushed to disk for nmbug to
pick up, but that means we're not committing while protected by the
read/write lock.  Ideally, we could flush the database to disk, run
the nmbug commit, and then close the database to release the lock.
Unfortunately there is no "commit changes to the datatabase without
closing" command exposed by notmuch, but maybe I can talk folks into
adding one ;).  On the other hand, the race is against parallel
request that aquires the lock after we release it *and* gets their
commit in before ours, which seems unlikely.  Still, it would be
better if we had a known-safe option here.

nmhive.py

index 6f9f346777dad836c0c878793fce49f3b53d449f..06c2cd0c90889d0d876292969e703c5f7e54502f 100755 (executable)
--- a/nmhive.py
+++ b/nmhive.py
@@ -8,6 +8,7 @@ import urllib.request
 
 import flask
 import flask_cors
+import nmbug
 import notmuch
 
 
@@ -43,6 +44,7 @@ def _message_tags(message):
 @app.route('/mid/<message_id>', methods=['GET', 'POST'])
 def message_id_tags(message_id):
     if flask.request.method == 'POST':
+        changes = flask.request.get_json()
         database = notmuch.Database(
             path=NOTMUCH_PATH,
             mode=notmuch.Database.MODE.READ_WRITE)
@@ -52,7 +54,7 @@ def message_id_tags(message_id):
                 return flask.Response(status=404)
             database.begin_atomic()
             message.freeze()
-            for change in flask.request.get_json():
+            for change in changes:
                 if change.startswith('+'):
                     message.add_tag(TAG_PREFIX + change[1:])
                 elif change.startswith('-'):
@@ -64,6 +66,8 @@ def message_id_tags(message_id):
             tags = _message_tags(message=message)
         finally:
             database.close()
+        nmbug.commit(message='nmhive: {} {}'.format(
+            message_id, ' '.join(changes)))
     elif flask.request.method == 'GET':
         database = notmuch.Database(path=NOTMUCH_PATH)
         try: