huangcm
2025-09-01 53d8e046ac1bf2ebe94f671983e3d3be059df91a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Scheduler email manager."""
 
 
import logging
import os
import re
import socket
import time
import traceback
 
import common
from autotest_lib.client.common_lib import global_config
from autotest_lib.site_utils  import gmail_lib
 
 
CONFIG_SECTION = 'SCHEDULER'
 
 
class EmailNotificationManager(object):
    """Scheduler email notification manager."""
 
    def __init__(self):
        """Initialize the manager."""
        self._emails = []
        self._notify_address = global_config.global_config.get_config_value(
            CONFIG_SECTION, "notify_email", default='')
 
 
    def send_email(self, to_string, subject, body):
        """Mails out emails to the addresses listed in to_string.
 
        @param to_string: is split into a list which can be delimited by any of:
                          ';', ',', ':' or any whitespace.
        @param subject: String, email subject.
        @param body: String, message body
        """
        # Create list from string removing empty strings from the list.
        to_list = [x for x in re.split('\s|,|;|:', to_string) if x]
        if not to_list:
            return
        to_string = ','.join(to_list)
        try:
            gmail_lib.send_email(to_string, subject, body)
        except Exception:
            logging.exception('Sending email failed:')
 
 
    def enqueue_notify_email(self, subject, message):
        """Enqueue a message that will be sent later.
 
        @param subject: String, subject of the message.
        @param message: String, message to enqueue.
        """
        logging.error(subject + '\n' + message)
        if not self._notify_address:
            return
 
        body = 'Subject: ' + subject + '\n'
        body += "%s / %s / %s\n%s" % (socket.gethostname(),
                                      os.getpid(),
                                      time.strftime("%X %x"), message)
        self._emails.append(body)
 
 
    def send_queued_emails(self):
        """Send queued emails."""
        if not self._emails:
            return
        subject = 'Scheduler notifications from ' + socket.gethostname()
        separator = '\n' + '-' * 40 + '\n'
        body = separator.join(self._emails)
 
        self.send_email(self._notify_address, subject, body)
        self._emails = []
 
 
    def log_stacktrace(self, reason):
        """Log an exception and enqueue it.
 
        @param reason: An exception to log and send.
        """
        logging.exception(reason)
        message = "EXCEPTION: %s\n%s" % (reason, traceback.format_exc())
        self.enqueue_notify_email("monitor_db exception", message)
 
 
manager = EmailNotificationManager()