mirror of
https://github.com/Dispatcharr/Dispatcharr.git
synced 2026-07-22 09:37:57 +00:00
File changes: Backend apps/channels/api_views.py — Added extend() action that uses queryset .update() to bypass signals, letting the running task pick up the new end_time via polling. Moved WS recording_stopped notification to synchronous (before HTTP response). Refactored destroy() to delete DB row first, send WS event immediately, defer cleanup to background thread. apps/channels/signals.py — Added pre_save guard to skip task revocation when status is "recording". Added post_save reentrancy guard to skip artwork prefetch for internal field-only saves (custom_properties, task_id, end_time). apps/channels/tasks.py — Added 2-second polling loop in streaming that checks for stop, deletion, and extended end_time. Added refresh_from_db() + merge before metadata save to prevent overwriting concurrent artwork prefetch. Replaced inline PeriodicTask cleanup with revoke_task()/_dvr_task_name(). Consolidated redundant DB queries in error path. Fixed TOCTOU os.path.exists + os.remove. Preserved "stopped" status in final-status logic instead of overwriting with "completed". core/utils.py — Made send_websocket_update gevent-safe by detecting monkey-patching and offloading async_to_sync to gevent's native threadpool, fixing the event loop deadlock that caused cross-recording interference. apps/proxy/ts_proxy/client_manager.py — Moved _trigger_stats_update to a background thread so remove_client() returns immediately. apps/proxy/ts_proxy/stream_generator.py — Added Redis-based health check for non-owner workers in _should_send_keepalive(), so keepalives fire correctly even when stream_manager is None. Frontend frontend/src/WebSocket.jsx — Added 400ms debounced scheduleRecordingFetch() replacing all inline fetchRecordings() calls. Added recording_extended and recording_updated event handlers. recording_cancelled now does surgical removeRecording() by ID when available. frontend/src/api.js — Added extendRecording(id, extraMinutes). frontend/src/components/cards/RecordingCard.jsx — Added Extend menu (+15m/+30m/+1h) for in-progress recordings. Widened Comskip button to stopped/interrupted statuses. frontend/src/components/forms/ProgramRecordingModal.jsx — Removed manual fetchRecordings() calls, relying on WS-driven debounced refresh. frontend/src/components/forms/RecordingDetailsModal.jsx — Added stopped to playable statuses. Widened Comskip button. Removed manual fetchRecordings(). frontend/src/components/forms/RecurringRuleModal.jsx — Removed all manual fetchRecordings() calls, relying on WS events. frontend/src/pages/Guide.jsx — Removed isLoading subscription that caused loading flash on debounced refresh. Removed manual fetchRecordings() after saving a series rule. frontend/src/store/channels.jsx — Stripped isLoading/error state from fetchRecordings to prevent full-page loading indicators. Added no-op guards in removeRecording. frontend/src/utils/cards/RecordingCardUtils.js — Added extendRecordingById wrapper. Tests apps/channels/tests/test_recording_extend.py (new) — 14 tests: extend endpoint validation, stacked extensions, signal bypass, pre_save guard behavior. apps/channels/tests/test_recording_stop_cancel.py (new) — 18 tests: stop endpoint, DVR client teardown, cancel was_in_progress flag, run_recording race guards, signal reentrancy guards. apps/channels/tests/test_ts_proxy_keepalive.py (new) — 21 tests: keepalive owner/non-owner logic, stats update error handling, client removal non-blocking, timing invariants. apps/channels/tests/test_recording_scheduling.py — Updated mock assertion to match new _stop_dvr_clients call signature. Summary: Adds an Extend action for in-progress recordings that bypasses Django signals and lets the running task dynamically pick up the new end_time via its 2-second DB polling loop. Fixes cross-recording interference caused by send_websocket_update deadlocking the gevent event loop. Fixes an artwork race condition where run_recording overwrote concurrent prefetch data. Adds Redis-based keepalive health checks for non-owner workers to prevent DVR read timeouts during source transitions. Replaces manual fetchRecordings() calls throughout the frontend with a debounced WS-driven refresh. 53 new tests.
234 lines
9.6 KiB
Python
234 lines
9.6 KiB
Python
# apps/channels/signals.py
|
|
|
|
from django.db.models.signals import m2m_changed, pre_save, post_save, post_delete
|
|
from django.dispatch import receiver
|
|
from django.utils.timezone import now, is_aware, make_aware
|
|
from celery.result import AsyncResult
|
|
from django_celery_beat.models import ClockedSchedule, PeriodicTask
|
|
from .models import Channel, Stream, ChannelProfile, ChannelProfileMembership, Recording
|
|
from apps.m3u.models import M3UAccount
|
|
from apps.epg.tasks import parse_programs_for_tvg_id
|
|
import json
|
|
import logging
|
|
from .tasks import run_recording, prefetch_recording_artwork
|
|
from datetime import timedelta
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@receiver(m2m_changed, sender=Channel.streams.through)
|
|
def update_channel_tvg_id_and_logo(sender, instance, action, reverse, model, pk_set, **kwargs):
|
|
"""
|
|
Whenever streams are added to a channel:
|
|
1) If the channel doesn't have a tvg_id, fill it from the first newly-added stream that has one.
|
|
"""
|
|
# We only care about post_add, i.e. once the new streams are fully associated
|
|
if action == "post_add":
|
|
# --- 1) Populate channel.tvg_id if empty ---
|
|
if not instance.tvg_id:
|
|
# Look for newly added streams that have a nonempty tvg_id
|
|
streams_with_tvg = model.objects.filter(pk__in=pk_set).exclude(tvg_id__exact='')
|
|
if streams_with_tvg.exists():
|
|
instance.tvg_id = streams_with_tvg.first().tvg_id
|
|
instance.save(update_fields=['tvg_id'])
|
|
|
|
@receiver(pre_save, sender=Stream)
|
|
def set_default_m3u_account(sender, instance, **kwargs):
|
|
"""
|
|
This function will be triggered before saving a Stream instance.
|
|
It sets the default m3u_account if not provided.
|
|
"""
|
|
if not instance.m3u_account:
|
|
instance.is_custom = True
|
|
default_account = M3UAccount.get_custom_account()
|
|
|
|
if default_account:
|
|
instance.m3u_account = default_account
|
|
else:
|
|
raise ValueError("No default M3UAccount found.")
|
|
|
|
@receiver(post_save, sender=Stream)
|
|
def generate_custom_stream_hash(sender, instance, created, **kwargs):
|
|
"""
|
|
Generate a stable stream_hash for custom streams after creation.
|
|
Uses the stream's ID to ensure the hash never changes even if name/url is edited.
|
|
"""
|
|
if instance.is_custom and not instance.stream_hash and created:
|
|
import hashlib
|
|
# Use stream ID for a stable, unique hash that never changes
|
|
unique_string = f"custom_stream_{instance.id}"
|
|
instance.stream_hash = hashlib.sha256(unique_string.encode()).hexdigest()
|
|
# Use update to avoid triggering signals again
|
|
Stream.objects.filter(id=instance.id).update(stream_hash=instance.stream_hash)
|
|
|
|
@receiver(post_save, sender=Channel)
|
|
def refresh_epg_programs(sender, instance, created, **kwargs):
|
|
"""
|
|
When a channel is saved, check if the EPG data has changed.
|
|
If so, trigger a refresh of the program data for the EPG.
|
|
"""
|
|
# Check if this is an update (not a new channel) and the epg_data has changed
|
|
if not created and kwargs.get('update_fields') and 'epg_data' in kwargs['update_fields']:
|
|
logger.info(f"Channel {instance.id} ({instance.name}) EPG data updated, refreshing program data")
|
|
if instance.epg_data:
|
|
logger.info(f"Triggering EPG program refresh for {instance.epg_data.tvg_id}")
|
|
parse_programs_for_tvg_id.delay(instance.epg_data.id)
|
|
# For new channels with EPG data, also refresh
|
|
elif created and instance.epg_data:
|
|
logger.info(f"New channel {instance.id} ({instance.name}) created with EPG data, refreshing program data")
|
|
parse_programs_for_tvg_id.delay(instance.epg_data.id)
|
|
|
|
@receiver(post_save, sender=ChannelProfile)
|
|
def create_profile_memberships(sender, instance, created, **kwargs):
|
|
if created:
|
|
channels = Channel.objects.all()
|
|
ChannelProfileMembership.objects.bulk_create([
|
|
ChannelProfileMembership(channel_profile=instance, channel=channel)
|
|
for channel in channels
|
|
])
|
|
|
|
def _dvr_task_name(recording_id):
|
|
"""Predictable PeriodicTask name for a DVR recording."""
|
|
return f"dvr-recording-{recording_id}"
|
|
|
|
|
|
def schedule_recording_task(instance, eta=None):
|
|
"""Schedule a recording task via ClockedSchedule + one-off PeriodicTask.
|
|
|
|
The task is stored in the database and dispatched by Celery Beat at the
|
|
scheduled time with no countdown. This avoids the Redis visibility_timeout
|
|
redelivery bug that caused duplicate recordings when using apply_async
|
|
with long countdowns.
|
|
"""
|
|
if eta is None:
|
|
eta = instance.start_time
|
|
if eta is not None and not is_aware(eta):
|
|
eta = make_aware(eta)
|
|
# Clamp to now so Beat dispatches immediately for past/current start times
|
|
if eta <= now():
|
|
eta = now()
|
|
|
|
task_args = [
|
|
instance.id,
|
|
instance.channel_id,
|
|
str(instance.start_time),
|
|
str(instance.end_time),
|
|
]
|
|
|
|
clocked, _ = ClockedSchedule.objects.get_or_create(clocked_time=eta)
|
|
task_name = _dvr_task_name(instance.id)
|
|
PeriodicTask.objects.update_or_create(
|
|
name=task_name,
|
|
defaults={
|
|
"task": "apps.channels.tasks.run_recording",
|
|
"clocked": clocked,
|
|
"args": json.dumps(task_args),
|
|
"one_off": True,
|
|
"enabled": True,
|
|
"interval": None,
|
|
"crontab": None,
|
|
"solar": None,
|
|
},
|
|
)
|
|
return task_name
|
|
|
|
|
|
def revoke_task(task_id):
|
|
"""Cancel a pending recording task.
|
|
|
|
task_id is normally a PeriodicTask name (e.g. "dvr-recording-42").
|
|
For backwards compatibility with legacy Celery async-result UUIDs,
|
|
falls back to AsyncResult.revoke().
|
|
"""
|
|
if not task_id:
|
|
return
|
|
# Primary path: delete the PeriodicTask and clean up its ClockedSchedule
|
|
try:
|
|
pt = PeriodicTask.objects.get(name=task_id)
|
|
old_clocked = pt.clocked
|
|
pt.delete()
|
|
if old_clocked and not PeriodicTask.objects.filter(clocked=old_clocked).exists():
|
|
old_clocked.delete()
|
|
return
|
|
except PeriodicTask.DoesNotExist:
|
|
pass
|
|
# Fallback for legacy Celery task UUIDs
|
|
try:
|
|
AsyncResult(task_id).revoke()
|
|
except Exception:
|
|
pass
|
|
|
|
@receiver(pre_save, sender=Recording)
|
|
def revoke_old_task_on_update(sender, instance, **kwargs):
|
|
if not instance.pk:
|
|
return # New instance
|
|
try:
|
|
old = Recording.objects.get(pk=instance.pk)
|
|
if old.task_id and (
|
|
old.start_time != instance.start_time or
|
|
old.end_time != instance.end_time or
|
|
old.channel_id != instance.channel_id
|
|
):
|
|
# Do NOT revoke while the recording is actively streaming.
|
|
# run_recording re-reads end_time from the DB every ~2 s and extends
|
|
# its internal deadline dynamically — revoking here would kill the task.
|
|
old_status = (old.custom_properties or {}).get("status", "")
|
|
if old_status == "recording":
|
|
return
|
|
revoke_task(old.task_id)
|
|
instance.task_id = None
|
|
except Recording.DoesNotExist:
|
|
pass
|
|
|
|
@receiver(post_save, sender=Recording)
|
|
def schedule_task_on_save(sender, instance, created, **kwargs):
|
|
try:
|
|
# Skip processing for internal field-only saves (metadata updates,
|
|
# task_id assignment, end_time extensions) to prevent re-entrant
|
|
# artwork dispatch and redundant recording_updated WS events.
|
|
update_fields = kwargs.get('update_fields')
|
|
if not created and update_fields is not None and set(update_fields) <= {'custom_properties', 'task_id', 'end_time'}:
|
|
return
|
|
|
|
if not instance.task_id:
|
|
start_time = instance.start_time
|
|
|
|
# Make both datetimes aware (in UTC)
|
|
if not is_aware(start_time):
|
|
print("Start time was not aware, making aware")
|
|
start_time = make_aware(start_time)
|
|
|
|
current_time = now()
|
|
|
|
# Debug log
|
|
print(f"Start time: {start_time}, Now: {current_time}")
|
|
|
|
# Optionally allow slight fudge factor (1 second) to ensure scheduling happens
|
|
if start_time > current_time - timedelta(seconds=1):
|
|
print("Scheduling recording task!")
|
|
# Pass the corrected, timezone-aware start_time explicitly so
|
|
# schedule_recording_task uses it as the Celery ETA rather than
|
|
# re-reading instance.start_time which may still be naive.
|
|
task_id = schedule_recording_task(instance, eta=start_time)
|
|
instance.task_id = task_id
|
|
instance.save(update_fields=['task_id'])
|
|
else:
|
|
print("Start time is in the past. Not scheduling.")
|
|
# Kick off poster/artwork prefetch to enrich Upcoming cards.
|
|
# Skip when the recording is already active or finished — run_recording
|
|
# handles its own poster resolution, and scheduling artwork prefetch
|
|
# while the task is running causes a race that can overwrite status.
|
|
cp = instance.custom_properties or {}
|
|
rec_status = cp.get("status", "")
|
|
if rec_status not in ("recording", "completed", "stopped", "interrupted"):
|
|
try:
|
|
prefetch_recording_artwork.apply_async(args=[instance.id], countdown=1)
|
|
except Exception as e:
|
|
print("Error scheduling artwork prefetch:", e)
|
|
except Exception as e:
|
|
import traceback
|
|
print("Error in post_save signal:", e)
|
|
traceback.print_exc()
|
|
|
|
@receiver(post_delete, sender=Recording)
|
|
def revoke_task_on_delete(sender, instance, **kwargs):
|
|
revoke_task(instance.task_id)
|