add aircox.test utilities

This commit is contained in:
bkfox
2023-06-18 16:07:09 +02:00
parent 95f5dca683
commit a1ca0e70cf
4 changed files with 129 additions and 27 deletions

View File

@ -5,14 +5,10 @@
# x when liquidsoap fails to start/exists: exit
# - handle restart after failure
# - is stream restart after live ok?
import pytz
from django.utils import timezone as tz
from aircox.models import Diffusion, Log, Sound, Track
# force using UTC
tz.activate(pytz.UTC)
class Monitor:
"""Log and launch diffusions for the given station.
@ -33,9 +29,9 @@ class Monitor:
""" Timedelta: minimal delay between two call of monitor. """
logs = None
"""Queryset to station's logs (ordered by -pk)"""
cancel_timeout = 20
cancel_timeout = tz.timedelta(minutes=20)
"""Timeout in minutes before cancelling a diffusion."""
sync_timeout = 5
sync_timeout = tz.timedelta(minutes=5)
"""Timeout in minutes between two streamer's sync."""
sync_next = None
"""Datetime of the next sync."""
@ -56,11 +52,10 @@ class Monitor:
"""Log of last triggered item (sound or diffusion)."""
return self.logs.start().with_diff().first()
def __init__(self, streamer, delay, cancel_timeout, **kwargs):
def __init__(self, streamer, delay, **kwargs):
self.streamer = streamer
# adding time ensure all calculation have a margin
# adding time ensures all calculations have a margin
self.delay = delay + tz.timedelta(seconds=5)
self.cancel_timeout = cancel_timeout
self.__dict__.update(kwargs)
self.logs = self.get_logs_queryset()
self.init_last_sound_logs()
@ -117,18 +112,6 @@ class Monitor:
self.handle_diffusions()
self.sync()
def log(self, source, **kwargs):
"""Create a log using **kwargs, and print info."""
kwargs.setdefault("station", self.station)
kwargs.setdefault("date", tz.now())
log = Log(source=source, **kwargs)
log.save()
log.print()
if log.sound:
self.last_sound_logs[source] = log
return log
def trace_sound(self, source):
"""Return on air sound log (create if not present)."""
air_uri, air_time = source.uri, source.air_time
@ -246,6 +229,18 @@ class Monitor:
if diff.start < now - self.cancel_timeout:
self.cancel_diff(dealer, diff)
def log(self, source, **kwargs):
"""Create a log using **kwargs, and print info."""
kwargs.setdefault("station", self.station)
kwargs.setdefault("date", tz.now())
log = Log(source=source, **kwargs)
log.save()
log.print()
if log.sound:
self.last_sound_logs[source] = log
return log
def start_diff(self, source, diff):
playlist = Sound.objects.episode(id=diff.episode_id).playlist()
source.push(*playlist)
@ -272,7 +267,7 @@ class Monitor:
if self.sync_next is not None and now < self.sync_next:
return
self.sync_next = now + tz.timedelta(minutes=self.sync_timeout)
self.sync_next = now + self.sync_timeout
for source in self.streamer.playlists:
source.sync()