Changeset - 188c824cd944
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2014-07-13 03:00:09
dennis.fink@c3l.lu
Use function to send mail, unix principle
1 file changed with 19 insertions and 13 deletions:
0 comments (0 inline, 0 general)
ennstatus/status/functions.py
Show inline comments
 
@@ -12,6 +12,23 @@ from flask_mail import Mail, Message
 
mail = Mail()
 

	
 

	
 
def _send_mail(server_name, status, last_updated):
 

	
 
    current_app.logger.debug('Try sending mail')
 
    try:
 
        msg = Message('[Ennstatus] %s went %s' % (server_name, status))
 
        msg.add_recipient(current_app.config['ENNSTATUS_ADMINS'][server_name])
 
        msg.body = ('%s went to %s. I received the last update at %s'
 
                    % (server_name, status, last_updated))
 
        mail.send(msg)
 
    except KeyError:
 
        current_app.logger.error('Admin for %s not found!' % server_name)
 
    except Exception as e:
 
        current_app.logger.error('Unexpected error: %s' % e,
 
                                 exc_info=True)
 
    current_app.logger.debug('Finished trying!')
 

	
 

	
 
def _check_server(data):
 

	
 
    server_name = data['server_name']
 
@@ -21,8 +38,6 @@ def _check_server(data):
 
    now = datetime.utcnow()
 
    delta = now - date
 

	
 
    print(server_name)
 
    print(delta.days)
 
    if delta.days >= 7:
 
        os.remove(os.path.join('data', server_name.lower()+'.json'))
 
        current_app.logger.error('%s was removed!' % server_name)
 
@@ -40,17 +55,8 @@ def _check_server(data):
 
    if status is not None:
 
        current_app.logger.error('%s is set to %s' % (server_name,
 
                                 status))
 
        try:
 
            msg = Message('[Ennstatus] %s went %s' % (server_name, status))
 
            msg.add_recipient(current_app.config['ENNSTATUS_ADMINS'][server_name])
 
            msg.body = ('%s went to %s. I received the last update at %s'
 
                        % (server_name, status, last_updated))
 
            mail.send(msg)
 
        except KeyError:
 
            current_app.logger.error('Admin for %s not found!' % server_name)
 
        except Exception as e:
 
            current_app.logger.error('Unexpected error: %s' % e,
 
                                     exc_info=True)
 

	
 
        _send_mail(server_name, status, last_updated)
 

	
 
        for key in ('server_status', 'tor_status'):
 
            data[key] = status
0 comments (0 inline, 0 general)