add mail rate limit

This commit is contained in:
jb-alvarado 2019-11-25 22:01:41 +01:00
parent 15138b8659
commit f28e276d27

View File

@ -365,6 +365,9 @@ class Mailer:
def __init__(self): def __init__(self):
self.level = _mail.level self.level = _mail.level
self.time = None self.time = None
self.timestamp = get_time('stamp')
self.last_error = None
self.rate_limit = 600
def current_time(self): def current_time(self):
self.time = get_time(None) self.time = get_time(None)
@ -399,17 +402,25 @@ class Mailer:
server.sendmail(_mail.s_addr, _mail.recip, text) server.sendmail(_mail.s_addr, _mail.recip, text)
server.quit() server.quit()
def send_if_new(self, msg):
# send messege only when is new or the rate_limit is pass
if msg != self.last_error \
or get_time('stamp') - self.timestamp > self.rate_limit:
self.send_mail(msg)
self.timestamp = get_time('stamp')
self.last_error = msg
def info(self, msg): def info(self, msg):
if self.level in ['INFO']: if self.level in ['INFO']:
self.send_mail(msg) self.send_if_new(msg)
def warning(self, msg): def warning(self, msg):
if self.level in ['INFO', 'WARNING']: if self.level in ['INFO', 'WARNING']:
self.send_mail(msg) self.send_if_new(msg)
def error(self, msg): def error(self, msg):
if self.level in ['INFO', 'WARNING', 'ERROR']: if self.level in ['INFO', 'WARNING', 'ERROR']:
self.send_mail(msg) self.send_if_new(msg)
class Messenger: class Messenger:
@ -1350,8 +1361,6 @@ class GetSourceFromPlaylist:
self.first = True self.first = True
self.last = False self.last = False
self.list_date = get_date(True) self.list_date = get_date(True)
self.last_error = ''
self.timestamp = get_time('stamp')
self.src = None self.src = None
self.seek = 0 self.seek = 0
@ -1491,12 +1500,6 @@ class GetSourceFromPlaylist:
self.probe, self.src_cmd = gen_filler(self.duration) self.probe, self.src_cmd = gen_filler(self.duration)
self.set_filtergraph() self.set_filtergraph()
# send messege only when is new or an half hour is pass
if message != self.last_error \
or get_time('stamp') - self.timestamp > 1800:
messenger.error('{}\n{}'.format(message, self.json_file))
self.timestamp = get_time('stamp')
self.last_error = message
else: else:
self.src_cmd = None self.src_cmd = None
self.next_playlist = True self.next_playlist = True