Skip to content

Commit 91d3dfe

Browse files
StevePCDeer-WarLord
authored andcommitted
ZEN-35430: break out of email retry loop on success
1 parent fa7e416 commit 91d3dfe

File tree

1 file changed

+88
-78
lines changed

1 file changed

+88
-78
lines changed

src/Products/ZenModel/actions.py

Lines changed: 88 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -434,86 +434,96 @@ def executeBatch(self, notification, signal, targets):
434434
original_fst = signal.event.first_seen_time
435435
original_sct = signal.event.status_change_time
436436
for target_timezone, targets in tz_targets.iteritems():
437-
# Convert timestamp to user timezone
438-
signal.event.last_seen_time = self._adjustToTimezone(
439-
original_lst, target_timezone)
440-
signal.event.first_seen_time = self._adjustToTimezone(
441-
original_fst, target_timezone)
442-
signal.event.status_change_time = self._adjustToTimezone(
443-
original_sct, target_timezone)
444-
445-
data = self._signalToContextDict(signal, notification)
446-
447-
# Check for email recipients in the event
448-
details = data['evt'].details
449-
mail_targets = details.get('recipients', '')
450-
mail_targets = [x.strip() for x in mail_targets.split(',') if x.strip()]
451-
if len(mail_targets) > 0:
452-
log.debug("Adding recipients defined in the event %s", mail_targets)
453-
targets |= set(mail_targets)
454-
455-
skipfails = notification.content.get('skipfails', False)
456-
457-
if signal.clear:
458-
log.debug("Generating a notification at enabled 'Send Clear' option when event was closed")
459-
subject = processTalSource(notification.content['clear_subject_format'], skipfails, **data)
460-
body = processTalSource(notification.content['clear_body_format'], skipfails, **data)
461-
else:
462-
subject = processTalSource(notification.content['subject_format'], skipfails, **data)
463-
body = processTalSource(notification.content['body_format'], skipfails, **data)
464-
465-
log.debug('Sending this subject: %s', subject)
466-
467-
# Find all time strings in body and add timezone to it
468-
body = re.sub(r'(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{3})',
469-
r'\1 ({})'.format(target_timezone), body)
470-
471-
plain_body = self._encodeBody(self._stripTags(body)) \
472-
if self.stripBodyTags else self._encodeBody(body)
473-
474-
email_message = plain_body
475-
476-
if notification.content['body_content_type'] == 'html':
477-
email_message = MIMEMultipart('related')
478-
email_message_alternative = MIMEMultipart('alternative')
479-
email_message_alternative.attach(plain_body)
480-
html_body = self._encodeBody(body)
481-
html_body.set_type('text/html')
482-
email_message_alternative.attach(html_body)
483-
484-
email_message.attach(email_message_alternative)
485-
log.debug('Sending this body: %s', body)
486-
else:
487-
log.debug('Sending this body: %s', plain_body)
488-
489-
host = notification.content['host']
490-
port = notification.content['port']
491-
user = notification.content['user']
492-
password = notification.content['password']
493-
useTls = notification.content['useTls']
494-
email_from = notification.content['email_from']
495-
496-
email_message['Subject'] = subject
497-
email_message['From'] = email_from
498-
email_message['To'] = ','.join(targets)
499-
email_message['Date'] = formatdate(None, True)
500-
501-
result, errorMsg = sendEmail(
502-
email_message,
503-
host, port,
504-
useTls,
505-
user, password
506-
)
507-
508-
if result:
509-
log.debug("Notification '%s' sent emails to: %s",
510-
notification.id, targets)
511-
else:
512-
raise ActionExecutionException(
513-
"Notification '%s' FAILED to send emails to %s: %s" %
514-
(notification.id, targets, errorMsg)
437+
num_of_attempts = 3
438+
for attempt in xrange(1, num_of_attempts+1):
439+
# Convert timestamp to user timezone
440+
signal.event.last_seen_time = self._adjustToTimezone(
441+
original_lst, target_timezone)
442+
signal.event.first_seen_time = self._adjustToTimezone(
443+
original_fst, target_timezone)
444+
signal.event.status_change_time = self._adjustToTimezone(
445+
original_sct, target_timezone)
446+
447+
data = self._signalToContextDict(signal, notification)
448+
449+
# Check for email recipients in the event
450+
details = data['evt'].details
451+
mail_targets = details.get('recipients', '')
452+
mail_targets = [x.strip() for x in mail_targets.split(',') if x.strip()]
453+
if len(mail_targets) > 0:
454+
log.debug("Adding recipients defined in the event %s", mail_targets)
455+
targets |= set(mail_targets)
456+
457+
skipfails = notification.content.get('skipfails', False)
458+
459+
if signal.clear:
460+
log.debug("Generating a notification at enabled 'Send Clear' option when event was closed")
461+
subject = processTalSource(notification.content['clear_subject_format'], skipfails, **data)
462+
body = processTalSource(notification.content['clear_body_format'], skipfails, **data)
463+
else:
464+
subject = processTalSource(notification.content['subject_format'], skipfails, **data)
465+
body = processTalSource(notification.content['body_format'], skipfails, **data)
466+
467+
log.debug('Sending this subject: %s', subject)
468+
469+
# Find all time strings in body and add timezone to it
470+
body = re.sub(r'(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{3})',
471+
r'\1 ({})'.format(target_timezone), body)
472+
473+
plain_body = self._encodeBody(self._stripTags(body)) \
474+
if self.stripBodyTags else self._encodeBody(body)
475+
476+
email_message = plain_body
477+
478+
if notification.content['body_content_type'] == 'html':
479+
email_message = MIMEMultipart('related')
480+
email_message_alternative = MIMEMultipart('alternative')
481+
email_message_alternative.attach(plain_body)
482+
html_body = self._encodeBody(body)
483+
html_body.set_type('text/html')
484+
email_message_alternative.attach(html_body)
485+
486+
email_message.attach(email_message_alternative)
487+
log.debug('Sending this body: %s', body)
488+
else:
489+
log.debug('Sending this body: %s', plain_body)
490+
491+
host = notification.content['host']
492+
port = notification.content['port']
493+
user = notification.content['user']
494+
password = notification.content['password']
495+
useTls = notification.content['useTls']
496+
email_from = notification.content['email_from']
497+
498+
email_message['Subject'] = subject
499+
email_message['From'] = email_from
500+
email_message['To'] = ','.join(targets)
501+
email_message['Date'] = formatdate(None, True)
502+
503+
result, errorMsg = sendEmail(
504+
email_message,
505+
host, port,
506+
useTls,
507+
user, password
515508
)
516509

510+
if result:
511+
log.debug("Notification '%s' sent emails to: %s",
512+
notification.id, targets)
513+
break
514+
elif "socket.error" in errorMsg:
515+
if attempt < num_of_attempts:
516+
current_timeout = attempt * 5
517+
log.info('socket.error: %s. Still have time to try again. Try in %d seconds again...' %
518+
(errorMsg, current_timeout))
519+
time.sleep(current_timeout)
520+
continue
521+
else:
522+
raise ActionExecutionException(
523+
"Notification '%s' FAILED to send emails to %s: %s" %
524+
(notification.id, targets, errorMsg)
525+
)
526+
517527
def getActionableTargets(self, target):
518528
"""
519529
@param target: This is an object that implements the IProvidesEmailAddresses

0 commit comments

Comments
 (0)