add date to mail, and catch errors to prevent crash

This commit is contained in:
jb-alvarado 2018-11-14 10:47:53 +01:00
parent 4e809cfea3
commit 605ee1c1bc

View File

@ -22,21 +22,22 @@ import configparser
import logging import logging
import re import re
import smtplib import smtplib
import socket
import sys import sys
import xml.etree.ElementTree as ET
from argparse import ArgumentParser from argparse import ArgumentParser
from ast import literal_eval from ast import literal_eval
from datetime import datetime, date, timedelta from datetime import date, datetime, timedelta
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.utils import formatdate
from logging.handlers import TimedRotatingFileHandler from logging.handlers import TimedRotatingFileHandler
from os import path from os import path
from shutil import copyfileobj from shutil import copyfileobj
from subprocess import Popen, PIPE from subprocess import PIPE, Popen
from threading import Thread from threading import Thread
from time import sleep from time import sleep
from types import SimpleNamespace from types import SimpleNamespace
import xml.etree.ElementTree as ET
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# read variables from config file # read variables from config file
@ -191,14 +192,28 @@ def mail_or_log(message, time, path):
msg['From'] = _mail.s_addr msg['From'] = _mail.s_addr
msg['To'] = _mail.recip msg['To'] = _mail.recip
msg['Subject'] = "Playout Error" msg['Subject'] = "Playout Error"
msg["Date"] = formatdate(localtime=True)
msg.attach(MIMEText('{} {}\n{}'.format(time, message, path), 'plain')) msg.attach(MIMEText('{} {}\n{}'.format(time, message, path), 'plain'))
text = msg.as_string() text = msg.as_string()
server = smtplib.SMTP(_mail.server, int(_mail.port)) try:
server.starttls() server = smtplib.SMTP(_mail.server, _mail.port)
server.login(_mail.s_addr, _mail.s_pass) except socket.error as err:
server.sendmail(_mail.s_addr, _mail.recip, text) logger.error(err)
server.quit() server = None
if server is not None:
server.starttls()
try:
login = server.login(_mail.s_addr, _mail.s_pass)
except smtplib.SMTPAuthenticationError as serr:
logger.error(serr)
login = None
if login is not None:
server.sendmail(_mail.s_addr, _mail.recip, text)
server.quit()
else: else:
logger.error('{} {}'.format(message, path)) logger.error('{} {}'.format(message, path))
@ -301,7 +316,7 @@ def src_or_dummy(src, duration, seek, out, dummy_len=None):
def check_sync(begin): def check_sync(begin):
time_now = get_time('full_sec') time_now = get_time('full_sec')
start = float(_playlist.start * 3600) start = float(_playlist.start * 3600)
tolerance = _buffer.tol * 2 tolerance = _buffer.tol * 4
t_dist = begin - time_now t_dist = begin - time_now
if 0 <= time_now < start and not begin == start: if 0 <= time_now < start and not begin == start: