From 4bbffa9a50d62d52608abfb7b2f7b92dc4f3a2fa Mon Sep 17 00:00:00 2001 From: bkfox Date: Tue, 19 Jul 2016 23:13:04 +0200 Subject: [PATCH] timeout before cancel a diffusion in monitoring --- .../management/commands/controllers.py | 22 ++++++++---- controllers/models.py | 4 +++ controllers/monitor.py | 36 ++++++++++++++++++- controllers/plugins/liquidsoap.py | 1 - programs/admin.py | 6 +--- programs/models.py | 2 +- website/admin.py | 7 ++-- website/models.py | 2 +- 8 files changed, 63 insertions(+), 17 deletions(-) diff --git a/controllers/management/commands/controllers.py b/controllers/management/commands/controllers.py index 733a87f..a1f990e 100644 --- a/controllers/management/commands/controllers.py +++ b/controllers/management/commands/controllers.py @@ -38,21 +38,28 @@ class Command (BaseCommand): ) group = parser.add_argument_group('options') + group.add_argument( + '-d', '--delay', type=int, + default=1000, + help='time to sleep in MILLISECONDS between two updates when we ' + 'monitor' + ) group.add_argument( '-s', '--station', type=str, action='append', help='name of the station to monitor instead of monitoring ' 'all stations' ) group.add_argument( - '-d', '--delay', type=int, - default=1000, - help='time to sleep in milliseconds between two updates when we ' - 'monitor' + '-t', '--timeout', type=int, + default=600, + help='time to wait in SECONDS before canceling a diffusion that ' + 'has not been ran but should have been. If 0, does not ' + 'check' ) def handle (self, *args, config = None, run = None, monitor = None, - station = [], delay = 1000, + station = [], delay = 1000, timeout = 600, **options): stations = Station.objects.filter(name__in = station)[:] \ @@ -67,7 +74,10 @@ class Command (BaseCommand): station.controller.process_run() if monitor: - monitors = [ Monitor(station) for station in stations ] + monitors = [ + Monitor(station, cancel_timeout = timeout) + for station in stations + ] delay = delay / 1000 while True: for monitor in monitors: diff --git a/controllers/models.py b/controllers/models.py index c1c2cc7..541658c 100644 --- a/controllers/models.py +++ b/controllers/models.py @@ -358,6 +358,10 @@ class Log(programs.Related): """ Source starts to be preload related_object """ + other = 0x03 + """ + Other log + """ type = models.SmallIntegerField( verbose_name = _('type'), diff --git a/controllers/monitor.py b/controllers/monitor.py index 6edeb8f..de89ba8 100644 --- a/controllers/monitor.py +++ b/controllers/monitor.py @@ -20,10 +20,16 @@ class Monitor: """ station = None controller = None + cancel_timeout = 60*10 + """ + Time in seconds before a diffusion that have archives is cancelled + because it has not been played. + """ - def __init__(self, station): + def __init__(self, station, **kwargs): Log.objects.all().delete() self.station = station + self.__dict__.update(kwargs) def monitor(self): """ @@ -107,6 +113,34 @@ class Monitor: related = track ) + def trace_canceled(self): + """ + Check diffusions that should have been played but did not start, + and cancel them + """ + if not self.cancel_timeout: + return + + diffs = programs.objects.get_at().filter( + type = programs.Diffusion.Type.normal, + sound__type = programs.Sound.Type.archive, + ) + logs = station.get_played(models = programs.Diffusion) + + date = tz.now() - datetime.timedelta(seconds = self.cancel_timeout) + for diff in diffs: + if logs.filter(related = diff): + continue + if diff.start < now: + diff.type = programs.Diffusion.Type.canceled + diff.save() + self.log( + type = Log.Type.other, + related = diff, + comment = 'Diffusion canceled after {} seconds' \ + .format(self.cancel_timeout) + ) + def __current_diff(self): """ Return a tuple with the currently running diffusion and the items diff --git a/controllers/plugins/liquidsoap.py b/controllers/plugins/liquidsoap.py index 15531a4..5a0b0b2 100644 --- a/controllers/plugins/liquidsoap.py +++ b/controllers/plugins/liquidsoap.py @@ -42,7 +42,6 @@ class StationController(plugins.StationController): def fetch(self): super().fetch() - rid = self._send('request.on_air').split(' ')[0] if ' ' in rid: rid = rid[:rid.index(' ')] diff --git a/programs/admin.py b/programs/admin.py index 127b928..f42cae0 100755 --- a/programs/admin.py +++ b/programs/admin.py @@ -27,7 +27,7 @@ class StreamInline(admin.TabularInline): class SoundInline(admin.TabularInline): fields = ['type', 'path', 'duration'] - readonly_fields = fields + # readonly_fields = fields model = Sound extra = 0 @@ -148,10 +148,6 @@ class DiffusionAdmin(admin.ModelAdmin): def get_queryset(self, request): qs = super().get_queryset(request) - print('type__exact' in request.GET, - request.GET.get('type__exact'), - Diffusion.Type.unconfirmed, - request.GET.get('type__exact') == Diffusion.Type.unconfirmed) if request.GET and len(request.GET): return qs return qs.exclude(type = Diffusion.Type.unconfirmed) diff --git a/programs/models.py b/programs/models.py index 019e1ec..a795625 100755 --- a/programs/models.py +++ b/programs/models.py @@ -587,7 +587,7 @@ class DiffusionManager(models.Manager): date. """ date = date or tz.now() - if issubclass(type(date), datetime.date): + if not issubclass(type(date), datetime.datetime): return self.filter( models.Q(start__contains = date) | \ models.Q(end__contains = date) diff --git a/website/admin.py b/website/admin.py index 71d03d6..0678b73 100644 --- a/website/admin.py +++ b/website/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin from suit.admin import SortableTabularInline, SortableModelAdmin +from suit.admin import SortableGenericTabularInline import aircox.programs.models as programs import aircox.cms.admin as cms @@ -7,12 +8,14 @@ import aircox.website.models as models import aircox.website.forms as forms -class TrackInline(SortableTabularInline): - fields = ['artist', 'name', 'tags', 'position'] +class TrackInline(SortableGenericTabularInline): + ct_field = 'related_type' + ct_fk_field = 'related_id' form = forms.TrackForm model = programs.Track sortable = 'position' extra = 4 + fields = ['artist', 'title', 'tags', 'info', 'position'] admin.site.register(models.Article, cms.PostAdmin) admin.site.register(models.Program, cms.RelatedPostAdmin) diff --git a/website/models.py b/website/models.py index 359aeb9..af79720 100644 --- a/website/models.py +++ b/website/models.py @@ -122,7 +122,7 @@ class Diffusion(cms.RelatedPost): self.rel_to_post() self.fill_empty() - if not self.subtitle: + if not self.subtitle and hasattr(self, 'related'): self.subtitle = _('Diffusion of the %(date)s') % { 'date': self.related.start.strftime('%A %d/%m') }