Skip to content
Snippets Groups Projects
Commit 92258d23 authored by Art's avatar Art :lizard:
Browse files

Add autodiscover_tasks to avoid kilometers of manual imports

parent 8c736918
No related branches found
No related tags found
No related merge requests found
import logging
import importlib
from . import utils
try:
from django import db as django_db
......@@ -61,3 +63,26 @@ def check_fix_db_connection():
pass
def autodiscover_tasks(apps=None, modules=("tasks",)):
"""
Imports modules with tasks from django apps.
This function utilizes the fact that each task registers itself in utils.all_tasks
:param apps: tuple of app names, leave None for everything in INSTALLED_APPS
:param modules: tuple of module names, if apps have their tasks in places other than "tasks.py"
:return: utils.all_tasks
"""
assert DJANGO
if apps is None:
apps = django_conf.settings.INSTALLED_APPS
for app_name in apps:
for module_name in modules:
full_module_name = "{0}.{1}".format(app_name, module_name)
try:
importlib.import_module(full_module_name)
# just importing the module is perfectly enough, each task will register itself on import
logger.info("Autodiscover: imported \"{0}\"".format(full_module_name))
except ImportError:
logger.debug("Autodiscover: module \"{0}\" does not exist".format(full_module_name))
return utils.all_tasks
......@@ -93,7 +93,7 @@ def start(tasks=utils.all_tasks, number_of_processes=None):
channel.queue_declare(queue=queue_name, passive=True)
exists = True
except AMQPChannelError as e:
logger.warning("Failed to {queue_name}. {e.__class__.__name__}: {e}".format(**locals()))
logger.warning("Cannot access queue \"{queue_name}\". {e.__class__.__name__}: {e}".format(**locals()))
exists = False
finally:
if conn and conn.is_open:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment