blob: 3cdb0f0186e75fb68c5b252f46936b46c30ac973 [file] [log] [blame]
Fang Deng8177dfd2015-04-10 11:57:33 -07001"""Scheduler email manager."""
2
3
4import logging
5import os
6import re
7import socket
8import time
9import traceback
10
showard170873e2009-01-07 00:22:26 +000011import common
12from autotest_lib.client.common_lib import global_config
Fang Deng8177dfd2015-04-10 11:57:33 -070013from autotest_lib.site_utils import gmail_lib
14
showard170873e2009-01-07 00:22:26 +000015
16CONFIG_SECTION = 'SCHEDULER'
17
showardb6a186f2009-09-11 18:45:54 +000018
showard170873e2009-01-07 00:22:26 +000019class EmailNotificationManager(object):
Fang Deng8177dfd2015-04-10 11:57:33 -070020 """Scheduler email notification manager."""
21
showard170873e2009-01-07 00:22:26 +000022 def __init__(self):
Fang Deng8177dfd2015-04-10 11:57:33 -070023 """Initialize the manager."""
showard170873e2009-01-07 00:22:26 +000024 self._emails = []
showard170873e2009-01-07 00:22:26 +000025 self._notify_address = global_config.global_config.get_config_value(
showardd1ee1dd2009-01-07 21:33:08 +000026 CONFIG_SECTION, "notify_email", default='')
showard170873e2009-01-07 00:22:26 +000027
28
29 def send_email(self, to_string, subject, body):
30 """Mails out emails to the addresses listed in to_string.
31
Fang Deng8177dfd2015-04-10 11:57:33 -070032 @param to_string: is split into a list which can be delimited by any of:
33 ';', ',', ':' or any whitespace.
34 @param subject: String, email subject.
35 @param body: String, message body
showard170873e2009-01-07 00:22:26 +000036 """
37 # Create list from string removing empty strings from the list.
38 to_list = [x for x in re.split('\s|,|;|:', to_string) if x]
39 if not to_list:
40 return
Fang Deng8177dfd2015-04-10 11:57:33 -070041 to_string = ','.join(to_list)
showard170873e2009-01-07 00:22:26 +000042 try:
Fang Deng8177dfd2015-04-10 11:57:33 -070043 gmail_lib.send_email(to_string, subject, body)
showard170873e2009-01-07 00:22:26 +000044 except Exception:
showardf098ebd2009-06-10 00:13:33 +000045 logging.exception('Sending email failed:')
showard170873e2009-01-07 00:22:26 +000046
47
48 def enqueue_notify_email(self, subject, message):
Fang Deng8177dfd2015-04-10 11:57:33 -070049 """Enqueue a message that will be sent later.
50
51 @param subject: String, subject of the message.
52 @param message: String, message to enqueue.
53 """
showarded2afea2009-07-07 20:54:07 +000054 logging.error(subject + '\n' + message)
showard170873e2009-01-07 00:22:26 +000055 if not self._notify_address:
56 return
57
58 body = 'Subject: ' + subject + '\n'
59 body += "%s / %s / %s\n%s" % (socket.gethostname(),
60 os.getpid(),
61 time.strftime("%X %x"), message)
62 self._emails.append(body)
63
64
65 def send_queued_emails(self):
Fang Deng8177dfd2015-04-10 11:57:33 -070066 """Send queued emails."""
showard170873e2009-01-07 00:22:26 +000067 if not self._emails:
68 return
69 subject = 'Scheduler notifications from ' + socket.gethostname()
70 separator = '\n' + '-' * 40 + '\n'
71 body = separator.join(self._emails)
72
73 self.send_email(self._notify_address, subject, body)
74 self._emails = []
75
76
77 def log_stacktrace(self, reason):
Fang Deng8177dfd2015-04-10 11:57:33 -070078 """Log an exception and enqueue it.
79
80 @param reason: An exception to log and send.
81 """
showard27f33872009-04-07 18:20:53 +000082 logging.exception(reason)
showard170873e2009-01-07 00:22:26 +000083 message = "EXCEPTION: %s\n%s" % (reason, traceback.format_exc())
showard170873e2009-01-07 00:22:26 +000084 self.enqueue_notify_email("monitor_db exception", message)
85
86
87manager = EmailNotificationManager()