feat(main): main
This commit is contained in:
56
core/management/commands/run_cache_worker.py
Normal file
56
core/management/commands/run_cache_worker.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
management command: run_cache_worker
|
||||
|
||||
Runs continuously in the background to automatically download and cache
|
||||
upcoming programming for the next 24 hours. Intended to run as a daemon
|
||||
or Docker service.
|
||||
"""
|
||||
|
||||
import time
|
||||
import logging
|
||||
from django.core.management.base import BaseCommand
|
||||
from core.services.cache import run_cache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Run the 24-hour ahead cache worker continuously in the background."
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"--interval",
|
||||
type=int,
|
||||
default=600,
|
||||
help="Interval in seconds between cache runs (default: 600s/10m).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--hours",
|
||||
type=int,
|
||||
default=24,
|
||||
help="How many hours ahead to scan for upcoming airings (default: 24).",
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
interval = options["interval"]
|
||||
hours = options["hours"]
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f"Starting continuous cache worker (interval: {interval}s, ahead: {hours}h)"))
|
||||
|
||||
while True:
|
||||
try:
|
||||
self.stdout.write(f"▶ Running background cache worker (window: {hours}h)")
|
||||
result = run_cache(hours=hours, prune_only=False)
|
||||
|
||||
if result["downloaded"] > 0 or result["pruned"] > 0 or result["failed"] > 0:
|
||||
self.stdout.write(self.style.SUCCESS(f" 🗑 Pruned: {result['pruned']}"))
|
||||
self.stdout.write(self.style.SUCCESS(f" ↓ Downloaded: {result['downloaded']}"))
|
||||
self.stdout.write(self.style.SUCCESS(f" ✓ Already cached: {result['already_cached']}"))
|
||||
if result["failed"]:
|
||||
self.stderr.write(self.style.ERROR(f" ✗ Failed: {result['failed']}"))
|
||||
|
||||
except Exception as e:
|
||||
self.stderr.write(self.style.ERROR(f"Error in cache worker loop: {e}"))
|
||||
logger.error(f"Error in cache worker loop: {e}")
|
||||
|
||||
# Sleep until next interval
|
||||
time.sleep(interval)
|
||||
Reference in New Issue
Block a user