timeout before cancel a diffusion in monitoring

This commit is contained in:
bkfox 2016-07-19 23:13:04 +02:00
parent 1be3bf1e74
commit 4bbffa9a50
8 changed files with 63 additions and 17 deletions

View File

@ -38,21 +38,28 @@ class Command (BaseCommand):
) )
group = parser.add_argument_group('options') 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( group.add_argument(
'-s', '--station', type=str, action='append', '-s', '--station', type=str, action='append',
help='name of the station to monitor instead of monitoring ' help='name of the station to monitor instead of monitoring '
'all stations' 'all stations'
) )
group.add_argument( group.add_argument(
'-d', '--delay', type=int, '-t', '--timeout', type=int,
default=1000, default=600,
help='time to sleep in milliseconds between two updates when we ' help='time to wait in SECONDS before canceling a diffusion that '
'monitor' 'has not been ran but should have been. If 0, does not '
'check'
) )
def handle (self, *args, def handle (self, *args,
config = None, run = None, monitor = None, config = None, run = None, monitor = None,
station = [], delay = 1000, station = [], delay = 1000, timeout = 600,
**options): **options):
stations = Station.objects.filter(name__in = station)[:] \ stations = Station.objects.filter(name__in = station)[:] \
@ -67,7 +74,10 @@ class Command (BaseCommand):
station.controller.process_run() station.controller.process_run()
if monitor: if monitor:
monitors = [ Monitor(station) for station in stations ] monitors = [
Monitor(station, cancel_timeout = timeout)
for station in stations
]
delay = delay / 1000 delay = delay / 1000
while True: while True:
for monitor in monitors: for monitor in monitors:

View File

@ -358,6 +358,10 @@ class Log(programs.Related):
""" """
Source starts to be preload related_object Source starts to be preload related_object
""" """
other = 0x03
"""
Other log
"""
type = models.SmallIntegerField( type = models.SmallIntegerField(
verbose_name = _('type'), verbose_name = _('type'),

View File

@ -20,10 +20,16 @@ class Monitor:
""" """
station = None station = None
controller = 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() Log.objects.all().delete()
self.station = station self.station = station
self.__dict__.update(kwargs)
def monitor(self): def monitor(self):
""" """
@ -107,6 +113,34 @@ class Monitor:
related = track 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): def __current_diff(self):
""" """
Return a tuple with the currently running diffusion and the items Return a tuple with the currently running diffusion and the items

View File

@ -42,7 +42,6 @@ class StationController(plugins.StationController):
def fetch(self): def fetch(self):
super().fetch() super().fetch()
rid = self._send('request.on_air').split(' ')[0] rid = self._send('request.on_air').split(' ')[0]
if ' ' in rid: if ' ' in rid:
rid = rid[:rid.index(' ')] rid = rid[:rid.index(' ')]

View File

@ -27,7 +27,7 @@ class StreamInline(admin.TabularInline):
class SoundInline(admin.TabularInline): class SoundInline(admin.TabularInline):
fields = ['type', 'path', 'duration'] fields = ['type', 'path', 'duration']
readonly_fields = fields # readonly_fields = fields
model = Sound model = Sound
extra = 0 extra = 0
@ -148,10 +148,6 @@ class DiffusionAdmin(admin.ModelAdmin):
def get_queryset(self, request): def get_queryset(self, request):
qs = super().get_queryset(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): if request.GET and len(request.GET):
return qs return qs
return qs.exclude(type = Diffusion.Type.unconfirmed) return qs.exclude(type = Diffusion.Type.unconfirmed)

View File

@ -587,7 +587,7 @@ class DiffusionManager(models.Manager):
date. date.
""" """
date = date or tz.now() date = date or tz.now()
if issubclass(type(date), datetime.date): if not issubclass(type(date), datetime.datetime):
return self.filter( return self.filter(
models.Q(start__contains = date) | \ models.Q(start__contains = date) | \
models.Q(end__contains = date) models.Q(end__contains = date)

View File

@ -1,5 +1,6 @@
from django.contrib import admin from django.contrib import admin
from suit.admin import SortableTabularInline, SortableModelAdmin from suit.admin import SortableTabularInline, SortableModelAdmin
from suit.admin import SortableGenericTabularInline
import aircox.programs.models as programs import aircox.programs.models as programs
import aircox.cms.admin as cms import aircox.cms.admin as cms
@ -7,12 +8,14 @@ import aircox.website.models as models
import aircox.website.forms as forms import aircox.website.forms as forms
class TrackInline(SortableTabularInline): class TrackInline(SortableGenericTabularInline):
fields = ['artist', 'name', 'tags', 'position'] ct_field = 'related_type'
ct_fk_field = 'related_id'
form = forms.TrackForm form = forms.TrackForm
model = programs.Track model = programs.Track
sortable = 'position' sortable = 'position'
extra = 4 extra = 4
fields = ['artist', 'title', 'tags', 'info', 'position']
admin.site.register(models.Article, cms.PostAdmin) admin.site.register(models.Article, cms.PostAdmin)
admin.site.register(models.Program, cms.RelatedPostAdmin) admin.site.register(models.Program, cms.RelatedPostAdmin)

View File

@ -122,7 +122,7 @@ class Diffusion(cms.RelatedPost):
self.rel_to_post() self.rel_to_post()
self.fill_empty() self.fill_empty()
if not self.subtitle: if not self.subtitle and hasattr(self, 'related'):
self.subtitle = _('Diffusion of the %(date)s') % { self.subtitle = _('Diffusion of the %(date)s') % {
'date': self.related.start.strftime('%A %d/%m') 'date': self.related.start.strftime('%A %d/%m')
} }