Skip to content

Commit 5acdc83

Browse files
authored
Merge pull request #48 from dbmurphy/master
Adding TODO's and general cleanup comments
2 parents a5fed30 + 3bb35f6 commit 5acdc83

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

MongoBackup/Backup.py

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121

2222
class Backup(object):
2323
def __init__(self, options):
24+
# TODO-timv
25+
"""
26+
We should move the layout to look like
27+
28+
self.options : {
29+
"program_name" : None,
30+
...
31+
"backup_options": {
32+
"host": localhost,
33+
"port": 27017,
34+
},
35+
"uploader": {},
36+
"notifier": {},
37+
38+
}
39+
Also all options should have defaults for example program_name should always be something
40+
"""
2441
self.program_name = None
2542
self.version = None
2643
self.git_commit = None
@@ -88,16 +105,29 @@ def __init__(self, options):
88105
except Exception:
89106
raise Exception, 'Field: %s is required by %s!' % (field, __name__), None
90107

108+
# Setup signal handler:
109+
signal(SIGINT, self.cleanup_and_exit)
110+
signal(SIGTERM, self.cleanup_and_exit)
111+
112+
# TODO Move to function
91113
# Set default lock file:
92114
if not self.lock_file:
93115
self.lock_file = '/tmp/%s.lock' % self.program_name
94116

117+
# TODO Move to function
118+
# Setup backup dir name:
119+
time_string = datetime.now().strftime("%Y%m%d_%H%M")
120+
self.backup_root_subdirectory = "%s/%s" % (self.backup_name, time_string)
121+
self.backup_root_directory = "%s/%s" % (self.backup_location, self.backup_root_subdirectory)
122+
123+
# TODO Move below to actual functions called by a master run function
95124
# Setup logging
96125
if self.verbose:
97126
self.log_level = logging.DEBUG
98127
logging.basicConfig(level=self.log_level,
99128
format='[%(asctime)s] [%(levelname)s] [%(processName)s] [%(module)s:%(funcName)s:%(lineno)d] %(message)s')
100129

130+
# TODO Move any reference to the actual dumping into dumper classes
101131
# Check mongodump binary and set version + dump_gzip flag if 3.2+
102132
if os.path.isfile(self.backup_binary) and os.access(self.backup_binary, os.X_OK):
103133
with hide('running', 'warnings'), settings(warn_only=True):
@@ -111,6 +141,7 @@ def __init__(self, options):
111141
logging.fatal("Cannot find or execute the mongodump binary file %s!" % self.backup_binary)
112142
sys.exit(1)
113143

144+
#TODO should this be in init or a sub-function?
114145
# Get a DB connection
115146
try:
116147
self.db = DB(self.host, self.port, self.user, self.password, self.authdb)
@@ -119,11 +150,7 @@ def __init__(self, options):
119150
except Exception, e:
120151
raise e
121152

122-
# Setup backup dir name:
123-
time_string = datetime.now().strftime("%Y%m%d_%H%M")
124-
self.backup_root_subdirectory = "%s/%s" % (self.backup_name, time_string)
125-
self.backup_root_directory = "%s/%s" % (self.backup_location, self.backup_root_subdirectory)
126-
153+
# TODO Move to notifier module called NSCA
127154
# Setup the notifier:
128155
try:
129156
if self.nsca_server and self.nsca_check_name:
@@ -136,21 +163,22 @@ def __init__(self, options):
136163
except Exception, e:
137164
raise e
138165

139-
# Setup signal handler:
140-
signal(SIGINT, self.cleanup_and_exit)
141-
signal(SIGTERM, self.cleanup_and_exit)
142166

167+
# TODO Rename class to be more exact as this assumes something went wrong
143168
# noinspection PyUnusedLocal
144169
def cleanup_and_exit(self, code, frame):
145170
if current_process().name == "MainProcess":
146171
logging.info("Starting cleanup and exit procedure! Killing running threads")
147172

173+
# TODO Rename the mongodumper module to just "backup" then have submodule in it for the backup type
174+
# TODO Move submodules into self that populates as used?
148175
submodules = ['replset', 'sharding', 'mongodumper', 'oplogtailer', 'archiver', 'uploader_s3']
149176
for submodule_name in submodules:
150177
submodule = getattr(self, submodule_name)
151178
if submodule:
152179
submodule.close()
153180

181+
# TODO Pass to notifier Notifier(level,mesg) and it will pick the medium
154182
if self.notify_nsca:
155183
self.notify_nsca.notify(self.notify_nsca.critical, "%s: backup '%s' failed!" % (
156184
self.program_name,
@@ -172,6 +200,23 @@ def exception(self, error_message):
172200
return self.cleanup_and_exit(None, None)
173201

174202
def run(self):
203+
# TODO would be nice to have this code look like: (functions do the work) and its readable
204+
"""
205+
self.log(version_message,INFO)
206+
self.lock()
207+
self.start_timer()
208+
if not self.is_sharded():
209+
self.exec_unsharded()
210+
else
211+
self.exec_sharded()
212+
self.stopTimer()
213+
self.archive()
214+
self.upload()
215+
self.notify()
216+
if self.db:
217+
self.db.close()
218+
self.log(backup_complete_message,INFO)
219+
"""
175220
logging.info("Starting %s version %s (git commit hash: %s)" % (self.program_name, self.version, self.git_commit))
176221

177222
# noinspection PyBroadException

0 commit comments

Comments
 (0)