forked from rc/aircox
		
	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(' ')]
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user