-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
56 lines (42 loc) · 1.33 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pymongo
MAX_RETRIES = 3
class DBException(Exception):
pass
def retry(num_tries, exceptions):
def decorator(func):
def f_retry(*args, **kwargs):
for i in range(num_tries):
try:
return func(*args, **kwargs)
except exceptions as e:
continue
return f_retry
return decorator
# Retry on AutoReconnect exception, maximum 3 times
retry_auto_reconnect = retry(MAX_RETRIES, (pymongo.errors.AutoReconnect,))
class MongoConnector(object):
def __init__(self):
self._client = pymongo.MongoClient(
"mongodb://localhost:27017")
scannerDb = self._client["webscanner"]
scannerDb["subscribers"].create_index(
[
("blockchain", pymongo.ASCENDING),
("wallet_address", pymongo.ASCENDING),
("webhook_url", pymongo.ASCENDING),
],
unique=True,
)
scannerDb["transactions"].create_index(
[
("subscriber_id", pymongo.ASCENDING),
("txn_hash", pymongo.ASCENDING),
],
unique=True,
)
@property
def client(self):
return self._client
@retry_auto_reconnect
def getDB(self, dbName: str):
return self.client[dbName]