Skip to content

Commit 170b702

Browse files
Return non-success on failure instead of OK (code=0), also include the error encountered. Cleaned up Notify/Notify.py
1 parent da55abd commit 170b702

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

mongodb_consistent_backup/Main.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self, prog_name="mongodb-consistent-backup"):
4949
self.logger = None
5050
self.current_log_file = None
5151
self.backup_log_file = None
52+
self.last_error_msg = None
5253

5354
try:
5455
self.setup_config()
@@ -184,23 +185,23 @@ def cleanup_and_exit(self, code, frame):
184185

185186
if self.manager:
186187
self.manager.shutdown()
188+
if self.db:
189+
self.db.close()
187190

188191
if self.notify:
189192
try:
190-
self.notify.notify("%s: backup '%s' failed!" % (
191-
self.config,
192-
self.program_name
193-
), False)
193+
self.notify.notify("%s: backup '%s/%s' failed! Error: '%s'" % (
194+
self.program_name,
195+
self.config.backup.name,
196+
self.backup_time,
197+
self.last_error_msg
198+
))
194199
self.notify.run()
195200
self.notify.close()
196-
except:
197-
pass
198-
199-
if self.db:
200-
self.db.close()
201+
except Exception, e:
202+
logging.error("Error from notifier: %s" % e)
201203

202204
logging.info("Cleanup complete, exiting")
203-
204205
if self.logger:
205206
self.logger.rotate()
206207
self.logger.close()
@@ -209,6 +210,7 @@ def cleanup_and_exit(self, code, frame):
209210
sys.exit(1)
210211

211212
def exception(self, error_message, error):
213+
self.last_error_msg = error_message
212214
if isinstance(error, NotifyError):
213215
logging.error(error_message)
214216
else:

mongodb_consistent_backup/Notify/Notify.py

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

3+
from mongodb_consistent_backup.Errors import Error, NotifyError
34
from mongodb_consistent_backup.Notify.Nsca import Nsca
45
from mongodb_consistent_backup.Pipeline import Stage
56

@@ -9,7 +10,6 @@ def __init__(self, manager, config, timer, base_dir, backup_dir):
910
super(Notify, self).__init__(self.__class__.__name__, manager, config, timer, base_dir, backup_dir)
1011
self.task = self.config.notify.method
1112

12-
self.completed = False
1313
self.notifications = []
1414
self.init()
1515

@@ -19,19 +19,19 @@ def notify(self, message, success=False):
1919

2020
def run(self, *args):
2121
if self._task and len(self.notifications) > 0:
22-
logging.info("Sending %i notification(s) to: %s" % (len(self.notifications), self._task.server))
23-
self.timers.start(self.stage)
24-
while len(self.notifications) > 0:
25-
try:
26-
(success, message) = self.notifications.pop()
27-
state = self._task.failed
28-
if success:
29-
state = self._task.success
30-
self._task.run(state, message)
31-
except:
32-
continue
33-
self.timers.stop(self.stage)
34-
self.completed = True
22+
try:
23+
logging.info("Sending %i notification(s) to: %s" % (len(self.notifications), self._task.server))
24+
while len(self.notifications) > 0:
25+
try:
26+
(success, message) = self.notifications.pop()
27+
state = self._task.failed
28+
if success == True:
29+
state = self._task.success
30+
self._task.run(state, message)
31+
except NotifyError:
32+
continue
33+
except Exception, e:
34+
raise Error(e)
3535

3636
def close(self):
3737
if self._task:

mongodb_consistent_backup/Notify/Nsca/Nsca.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def close(self):
5454

5555
def run(self, ret_code, output):
5656
if self.notifier:
57-
self.timer.start(self.timer_name)
5857
logging.info("Sending %sNSCA report to check host/name '%s/%s' at NSCA host %s" % (
5958
self.mode_type,
6059
self.check_host,
@@ -66,7 +65,6 @@ def run(self, ret_code, output):
6665
try:
6766
self.notifier.svc_result(self.check_host, self.check_name, ret_code, str(output))
6867
logging.debug('Sent %sNSCA report to host %s' % (self.mode_type, self.server))
69-
self.timer.stop(self.timer_name)
7068
except Exception, e:
7169
logging.error('Failed to send %sNSCA report to host %s: %s' % (self.mode_type, self.server, sys.exc_info()[1]))
7270
raise NotifyError(e)

0 commit comments

Comments
 (0)