Mercurial > code > home > repos > href
comparison mongo_required.py @ 38:f3a15a724483
mongo api and up-checking
author | drewp@bigasterisk.com |
---|---|
date | Sat, 19 Nov 2022 17:05:15 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
37:bbc2431fd634 | 38:f3a15a724483 |
---|---|
1 import contextlib | |
2 import os | |
3 import traceback | |
4 | |
5 import pymongo | |
6 import pymongo.errors | |
7 | |
8 | |
9 @contextlib.contextmanager | |
10 def die_on_mongo_connection_errors(): | |
11 try: | |
12 yield | |
13 except pymongo.errors.ServerSelectionTimeoutError: | |
14 traceback.print_exc() | |
15 os.abort() | |
16 | |
17 | |
18 def open_mongo_or_die(): | |
19 client = pymongo.MongoClient(host=os.environ['MONGODB_SERVICE_HOST'], | |
20 tz_aware=True, | |
21 connectTimeoutMS=3000, | |
22 serverSelectionTimeoutMS=3000) | |
23 with die_on_mongo_connection_errors(): | |
24 client.admin.command('ismaster') | |
25 return client |