This commit is contained in:
42
backlogger/management/commands/run_hltb_worker.py
Normal file
42
backlogger/management/commands/run_hltb_worker.py
Normal file
@@ -0,0 +1,42 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user