timeout before cancel a diffusion in monitoring
This commit is contained in:
		@ -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:
 | 
			
		||||
 | 
			
		||||
@ -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'),
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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(' ')]
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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')
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user