Files
2026-04-11 22:07:59 +02:00

80 lines
2.6 KiB
Python

import signal
import time
import traceback
from datetime import timedelta
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import close_old_connections
from django.utils import timezone
from app.utils import send_sync_channel_message
from torrent.models import Torrent
from torrent.utils import transmission_handler
def update_transmission_data():
data = transmission_handler.get_all_data()
updated_torrents = []
for torrent in Torrent.objects.all():
if torrent.id in data and torrent.transmission_data != data[torrent.id]:
torrent.transmission_data = data[torrent.id]
updated_torrents.append(torrent)
if updated_torrents:
Torrent.objects.bulk_update(updated_torrents, ["transmission_data"])
send_sync_channel_message(
"torrent",
"transmission_data_updated",
{torrent.id: torrent.transmission_data for torrent in updated_torrents},
)
def clean_old_torrents():
expired_date = timezone.now() - timedelta(seconds=settings.TORRENT_TTL)
torrent = Torrent.objects.filter(date_created__lt=expired_date).first()
if torrent:
print(f"delete torrent {torrent.name}")
torrent.delete()
def update_peer_port():
transmission_handler.update_vpn_port()
class Command(BaseCommand):
task_schedule = {
"update_transmission_data": {"func": update_transmission_data, "schedule": 5.0},
"clean_old_torrents": {"func": clean_old_torrents, "schedule": 5.0},
"update_peer_port": {"func": update_peer_port, "schedule": 10.0},
}
histories = {}
run = True
def handle(self, *args, **options):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
self.stdout.write(self.style.SUCCESS("start"))
while self.run:
for name, task in self.task_schedule.items():
if (
name not in self.histories
or time.time() - self.histories[name] > task["schedule"]
):
self.call_func(name)
time.sleep(1)
def exit_gracefully(self, signum, frame):
self.stdout.write(self.style.SUCCESS("exit"))
self.run = False
def call_func(self, name):
close_old_connections()
try:
self.task_schedule[name]["func"]()
self.histories[name] = time.time()
except Exception as e:
tb = traceback.format_exc()
self.stderr.write(self.style.ERROR(f"Error in {name}: {e}\n{tb}"))