Added update_mc_username management command

+ This replaces the internal background task to handle username updates
+ This command can be called from an external service to update
  usernames
doc_update
Joey Hines 2019-07-21 09:51:47 -05:00
parent fabc697492
commit 00ad1f429f
3 changed files with 29 additions and 25 deletions

View File

@ -1,5 +1,6 @@
from django.apps import AppConfig
import os
import background_task
path = os.path.dirname(os.path.abspath(__file__)) + "/assets/bots/geoffrey.py"
@ -7,7 +8,3 @@ path = os.path.dirname(os.path.abspath(__file__)) + "/assets/bots/geoffrey.py"
class GeoffreyAppConfig(AppConfig):
name = 'GeoffreyApp'
verbose_name = "Geoffrey: Minecraft Web Database"
def ready(self):
import GeoffreyApp.background_tasks as background_tasks
background_tasks.run_tasks()

View File

@ -1,21 +0,0 @@
from GeoffreyApp.models import Player
from GeoffreyApp.minecraft_api import grab_playername
from apscheduler.schedulers.background import BackgroundScheduler
def update_usernames():
players = Player.objects.all()
for player in players:
mc_username = grab_playername(player.mc_uuid)
if player.name != mc_username:
player.name = mc_username
player.save()
def run_tasks():
scheduler = BackgroundScheduler()
scheduler.add_job(update_usernames, 'interval', minutes=10)
scheduler.start()

View File

@ -0,0 +1,28 @@
from django.core.management.base import BaseCommand, CommandError
from GeoffreyApp.models import Player
from GeoffreyApp.minecraft_api import grab_playername
from GeoffreyApp.errors import UsernameLookupFailed
import logging
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = "Updates all the minecraft usernames in the database"
def handle(self, *args, **options):
players = Player.objects.all()
for player in players:
try:
mc_username = grab_playername(player.mc_uuid)
if player.name != mc_username:
player.name = mc_username
player.save()
except UsernameLookupFailed:
logger.error("Error updating MC UUID %s" % player.mc_uuid)
except Exception as e:
error_str = "Error updating MC usernames: %s" % e.__str__()
logger.error(error_str)
raise CommandError(error_str)
self.stdout.write("Done updating MC usernames")