From 92258d233717879fab799b1c9a89ea5c794a01ce Mon Sep 17 00:00:00 2001
From: Art Lukyanchyk <artiom.lukyanchyk@hs-hannover.de>
Date: Fri, 11 May 2018 18:35:55 +0200
Subject: [PATCH] Add autodiscover_tasks to avoid kilometers of manual imports

---
 pikatasks/django_compat.py | 25 +++++++++++++++++++++++++
 pikatasks/worker.py        |  2 +-
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/pikatasks/django_compat.py b/pikatasks/django_compat.py
index 46319df..c5dcdce 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 88a778d..651f2be 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:
-- 
GitLab