diff --git a/pikatasks/django_compat.py b/pikatasks/django_compat.py index 46319dfb10840ac2e0f05198a931328c7849c25c..c5dcdcedc01c085e1909d3d8a570ada4ff16d6dd 100644 --- a/pikatasks/django_compat.py +++ b/pikatasks/django_compat.py @@ -1,4 +1,6 @@ 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 + diff --git a/pikatasks/worker.py b/pikatasks/worker.py index 88a778d5c7c7887b4f534c4c9a79856e18e93f04..651f2be0e33fda989b8c7321a6db65a8e49f94ed 100644 --- a/pikatasks/worker.py +++ b/pikatasks/worker.py @@ -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: