dynamically update tasks on saved changes to restream configs
This commit is contained in:
		
							parent
							
								
									64758083b3
								
							
						
					
					
						commit
						88620b99af
					
				| 
						 | 
					@ -7,7 +7,7 @@ class IdentityAdmin(admin.ModelAdmin):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TaskAdmin(admin.ModelAdmin):
 | 
					class TaskAdmin(admin.ModelAdmin):
 | 
				
			||||||
    fields = ['stream', 'type', 'configuration', 'claimed_by']
 | 
					    fields = ['stream', 'type', 'config_id', 'configuration', 'claimed_by']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
admin.site.register(Identity, IdentityAdmin)
 | 
					admin.site.register(Identity, IdentityAdmin)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,19 @@
 | 
				
			||||||
 | 
					# Generated by Django 3.0.6 on 2020-05-31 09:51
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from django.db import migrations, models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dependencies = [
 | 
				
			||||||
 | 
					        ('concierge', '0005_auto_20200426_2007'),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    operations = [
 | 
				
			||||||
 | 
					        migrations.AddField(
 | 
				
			||||||
 | 
					            model_name='task',
 | 
				
			||||||
 | 
					            name='config_id',
 | 
				
			||||||
 | 
					            field=models.IntegerField(default=0),
 | 
				
			||||||
 | 
					            preserve_default=False,
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
| 
						 | 
					@ -21,5 +21,6 @@ class Task(models.Model):
 | 
				
			||||||
    uuid = models.UUIDField(unique=True, default=uuid.uuid4)
 | 
					    uuid = models.UUIDField(unique=True, default=uuid.uuid4)
 | 
				
			||||||
    stream = models.ForeignKey(Stream, on_delete=models.CASCADE)
 | 
					    stream = models.ForeignKey(Stream, on_delete=models.CASCADE)
 | 
				
			||||||
    type = models.CharField(max_length=100)
 | 
					    type = models.CharField(max_length=100)
 | 
				
			||||||
 | 
					    config_id = models.IntegerField()
 | 
				
			||||||
    configuration = models.TextField()
 | 
					    configuration = models.TextField()
 | 
				
			||||||
    claimed_by = models.ForeignKey(Identity, null=True, blank=True, on_delete=models.CASCADE)
 | 
					    claimed_by = models.ForeignKey(Identity, null=True, blank=True, on_delete=models.CASCADE)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,6 +3,7 @@ from django.urls import reverse
 | 
				
			||||||
from django.utils.translation import gettext_lazy as _
 | 
					from django.utils.translation import gettext_lazy as _
 | 
				
			||||||
from django.db.models.signals import pre_delete
 | 
					from django.db.models.signals import pre_delete
 | 
				
			||||||
from portier.common import handlers
 | 
					from portier.common import handlers
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from rtmp.models import Stream
 | 
					from rtmp.models import Stream
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,5 +27,14 @@ class RestreamConfig(models.Model):
 | 
				
			||||||
    def __str__(self):
 | 
					    def __str__(self):
 | 
				
			||||||
        return '{} to {}'.format(self.stream, self.name)
 | 
					        return '{} to {}'.format(self.stream, self.name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_json_config(self):
 | 
				
			||||||
 | 
					        config = {
 | 
				
			||||||
 | 
					            'name': self.name,
 | 
				
			||||||
 | 
					            'app': self.stream.application.name,
 | 
				
			||||||
 | 
					            'stream': str(self.stream.stream),
 | 
				
			||||||
 | 
					            'target': self.target
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return json.dumps(config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pre_delete.connect(handlers.remove_obj_perms_connected_with_user, sender=RestreamConfig)
 | 
					pre_delete.connect(handlers.remove_obj_perms_connected_with_user, sender=RestreamConfig)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,23 +1,35 @@
 | 
				
			||||||
from django.dispatch import receiver
 | 
					from django.dispatch import receiver
 | 
				
			||||||
 | 
					from django.db.models.signals import post_save
 | 
				
			||||||
from rtmp.signals import stream_active
 | 
					from rtmp.signals import stream_active
 | 
				
			||||||
from .models import RestreamConfig
 | 
					from .models import RestreamConfig
 | 
				
			||||||
from rtmp.models import Stream
 | 
					from rtmp.models import Stream
 | 
				
			||||||
from concierge.models import Task
 | 
					from concierge.models import Task
 | 
				
			||||||
import json
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@receiver(stream_active)
 | 
					@receiver(stream_active)
 | 
				
			||||||
def create_tasks(sender, **kwargs):
 | 
					def create_tasks(sender, **kwargs):
 | 
				
			||||||
    stream = Stream.objects.get(stream=kwargs['stream'])
 | 
					    stream = Stream.objects.get(stream=kwargs['stream'])
 | 
				
			||||||
    instances = RestreamConfig.objects.filter(active=True, stream=stream)
 | 
					    instances = RestreamConfig.objects.filter(active=True, stream=stream)
 | 
				
			||||||
    for inst in instances:
 | 
					    for instance in instances:
 | 
				
			||||||
        config = {
 | 
					        task = Task(stream=instance.stream, type='restream', config_id=instance.id,
 | 
				
			||||||
            'name': inst.name,
 | 
					                    configuration=instance.get_json_config())
 | 
				
			||||||
            'app': inst.stream.application.name,
 | 
					        task.save()
 | 
				
			||||||
            'stream': str(inst.stream.stream),
 | 
					
 | 
				
			||||||
            'target': inst.target
 | 
					
 | 
				
			||||||
        }
 | 
					@receiver(post_save, sender=RestreamConfig)
 | 
				
			||||||
 | 
					def update_tasks(sender, **kwargs):
 | 
				
			||||||
        json_config = json.dumps(config)
 | 
					    instance = kwargs['instance']
 | 
				
			||||||
        task = Task(stream=stream, type='restream', configuration=json_config)
 | 
					    # TODO: check for breaking changes using update_fields. This needs custom save_model functions though.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Get the current task instance if it exists, and remove it
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        task = Task.objects.filter(config_id=instance.id).get()
 | 
				
			||||||
 | 
					        task.delete()
 | 
				
			||||||
 | 
					    except Task.DoesNotExist:
 | 
				
			||||||
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # If the configuration is set to be active, and the stream is published, (re)create new task
 | 
				
			||||||
 | 
					    if instance.active and instance.stream.publish_counter > 0:
 | 
				
			||||||
 | 
					        task = Task(stream=instance.stream, type='restream', config_id=instance.id,
 | 
				
			||||||
 | 
					                    configuration=instance.get_json_config())
 | 
				
			||||||
        task.save()
 | 
					        task.save()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,39 @@
 | 
				
			||||||
 | 
					# Generated by Django 3.0.6 on 2020-05-31 09:51
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from django.db import migrations, models
 | 
				
			||||||
 | 
					import django.db.models.deletion
 | 
				
			||||||
 | 
					import uuid
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dependencies = [
 | 
				
			||||||
 | 
					        ('rtmp', '0004_auto_20200501_1302'),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    operations = [
 | 
				
			||||||
 | 
					        migrations.AlterModelOptions(
 | 
				
			||||||
 | 
					            name='application',
 | 
				
			||||||
 | 
					            options={'verbose_name': 'RTMP application', 'verbose_name_plural': 'RTMP applications'},
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					        migrations.AlterField(
 | 
				
			||||||
 | 
					            model_name='application',
 | 
				
			||||||
 | 
					            name='name',
 | 
				
			||||||
 | 
					            field=models.CharField(help_text='RTMP application name', max_length=100, unique=True),
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					        migrations.AlterField(
 | 
				
			||||||
 | 
					            model_name='stream',
 | 
				
			||||||
 | 
					            name='application',
 | 
				
			||||||
 | 
					            field=models.ForeignKey(help_text='Application which the stream is assigned to', on_delete=django.db.models.deletion.CASCADE, to='rtmp.Application'),
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					        migrations.AlterField(
 | 
				
			||||||
 | 
					            model_name='stream',
 | 
				
			||||||
 | 
					            name='name',
 | 
				
			||||||
 | 
					            field=models.CharField(help_text='Name for this stream', max_length=100),
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					        migrations.AlterField(
 | 
				
			||||||
 | 
					            model_name='stream',
 | 
				
			||||||
 | 
					            name='stream',
 | 
				
			||||||
 | 
					            field=models.UUIDField(default=uuid.uuid4, help_text='Stream ID for this stream', unique=True),
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
		Loading…
	
		Reference in New Issue