Skip to content

Commit 94e3a04

Browse files
author
Tim Vaillancourt
committed
moved admin_command method into main DB class
1 parent 627f0fd commit 94e3a04

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

MongoBackup/DB.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import logging
22

33
from pymongo import MongoClient
4+
from time import sleep
45

56

67
class DB:
7-
def __init__(self, host='localhost', port=27017, username=None, password=None, authdb="admin", conn_timeout=5000):
8-
self.host = host
9-
self.port = port
10-
self.username = username
11-
self.password = password
12-
self.authdb = authdb
8+
def __init__(self, host='localhost', port=27017, username=None, password=None, authdb="admin", conn_timeout=5000, retries=5):
9+
self.host = host
10+
self.port = port
11+
self.username = username
12+
self.password = password
13+
self.authdb = authdb
1314
self.conn_timeout = conn_timeout
15+
self.retries = retries
16+
1417
self._conn = None
1518
self.connect()
1619
self.auth_if_required()
@@ -41,6 +44,22 @@ def auth_if_required(self):
4144
else:
4245
pass
4346

47+
def admin_command(self, admin_command):
48+
tries = 0
49+
status = None
50+
while not status and tries < self.retries:
51+
try:
52+
status = self._conn['admin'].command(admin_command)
53+
if not status:
54+
raise e
55+
except Exception, e:
56+
logging.error("Error running admin command '%s': %s" % (admin_command, e))
57+
tries += 1
58+
sleep(1)
59+
if not status:
60+
raise Exception, "Could not get output from command: '%s' after %i retries!" % (admin_command, self.retries), None
61+
return status
62+
4463
def connection(self):
4564
return self._conn
4665

MongoBackup/ReplsetHandler.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
11
import logging
22

33
from math import ceil
4-
from time import mktime, sleep
4+
from time import mktime
55

66
from DB import DB
77
from ShardingHandler import ShardingHandler
88

99

1010
class ReplsetHandler:
11-
def __init__(self, host, port, user, password, authdb, max_lag_secs, retries=5):
11+
def __init__(self, host, port, user, password, authdb, max_lag_secs):
1212
self.host = host
1313
self.port = port
1414
self.user = user
1515
self.password = password
1616
self.authdb = authdb
1717
self.max_lag_secs = max_lag_secs
18-
self.retries = retries
1918

2019
try:
21-
self.connection = DB(self.host, self.port, self.user, self.password, self.authdb).connection()
20+
self.db = DB(self.host, self.port, self.user, self.password, self.authdb)
21+
self.connection = self.db.connection()
2222
except Exception, e:
2323
logging.fatal("Could not get DB connection! Error: %s" % e)
2424
raise e
2525

2626
def close(self):
2727
return self.connection.close()
2828

29-
def admin_command(self, admin_command):
30-
tries = 0
31-
status = None
32-
while not status and tries < self.retries:
33-
try:
34-
status = self.connection['admin'].command(admin_command)
35-
if not status:
36-
raise e
37-
except Exception, e:
38-
logging.error("Error running command '%s': %s" % (admin_command, e))
39-
tries += 1
40-
sleep(1)
41-
if not status:
42-
raise Exception, "Could not get output from command: '%s' after %i retries!" % (admin_command, self.retries), None
43-
return status
44-
4529
def get_rs_status(self):
46-
return self.admin_command('replSetGetStatus')
30+
return self.db.admin_command('replSetGetStatus')
4731

4832
def get_rs_config(self):
49-
return self.admin_command('replSetGetConfig')
33+
return self.db.admin_command('replSetGetConfig')
5034

5135
def find_desirable_secondary(self):
5236
rs_status = self.get_rs_status()

0 commit comments

Comments
 (0)