From f037d7fea6b1b1e7c3722cd42500cc7702591a1b Mon Sep 17 00:00:00 2001 From: Basraah Date: Mon, 11 Dec 2017 00:26:07 +1000 Subject: [PATCH] Change celery tasks to shared task decorator --- allianceauth/authentication/tasks.py | 6 +++--- allianceauth/corputils/tasks.py | 6 +++--- allianceauth/eveonline/tasks.py | 10 +++++----- allianceauth/services/modules/discord/tasks.py | 10 +++++----- allianceauth/services/modules/discourse/tasks.py | 6 +++--- allianceauth/services/modules/mumble/tasks.py | 6 +++--- allianceauth/services/modules/openfire/tasks.py | 6 +++--- allianceauth/services/modules/phpbb3/tasks.py | 6 +++--- allianceauth/services/modules/seat/tasks.py | 6 +++--- allianceauth/services/modules/smf/tasks.py | 6 +++--- allianceauth/services/modules/teamspeak3/tasks.py | 8 ++++---- allianceauth/services/tasks.py | 4 ++-- tests/__init__.py | 3 +++ {allianceauth => tests}/celery.py | 6 +++--- 14 files changed, 46 insertions(+), 43 deletions(-) rename {allianceauth => tests}/celery.py (73%) diff --git a/allianceauth/authentication/tasks.py b/allianceauth/authentication/tasks.py index 67620481..61cc49a5 100644 --- a/allianceauth/authentication/tasks.py +++ b/allianceauth/authentication/tasks.py @@ -2,14 +2,14 @@ import logging from esi.errors import TokenExpiredError, TokenInvalidError from esi.models import Token +from celery import shared_task from allianceauth.authentication.models import CharacterOwnership -from allianceauth.celery import app logger = logging.getLogger(__name__) -@app.task +@shared_task def check_character_ownership(owner_hash): tokens = Token.objects.filter(character_owner_hash=owner_hash) if tokens: @@ -31,7 +31,7 @@ def check_character_ownership(owner_hash): CharacterOwnership.objects.filter(owner_hash=owner_hash).delete() -@app.task +@shared_task def check_all_character_ownership(): for c in CharacterOwnership.objects.all().only('owner_hash'): check_character_ownership.delay(c.owner_hash) diff --git a/allianceauth/corputils/tasks.py b/allianceauth/corputils/tasks.py index d78ac3d2..40b6835a 100644 --- a/allianceauth/corputils/tasks.py +++ b/allianceauth/corputils/tasks.py @@ -1,14 +1,14 @@ -from allianceauth.celery import app +from celery import shared_task from allianceauth.corputils import CorpStats -@app.task +@shared_task def update_corpstats(pk): cs = CorpStats.objects.get(pk=pk) cs.update() -@app.task +@shared_task def update_all_corpstats(): for cs in CorpStats.objects.all(): update_corpstats.delay(cs.pk) diff --git a/allianceauth/eveonline/tasks.py b/allianceauth/eveonline/tasks.py index 115723f4..46fed923 100644 --- a/allianceauth/eveonline/tasks.py +++ b/allianceauth/eveonline/tasks.py @@ -1,6 +1,6 @@ import logging -from allianceauth.celery import app +from celery import shared_task from .models import EveAllianceInfo from .models import EveCharacter from .models import EveCorporationInfo @@ -8,22 +8,22 @@ from .models import EveCorporationInfo logger = logging.getLogger(__name__) -@app.task +@shared_task def update_corp(corp_id): EveCorporationInfo.objects.update_corporation(corp_id) -@app.task +@shared_task def update_alliance(alliance_id): EveAllianceInfo.objects.update_alliance(alliance_id).populate_alliance() -@app.task +@shared_task def update_character(character_id): EveCharacter.objects.update_character(character_id) -@app.task +@shared_task def run_model_update(): # update existing corp models for corp in EveCorporationInfo.objects.all().values('corporation_id'): diff --git a/allianceauth/services/modules/discord/tasks.py b/allianceauth/services/modules/discord/tasks.py index 359831b1..93ccc482 100644 --- a/allianceauth/services/modules/discord/tasks.py +++ b/allianceauth/services/modules/discord/tasks.py @@ -4,8 +4,8 @@ from django.conf import settings from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist from allianceauth.notifications import notify +from celery import shared_task -from allianceauth.celery import app from allianceauth.services.hooks import NameFormatter from .manager import DiscordOAuthManager, DiscordApiBackoff from .models import DiscordUser @@ -57,7 +57,7 @@ class DiscordTasks: return True @staticmethod - @app.task(bind=True, name='discord.update_groups') + @shared_task(bind=True, name='discord.update_groups') def update_groups(task_self, pk): user = User.objects.get(pk=pk) logger.debug("Updating discord groups for user %s" % user) @@ -87,14 +87,14 @@ class DiscordTasks: logger.debug("User does not have a discord account, skipping") @staticmethod - @app.task(name='discord.update_all_groups') + @shared_task(name='discord.update_all_groups') def update_all_groups(): logger.debug("Updating ALL discord groups") for discord_user in DiscordUser.objects.exclude(uid__exact=''): DiscordTasks.update_groups.delay(discord_user.user.pk) @staticmethod - @app.task(bind=True, name='discord.update_nickname') + @shared_task(bind=True, name='discord.update_nickname') def update_nickname(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating discord nickname for user %s" % user) @@ -118,7 +118,7 @@ class DiscordTasks: logger.debug("User %s does not have a discord account" % user) @staticmethod - @app.task(name='discord.update_all_nicknames') + @shared_task(name='discord.update_all_nicknames') def update_all_nicknames(): logger.debug("Updating ALL discord nicknames") for discord_user in DiscordUser.objects.exclude(uid__exact=''): diff --git a/allianceauth/services/modules/discourse/tasks.py b/allianceauth/services/modules/discourse/tasks.py index 71bca870..43cd4dd6 100644 --- a/allianceauth/services/modules/discourse/tasks.py +++ b/allianceauth/services/modules/discourse/tasks.py @@ -2,8 +2,8 @@ import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist +from celery import shared_task -from allianceauth.celery import app from allianceauth.notifications import notify from allianceauth.services.hooks import NameFormatter from .manager import DiscourseManager @@ -40,7 +40,7 @@ class DiscourseTasks: return False @staticmethod - @app.task(bind=True, name='discourse.update_groups') + @shared_task(bind=True, name='discourse.update_groups') def update_groups(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating discourse groups for user %s" % user) @@ -52,7 +52,7 @@ class DiscourseTasks: logger.debug("Updated user %s discourse groups." % user) @staticmethod - @app.task(name='discourse.update_all_groups') + @shared_task(name='discourse.update_all_groups') def update_all_groups(): logger.debug("Updating ALL discourse groups") for discourse_user in DiscourseUser.objects.filter(enabled=True): diff --git a/allianceauth/services/modules/mumble/tasks.py b/allianceauth/services/modules/mumble/tasks.py index 3881651e..541a0990 100644 --- a/allianceauth/services/modules/mumble/tasks.py +++ b/allianceauth/services/modules/mumble/tasks.py @@ -2,8 +2,8 @@ import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist +from celery import shared_task -from allianceauth.celery import app from .models import MumbleUser logger = logging.getLogger(__name__) @@ -26,7 +26,7 @@ class MumbleTasks: MumbleUser.objects.all().delete() @staticmethod - @app.task(bind=True, name="mumble.update_groups") + @shared_task(bind=True, name="mumble.update_groups") def update_groups(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating mumble groups for user %s" % user) @@ -46,7 +46,7 @@ class MumbleTasks: return False @staticmethod - @app.task(name="mumble.update_all_groups") + @shared_task(name="mumble.update_all_groups") def update_all_groups(): logger.debug("Updating ALL mumble groups") for mumble_user in MumbleUser.objects.exclude(username__exact=''): diff --git a/allianceauth/services/modules/openfire/tasks.py b/allianceauth/services/modules/openfire/tasks.py index bec8371d..fef2d072 100644 --- a/allianceauth/services/modules/openfire/tasks.py +++ b/allianceauth/services/modules/openfire/tasks.py @@ -3,8 +3,8 @@ import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist from allianceauth.notifications import notify +from celery import shared_task -from allianceauth.celery import app from allianceauth.services.modules.openfire.manager import OpenfireManager from allianceauth.services.hooks import NameFormatter from .models import OpenfireUser @@ -40,7 +40,7 @@ class OpenfireTasks: OpenfireUser.objects.all().delete() @staticmethod - @app.task(bind=True, name="openfire.update_groups") + @shared_task(bind=True, name="openfire.update_groups") def update_groups(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating jabber groups for user %s" % user) @@ -61,7 +61,7 @@ class OpenfireTasks: logger.debug("User does not have an openfire account") @staticmethod - @app.task(name="openfire.update_all_groups") + @shared_task(name="openfire.update_all_groups") def update_all_groups(): logger.debug("Updating ALL jabber groups") for openfire_user in OpenfireUser.objects.exclude(username__exact=''): diff --git a/allianceauth/services/modules/phpbb3/tasks.py b/allianceauth/services/modules/phpbb3/tasks.py index 10cd1911..b6224ca8 100644 --- a/allianceauth/services/modules/phpbb3/tasks.py +++ b/allianceauth/services/modules/phpbb3/tasks.py @@ -2,8 +2,8 @@ import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist +from celery import shared_task -from allianceauth.celery import app from allianceauth.notifications import notify from allianceauth.services.hooks import NameFormatter from .manager import Phpbb3Manager @@ -35,7 +35,7 @@ class Phpbb3Tasks: return False @staticmethod - @app.task(bind=True, name="phpbb3.update_groups") + @shared_task(bind=True, name="phpbb3.update_groups") def update_groups(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating phpbb3 groups for user %s" % user) @@ -56,7 +56,7 @@ class Phpbb3Tasks: logger.debug("User does not have a Phpbb3 account") @staticmethod - @app.task(name="phpbb3.update_all_groups") + @shared_task(name="phpbb3.update_all_groups") def update_all_groups(): logger.debug("Updating ALL phpbb3 groups") for user in Phpbb3User.objects.exclude(username__exact=''): diff --git a/allianceauth/services/modules/seat/tasks.py b/allianceauth/services/modules/seat/tasks.py index c44306cf..e0fffa57 100644 --- a/allianceauth/services/modules/seat/tasks.py +++ b/allianceauth/services/modules/seat/tasks.py @@ -2,8 +2,8 @@ import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist +from celery import shared_task -from allianceauth.celery import app from allianceauth.notifications import notify from allianceauth.services.hooks import NameFormatter from .manager import SeatManager @@ -34,7 +34,7 @@ class SeatTasks: return False @staticmethod - @app.task(bind=True) + @shared_task(bind=True) def update_roles(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating SeAT roles for user %s" % user) @@ -56,7 +56,7 @@ class SeatTasks: logger.debug("User %s does not have a SeAT account") @staticmethod - @app.task + @shared_task def update_all_roles(): logger.debug("Updating ALL SeAT roles") for user in SeatUser.objects.all(): diff --git a/allianceauth/services/modules/smf/tasks.py b/allianceauth/services/modules/smf/tasks.py index f00e1a5d..e31ee944 100644 --- a/allianceauth/services/modules/smf/tasks.py +++ b/allianceauth/services/modules/smf/tasks.py @@ -2,8 +2,8 @@ import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist +from celery import shared_task -from allianceauth.celery import app from allianceauth.notifications import notify from allianceauth.services.hooks import NameFormatter from .manager import SmfManager @@ -39,7 +39,7 @@ class SmfTasks: SmfUser.objects.all().delete() @staticmethod - @app.task(bind=True, name="smf.update_groups") + @shared_task(bind=True, name="smf.update_groups") def update_groups(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating smf groups for user %s" % user) @@ -60,7 +60,7 @@ class SmfTasks: logger.debug("User does not have an smf account") @staticmethod - @app.task(name="smf.update_all_groups") + @shared_task(name="smf.update_all_groups") def update_all_groups(): logger.debug("Updating ALL smf groups") for user in SmfUser.objects.exclude(username__exact=''): diff --git a/allianceauth/services/modules/teamspeak3/tasks.py b/allianceauth/services/modules/teamspeak3/tasks.py index 1accc78a..b7e811b5 100644 --- a/allianceauth/services/modules/teamspeak3/tasks.py +++ b/allianceauth/services/modules/teamspeak3/tasks.py @@ -2,8 +2,8 @@ import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist +from celery import shared_task -from allianceauth.celery import app from allianceauth.notifications import notify from allianceauth.services.hooks import NameFormatter from .manager import Teamspeak3Manager @@ -37,7 +37,7 @@ class Teamspeak3Tasks: return False @staticmethod - @app.task() + @shared_task def run_ts3_group_update(): logger.debug("TS3 installed. Syncing local group objects.") with Teamspeak3Manager() as ts3man: @@ -56,7 +56,7 @@ class Teamspeak3Tasks: logger.info("Teamspeak3 disabled") @staticmethod - @app.task(bind=True, name="teamspeak3.update_groups") + @shared_task(bind=True, name="teamspeak3.update_groups") def update_groups(self, pk): user = User.objects.get(pk=pk) logger.debug("Updating user %s teamspeak3 groups" % user) @@ -81,7 +81,7 @@ class Teamspeak3Tasks: logger.debug("User does not have a teamspeak3 account") @staticmethod - @app.task(name="teamspeak3.update_all_groups") + @shared_task(name="teamspeak3.update_all_groups") def update_all_groups(): logger.debug("Updating ALL teamspeak3 groups") for user in Teamspeak3User.objects.exclude(uid__exact=''): diff --git a/allianceauth/services/tasks.py b/allianceauth/services/tasks.py index 19e4bc55..ec567235 100644 --- a/allianceauth/services/tasks.py +++ b/allianceauth/services/tasks.py @@ -1,8 +1,8 @@ import logging import redis +from celery import shared_task -from allianceauth.celery import app from .hooks import ServicesHook REDIS_CLIENT = redis.Redis() @@ -37,7 +37,7 @@ def only_one(function=None, key="", timeout=None): return _dec(function) if function is not None else _dec -@app.task(bind=True) +@shared_task(bind=True) def validate_services(self, user): logger.debug('Ensuring user {} has permissions for active services'.format(user)) # Iterate through services hooks and have them check the validity of the user diff --git a/tests/__init__.py b/tests/__init__.py index e69de29b..cd042640 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +from .celery import app as celery_app + +__all__ = ['celery_app'] diff --git a/allianceauth/celery.py b/tests/celery.py similarity index 73% rename from allianceauth/celery.py rename to tests/celery.py index 63c652e0..6492058a 100644 --- a/allianceauth/celery.py +++ b/tests/celery.py @@ -1,12 +1,12 @@ -import os +import os, sys from celery import Celery # set the default Django settings module for the 'celery' program. -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'allianceauth.project_template.project_name.settings.base') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') from django.conf import settings # noqa -app = Celery('alliance_auth') +app = Celery('devauth') # Using a string here means the worker don't have to serialize # the configuration object to child processes.