From 7dc93e44396b8fac2561e7fcc42e3fd57ff662ad Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 15 Dec 2018 16:00:45 -0600 Subject: [PATCH] First pass on username update background task --- api/commands.py | 2 +- apps.py | 4 +++- background_tasks.py | 21 +++++++++++++++++++ ...tAccountInfoGrabber.py => minecraft_api.py | 0 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 background_tasks.py rename api/MinecraftAccountInfoGrabber.py => minecraft_api.py (100%) diff --git a/api/commands.py b/api/commands.py index 43da6e4..4118c2a 100644 --- a/api/commands.py +++ b/api/commands.py @@ -1,7 +1,7 @@ from django.db.models import Q, F from GeoffreyApp.models import * from GeoffreyApp.api.BotErrors import * -from GeoffreyApp.api.MinecraftAccountInfoGrabber import * +from GeoffreyApp.minecraft_api import * post_list = [] get_list = [] diff --git a/apps.py b/apps.py index a7c3efe..2335a09 100644 --- a/apps.py +++ b/apps.py @@ -11,7 +11,9 @@ class GeoffreyAppConfig(AppConfig): verbose_name = "Geoffrey: Minecraft Web Database" def ready(self): - subprocess.run('screen -X -S "{0}" quit'.format("geoffrey"), shell=True) + import GeoffreyApp.background_tasks as background_tasks + background_tasks.run_tasks() + #subprocess.run('screen -X -S "{0}" quit'.format("geoffrey"), shell=True) #subprocess.run( # 'screen -S geoffrey -d -m {} {}'.format(sys.executable, path), # shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) diff --git a/background_tasks.py b/background_tasks.py new file mode 100644 index 0000000..9dd8c31 --- /dev/null +++ b/background_tasks.py @@ -0,0 +1,21 @@ +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() diff --git a/api/MinecraftAccountInfoGrabber.py b/minecraft_api.py similarity index 100% rename from api/MinecraftAccountInfoGrabber.py rename to minecraft_api.py