From bdbfab1fca947861c23887adc48a126f12578f39 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 21 Sep 2014 11:38:33 -0700 Subject: [PATCH] nmhive.py: Use notmuch behind GET /tags I stick with the NMBPREFIX environment variable for consistency with nmbug itself. I also drop 'app.debug = True', to avoid trouble like: $ ./nmhive.py * Running on http://0.0.0.0:5000/ * Restarting with reloader A Xapian exception occurred opening database: Unable to get write lock on /.../xapian: already locked Traceback (most recent call last): File "./nmhive.py", line 83, in mode=notmuch.Database.MODE.READ_WRITE) File "/.../notmuch/database.py", line 154, in __init__ self.open(path, mode) File "/.../notmuch/database.py", line 214, in open raise NotmuchError(status) notmuch.errors.XapianError because with 'debug = True', Flask tries to run two instances of this process simultaneously, but only one can hold the write lock at a time. If we want to scale this up to multiple writing threads/processes, we'll probably want to make the persistant Database instance read-only, and either acquire a write lock as necessary, or just instantiate a read/write database for each PUT. For now, it's easy enough to just have a single thread. --- nmhive.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/nmhive.py b/nmhive.py index 23bd499..2acde49 100755 --- a/nmhive.py +++ b/nmhive.py @@ -2,31 +2,32 @@ import json import mailbox +import os import tempfile import urllib.request import flask import flask_cors +import notmuch app = flask.Flask(__name__) app.config['CORS_HEADERS'] = 'Content-Type' flask_cors.CORS(app) - -_AVAILABLE_TAGS = { - 'bug', - 'needs-review', - 'obsolete', - 'patch', - } +TAG_PREFIX = os.getenv('NMBPREFIX', 'notmuch::') +NOTMUCH = None _TAGS = {} @app.route('/tags', methods=['GET']) def tags(): + tags = set() + for t in NOTMUCH.get_all_tags(): + if t.startswith(TAG_PREFIX): + tags.add(t[len(TAG_PREFIX):]) return flask.Response( - response=json.dumps(sorted(_AVAILABLE_TAGS)), + response=json.dumps(sorted(tags)), mimetype='application/json') @@ -76,5 +77,7 @@ def gmane_message_id(group, article): if __name__ == '__main__': - app.debug = True + NOTMUCH = notmuch.Database( + path=None, + mode=notmuch.Database.MODE.READ_WRITE) app.run(host='0.0.0.0') -- 2.26.2