Changeset - 05470b798e71
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2014-07-15 22:37:51
dennis.fink@c3l.lu
Send mail in a seperate thread
1 file changed with 20 insertions and 4 deletions:
0 comments (0 inline, 0 general)
ennstatus/status/functions.py
Show inline comments
 
@@ -4,6 +4,7 @@ import json
 

	
 
from collections import defaultdict
 
from datetime import datetime
 
from threading import Thread
 

	
 
from flask import current_app
 
from flask_mail import Mail, Message
 
@@ -12,16 +13,30 @@ from flask_mail import Mail, Message
 
mail = Mail()
 

	
 

	
 
def async(f):
 

	
 
    def wrapper(*args, **kwargs):
 
        thr = Thread(target=f, args=args, kwargs=kwargs)
 
        thr.start()
 

	
 
    return wrapper
 

	
 

	
 
@async
 
def send_async_mail(msg):
 
    mail.send(msg)
 

	
 

	
 
def _send_mail(server_name, status, last_updated):
 

	
 
    current_app.logger.debug('Try sending mail')
 
    current_app.logger.info('Try sending mail')
 
    try:
 
        msg = Message('[Ennstatus] %s went %s' % (server_name, status),
 
                      sender='ennstatus@enn.lu')
 
        msg.add_recipient(current_app.config['SERVER_ADMINS'][server_name])
 
        msg.body = ('%s went to %s. I received the last update at %s'
 
                    % (server_name, status, last_updated))
 
        mail.send(msg)
 
        send_async_mail(msg)
 
    except KeyError:
 
        current_app.logger.error('Admin for %s not found!' % server_name)
 
    except AssertionError:
 
@@ -29,7 +44,7 @@ def _send_mail(server_name, status, last
 
    except Exception as e:
 
        current_app.logger.error('Unexpected error: %s' % e,
 
                                 exc_info=True)
 
    current_app.logger.debug('Finished trying!')
 
    current_app.logger.info('Finished trying!')
 

	
 

	
 
def _check_server(data):
 
@@ -60,7 +75,8 @@ def _check_server(data):
 
        current_app.logger.error('%s is set to %s' % (server_name,
 
                                 status))
 

	
 
        if data['server_status'] == 'Unknown' and status == 'Offline':
 
        if (data['server_status'] == 'Unknown' and status == 'Offline') \
 
           or (data['server_status'] == 'Online' and status == 'Unknown'):
 
            _send_mail(server_name, status, last_updated)
 

	
 
        for key in ('server_status', 'tor_status'):
0 comments (0 inline, 0 general)