Browse Source

Can now configure the apps main module name, used to autogenerate the module name if '__main__'

Ask Solem 14 năm trước cách đây
mục cha
commit
dbe832f744

+ 4 - 0
celery/app/__init__.py

@@ -74,6 +74,10 @@ class App(base.BaseApp):
         kwargs["app"] = self
         return TaskSet(*args, **kwargs)
 
+    def worker_main(self, argv=None):
+        from celery.bin.celeryd import WorkerCommand
+        return WorkerCommand(app=self).execute_from_commandline(argv)
+
     def task(self, *args, **options):
         """Decorator to create a task class out of any callable.
 

+ 3 - 1
celery/app/base.py

@@ -79,7 +79,9 @@ class BaseApp(object):
     IS_OSX = SYSTEM == "Darwin"
     IS_WINDOWS = SYSTEM == "Windows"
 
-    def __init__(self, loader=None, backend=None, set_as_current=True):
+    def __init__(self, main=None, loader=None, backend=None,
+            set_as_current=True):
+        self.main = main
         self.loader_cls = loader or "app"
         self.backend_cls = backend
         self._amqp = None

+ 1 - 1
celery/app/defaults.py

@@ -77,7 +77,7 @@ NAMESPACES = {
         "IMPORTS": Option((), type="tuple"),
         "IGNORE_RESULT": Option(False, type="bool"),
         "MAX_CACHED_RESULTS": Option(5000, type="int"),
-        "RESULT_BACKEND": Option("database"),
+        "RESULT_BACKEND": Option("amqp"),
         "RESULT_DBURI": Option(),
         "RESULT_ENGINE_OPTIONS": Option(None, type="dict"),
         "RESULT_EXCHANGE": Option("celeryresults"),

+ 1 - 1
celery/backends/amqp.py

@@ -49,7 +49,7 @@ class AMQPBackend(BaseDictBackend):
     _connection = None
 
     def __init__(self, connection=None, exchange=None, exchange_type=None,
-            persistent=None, serializer=None, auto_delete=False,
+            persistent=None, serializer=None, auto_delete=True,
             expires=None, **kwargs):
         super(AMQPBackend, self).__init__(**kwargs)
         conf = self.app.conf

+ 5 - 2
celery/bin/base.py

@@ -30,6 +30,7 @@ class Command(object):
     Parser = OptionParser
 
     def __init__(self, app=None, get_app=None):
+        self.app = app
         self.get_app = get_app or self._get_default_app
 
     def usage(self):
@@ -75,8 +76,10 @@ class Command(object):
         config_module = preload_options.pop("config_module", None)
         if config_module:
             os.environ["CELERY_CONFIG_MODULE"] = config_module
-        self.app = (app and self.get_cls_by_name(app) or
-                            self.get_app(loader=loader))
+        if app:
+            self.app = self.get_cls_by_name(app)
+        elif not self.app:
+            self.app = self.get_app(loader=loader)
         if self.enable_config_from_cmdline:
             argv = self.process_cmdline_config(argv)
         return argv

+ 6 - 3
celery/task/base.py

@@ -60,8 +60,7 @@ class TaskType(type):
 
         # Automatically generate missing name.
         if not attrs.get("name"):
-            task_module = sys.modules[task_module]
-            task_name = ".".join([task_module.__name__, name])
+            task_name = ".".join([sys.modules[task_module].__name__, name])
             attrs["name"] = task_name
 
         # Because of the way import happens (recursively)
@@ -72,8 +71,12 @@ class TaskType(type):
         task_name = attrs["name"]
         if task_name not in tasks:
             task_cls = super_new(cls, name, bases, attrs)
+            if task_module == "__main__" and task_cls.app.main:
+                task_name = task_cls.name = ".".join([task_cls.app.main,
+                                                      name])
             tasks.register(task_cls)
-        return tasks[task_name].__class__
+        task = tasks[task_name].__class__
+        return task
 
 
 class BaseTask(object):

+ 24 - 0
examples/app/myapp.py

@@ -0,0 +1,24 @@
+"""myapp.py
+
+Usage:
+
+   (window1)$ python myapp.py -l info
+
+   (window2)$ python
+   >>> from myapp import add
+   >>> add.delay(16, 16).get()
+   32
+
+"""
+from celery import Celery
+
+
+celery = Celery("myapp")
+celery.conf.update(BROKER_HOST="localhost")
+
+@celery.task()
+def add(x, y):
+    return x + y
+
+if __name__ == "__main__":
+    celery.worker_main()