Mercurial > code > home > repos > href
changeset 38:f3a15a724483
mongo api and up-checking
author | drewp@bigasterisk.com |
---|---|
date | Sat, 19 Nov 2022 17:05:15 -0800 |
parents | bbc2431fd634 |
children | c538dc39b851 |
files | link.py lookup.py mongo_required.py pagetitle.py |
diffstat | 4 files changed, 31 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/link.py Sat Nov 19 17:03:17 2022 -0800 +++ b/link.py Sat Nov 19 17:05:15 2022 -0800 @@ -17,7 +17,7 @@ if not doc['href']: raise ValueError("no link") self.extract(doc) - self.coll.update({'href': doc['href']}, doc, upsert=True, safe=True) + self.coll.update({'href': doc['href']}, doc, upsert=True) def extract(self, doc): forUsers = []
--- a/lookup.py Sat Nov 19 17:03:17 2022 -0800 +++ b/lookup.py Sat Nov 19 17:05:15 2022 -0800 @@ -24,8 +24,9 @@ from jadestache import Renderer from link import Links, NotFound from pagetitle import PageTitle - -db = pymongo.MongoClient(os.environ['MONGODB_SERVICE_HOST'], tz_aware=True)['href'] +from mongo_required import open_mongo_or_die, die_on_mongo_connection_errors +from get_agent import bottleGetAgent +db = open_mongo_or_die()['href'] pageTitle = PageTitle(db) links = Links(db) renderer = Renderer(search_dirs=['template'], debug=bottle.DEBUG) @@ -193,6 +194,7 @@ @bottle.route('/<user>/<tags>') +@die_on_mongo_connection_errors() def userLinks(user, tags): tags = parseTags(tags) log.info('userLinks user=%r tags=%r', user, tags)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mongo_required.py Sat Nov 19 17:05:15 2022 -0800 @@ -0,0 +1,25 @@ +import contextlib +import os +import traceback + +import pymongo +import pymongo.errors + + +@contextlib.contextmanager +def die_on_mongo_connection_errors(): + try: + yield + except pymongo.errors.ServerSelectionTimeoutError: + traceback.print_exc() + os.abort() + + +def open_mongo_or_die(): + client = pymongo.MongoClient(host=os.environ['MONGODB_SERVICE_HOST'], + tz_aware=True, + connectTimeoutMS=3000, + serverSelectionTimeoutMS=3000) + with die_on_mongo_connection_errors(): + client.admin.command('ismaster') + return client