Files
k-boris-website/backlogger/management/commands/run_hltb_worker.py
Boris c84600ae3e
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
fix hltb worker
2026-04-02 21:23:00 +03:00

43 lines
1.4 KiB
Python

import time
import logging
from django.core.management.base import BaseCommand
from django.utils import timezone
from datetime import timedelta
from backlogger.models import Item
from backlogger import hltb as hltb_api
logger = logging.getLogger(__name__)
INITIAL_DELAY_SECONDS = 20
BETWEEN_LOOKUPS_SECONDS = 5
IDLE_POLL_SECONDS = 10
class Command(BaseCommand):
help = 'Background worker: fetches HLTB data for newly created game items.'
def handle(self, *args, **options):
self.stdout.write('HLTB worker started.')
while True:
cutoff = timezone.now() - timedelta(seconds=INITIAL_DELAY_SECONDS)
item = (
Item.objects
.filter(category=Item.GAMES, hltb_fetched=False, created_at__lte=cutoff)
.order_by('created_at')
.first()
)
if item is None:
time.sleep(IDLE_POLL_SECONDS)
continue
self.stdout.write(f'Fetching HLTB for: {item.name} (id={item.pk})')
try:
hltb_api.apply_to_item(item)
except Exception as e:
logger.exception('HLTB lookup failed for item %s', item.pk)
# Mark as fetched anyway to avoid retrying indefinitely
item.hltb_fetched = True
item.save(update_fields=['hltb_fetched'])
time.sleep(BETWEEN_LOOKUPS_SECONDS)