2020-04-15 20:29:59 +02:00
|
|
|
from django.dispatch import receiver
|
2020-05-31 12:16:09 +02:00
|
|
|
from django.db.models.signals import post_save
|
2020-04-26 19:46:58 +02:00
|
|
|
from rtmp.signals import stream_active
|
2020-04-15 20:29:59 +02:00
|
|
|
from .models import RestreamConfig
|
2020-04-23 21:08:39 +02:00
|
|
|
from rtmp.models import Stream
|
2020-04-26 19:46:58 +02:00
|
|
|
from concierge.models import Task
|
2020-04-20 14:51:44 +02:00
|
|
|
|
2020-04-15 20:29:59 +02:00
|
|
|
|
2020-04-26 19:46:58 +02:00
|
|
|
@receiver(stream_active)
|
|
|
|
def create_tasks(sender, **kwargs):
|
|
|
|
stream = Stream.objects.get(stream=kwargs['stream'])
|
2020-04-26 22:25:58 +02:00
|
|
|
instances = RestreamConfig.objects.filter(active=True, stream=stream)
|
2020-05-31 12:16:09 +02:00
|
|
|
for instance in instances:
|
|
|
|
task = Task(stream=instance.stream, type='restream', config_id=instance.id,
|
|
|
|
configuration=instance.get_json_config())
|
|
|
|
task.save()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=RestreamConfig)
|
|
|
|
def update_tasks(sender, **kwargs):
|
|
|
|
instance = kwargs['instance']
|
|
|
|
# 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:
|
2020-05-31 15:22:05 +02:00
|
|
|
task = Task.objects.filter(type='restream', config_id=instance.id).get()
|
2020-05-31 12:16:09 +02:00
|
|
|
task.delete()
|
|
|
|
except Task.DoesNotExist:
|
|
|
|
pass
|
2020-04-26 22:25:58 +02:00
|
|
|
|
2020-05-31 12:16:09 +02:00
|
|
|
# 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())
|
2020-04-26 19:46:58 +02:00
|
|
|
task.save()
|
2020-05-31 15:20:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_delete, sender=RestreamConfig)
|
|
|
|
def delete_tasks(sender, **kwargs):
|
|
|
|
instance = kwargs['instance']
|
|
|
|
# Get the current task instance if it exists, and remove it
|
|
|
|
try:
|
2020-05-31 15:22:05 +02:00
|
|
|
task = Task.objects.filter(type='restream', config_id=instance.id).get()
|
2020-05-31 15:20:22 +02:00
|
|
|
task.delete()
|
|
|
|
except Task.DoesNotExist:
|
|
|
|
pass
|