Browse Source

[5.0] Always require --app argument (cc #2447)

Ask Solem 8 years ago
parent
commit
12b6800078
4 changed files with 40 additions and 19 deletions
  1. 26 11
      celery/bin/base.py
  2. 4 2
      celery/bin/celery.py
  3. 2 2
      celery/tests/bin/test_base.py
  4. 8 4
      celery/tests/bin/test_celery.py

+ 26 - 11
celery/bin/base.py

@@ -137,7 +137,10 @@ class Command:
 
     # Some programs (multi) does not want to load the app specified
     # (Issue #1008).
-    respects_app_option = True
+    requires_app = True
+
+    # Some programs (multi) does not want to set up fixups etc.
+    fake_app = False
 
     #: List of options to parse before parsing other options.
     preload_options = (
@@ -176,7 +179,7 @@ class Command:
                  stdout=None, stderr=None, quiet=False, on_error=None,
                  on_usage_error=None):
         self.app = app
-        self.get_app = get_app or self._get_default_app
+        self.get_app = get_app
         self.stdout = stdout or sys.stdout
         self.stderr = stderr or sys.stderr
         self._colored = None
@@ -408,15 +411,8 @@ class Command:
         config = preload_options.get('config')
         if config:
             os.environ['CELERY_CONFIG_MODULE'] = config
-        if self.respects_app_option:
-            if app:
-                self.app = self.find_app(app)
-            elif self.app is None:
-                self.app = self.get_app(loader=loader)
-            if self.enable_config_from_cmdline:
-                argv = self.process_cmdline_config(argv)
-        else:
-            self.app = Celery(fixups=[])
+
+        self.initialize_app(app, loader)
 
         user_preload = tuple(self.app.user_options['preload'] or ())
         if user_preload:
@@ -428,6 +424,25 @@ class Command:
             )
         return argv
 
+    def initialize_app(self, app=None, loader=None):
+        if self.requires_app:
+            if app:
+                self.app = self.find_app(app)
+            elif self.app is None:
+                self.app = self.get_default_app(app, loader)
+            if self.enable_config_from_cmdline:
+                argv = self.process_cmdline_config(argv)
+        else:
+            if self.fake_app:
+                self.app = Celery(fixups=[])
+            else:
+                self.app = self.get_default_app(app, loader)
+
+    def get_default_app(self, app=None, loader=None):
+        if self.get_app is not None:
+            return self.get_app(loader=loader)
+        raise ImproperlyConfigured('Missing required --app|-A option')
+
     def find_app(self, app):
         from celery.app.utils import find_app
         return find_app(app, symbol_by_name=self.symbol_by_name)

+ 4 - 2
celery/bin/celery.py

@@ -333,7 +333,8 @@ def main(argv=None):
 
 class multi(Command):
     """Start multiple worker instances."""
-    respects_app_option = False
+    fake_app = True
+    requires_app = False
 
     def get_options(self):
         pass
@@ -1154,7 +1155,8 @@ class CeleryCommand(Command):
     def execute_from_commandline(self, argv=None):
         argv = sys.argv if argv is None else argv
         if 'multi' in argv[1:3]:  # Issue 1008
-            self.respects_app_option = False
+            self.requires_app = False
+            self.fake_app = True
         try:
             sys.exit(determine_exit_status(
                 super().execute_from_commandline(argv)))

+ 2 - 2
celery/tests/bin/test_base.py

@@ -211,9 +211,9 @@ class test_Command(AppCase):
             if prev is not None:
                 os.environ['CELERY_LOADER'] = prev
 
-    def test_setup_app_no_respect(self):
+    def test_setup_app__no_requires_app(self):
         cmd = MockCommand(app=self.app)
-        cmd.respects_app_option = False
+        cmd.requires_app = False
         with patch('celery.bin.base.Celery') as cp:
             cmd.setup_app_from_commandline(['--app=x.y:z'])
             cp.assert_called()

+ 8 - 4
celery/tests/bin/test_celery.py

@@ -288,14 +288,18 @@ class test_CeleryCommand(AppCase):
         with self.assertRaises(SystemExit):
             x.execute_from_commandline()
 
-        x.respects_app_option = True
+        x.requires_app = True
+        x.fake_app = False
         with self.assertRaises(SystemExit):
             x.execute_from_commandline(['celery', 'multi'])
-        self.assertFalse(x.respects_app_option)
-        x.respects_app_option = True
+        self.assertFalse(x.requires_app)
+        self.assertTrue(x.fake_app)
+        x.requires_app = True
+        x.fake_app = False
         with self.assertRaises(SystemExit):
             x.execute_from_commandline(['manage.py', 'celery', 'multi'])
-        self.assertFalse(x.respects_app_option)
+        self.assertFalse(x.requires_app)
+        self.assertTrue(x.fake_app)
 
     def test_with_pool_option(self):
         x = CeleryCommand(app=self.app)