Tom Willis avatar Tom Willis committed 1991d29

initial commit

Comments (0)

Files changed (8)

+syntax:glob
+
+.coverage
+*.pyc
+*.*~
+*.*.orig
+*.egg-info
+doc/build
+register.ini
+build
+dist
+_*.ini

celerypaste/__init__.py

+#from celery.loaders.base import BaseLoader
+from celery.loaders.default import Loader
+from paste.script.command import Command, BadCommand
+from paste.deploy import appconfig
+import os
+import imp
+
+KEY_ENV_LOADER = "CELERY_LOADER"
+
+
+class PasteLoader(Loader):
+
+    def __init__(self, config):
+        self._config = config
+
+    def read_configuration(self):
+        config = {}
+        config.update(self._config)
+        #this trips up django, and paste puts this in by default
+        if "tasks" in config:
+            init_tasks = config.pop("tasks")
+        else:
+            init_tasks = None
+
+        if "__file__" in config:
+            config.pop("__file__")
+        self.django_settings = self.setup_django_env(config)
+        self.init_tasks(init_tasks)
+
+        return self.django_settings
+
+    def init_tasks(self, init_tasks):
+        """
+        import this module, seems to be the thing to do
+        """
+        print init_tasks
+        self.tasks_module = imp.load_source(\
+            os.path.basename(init_tasks)[:-3], init_tasks)
+
+
+class PasteLoaderFactory(object):
+
+    def __init__(self, **config):
+        self._config = config
+
+    def __call__(self):
+        return PasteLoader(self._config)
+
+
+def set_loader(config):
+    """
+    order of operations is important
+    """
+    os.environ[KEY_ENV_LOADER] = __name__
+    os.environ["DJANGO_SETTINGS_MODULE"] = __name__ #why?
+    _config={}
+
+    from celery.loaders.default import DEFAULT_SETTINGS
+    _config.update(DEFAULT_SETTINGS)
+    _config.update(config)
+
+    global Loader
+    Loader = PasteLoaderFactory(**_config) #config from ini file
+
+
+class BaseCommand(Command):
+    """"""
+
+    def __init__(self, command_name):
+        Command.__init__(self, command_name)
+        self.min_args = 1
+        self.max_args = 1
+        self.summary =self. __doc__.splitlines()[0]
+        self.usage = '\n' +self. __doc__
+        self.parser = Command.standard_parser()
+        self.parser.add_option("--section",
+                          action="store",
+                          type="string",
+                          dest="section",
+                          help="section in config file to pickup settings")
+
+    def init_loader(self):
+        paste_config = {}
+        paste_config.update(self.get_config(self.parse_section()))
+        set_loader(paste_config)
+
+    def parse_section(self):
+        return self.options.section
+
+    def get_config(self, name):
+        if len(self.args)==1:
+            config_file = self.args[0]
+            if not os.path.isfile(config_file):
+                raise BadCommand(
+"""
+%s
+Error: CONFIG_FILE not found at %s%s.
+Please specify a CONFIG_FILE"""%
+                    (self.parser.get_usage(),
+                    os.path.sep,
+                    config_file))
+            else:
+                config = appconfig("config:%s" % config_file,
+                                   name = name,
+                                   relative_to = os.getcwd())
+                return config
+        else:
+            raise BadCommand(self.parser.get_usage())
+
+
+class CelerySyncDBCmd(BaseCommand):
+    """
+    run with settings from paste config file
+
+    Example::
+       $ paster celerysyncdb production.ini --section <section_name>
+    """
+
+    def command(self):
+        import sys
+        self.init_loader()
+        from celery.loaders import current_loader
+        loader = current_loader()
+        conf = loader.read_configuration()
+        from django.core.management import call_command, setup_environ
+        sys.stderr.write("Creating database tables...\n")
+        print conf
+        setup_environ(conf)
+        call_command("syncdb")
+
+
+class CeleryDCmd(BaseCommand):
+    """
+    run with settings from paste config file
+
+    Example::
+       $ paster celeryd production.ini --section <section_name>
+    """
+
+    def _get_celeryd_opts(self, config):
+        """
+        any option in the config prefixed with 'celeryd.' will be
+        treated like command line arguments to celeryd
+        """
+        celeryd_keys = []
+        result = {}
+        for k in config.keys():
+            if k.lower().startswith("celeryd."):
+                celeryd_keys.append(k)
+
+        for k in celeryd_keys:
+            result[k.replace("celeryd.", "")] = config.pop(k)
+
+        return result
+
+    def command(self):
+        self.init_loader()
+        options = {}
+        options.update(
+            self._get_celeryd_opts(
+                self.get_config(self.parse_section())))
+        #any celeryd. options should be in options at this point
+        #be careful about when you import this, once you do, the
+        #settings are initialized
+        from celery.bin.celeryd import run_worker
+        run_worker(**options)
+
+
+class CeleryApp(object):
+    """
+    this does nothing except allow for a config section to be defined
+    for on it's own
+    """
+"""
+my tasks module
+"""
+from celery.task import Task
+from celery.registry import tasks
+
+
+class AddTask(Task):
+
+    def run(self, x, y):
+        return x+y
+
+tasks.register(AddTask)
+[egg_info]
+tag_build = dev
+tag_svn_revision = true
+from setuptools import setup, find_packages
+import sys, os
+
+version = '0.0'
+requires = ["PasteDeploy", "celery"]
+setup(name='celery-paste',
+      version=version,
+      description="paste things to drive celeryd",
+      long_description="""\
+""",
+      classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
+      keywords='',
+      author='Tom Willis',
+      author_email='tom.willis@gmail.com',
+      url='http://sadphaeton.com',
+      license='',
+      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+      include_package_data=True,
+      zip_safe=False,
+      install_requires=requires,
+      entry_points="""
+      # -*- Entry points: -*-
+      [paste.app_factory]
+      app=celerypaste:CeleryApp
+
+      [paste.global_paster_command]
+      celeryd=celerypaste:CeleryDCmd
+      celerysyncdb=celerypaste:CelerySyncDBCmd
+      """,
+      )

Binary file added.

+[app:celery]
+use=egg:celery-paste#app
+BROKER_HOST= localhost
+BROKER_PORT= 5672
+BROKER_VHOST= celeryTst
+BROKER_USER= celery
+BROKER_PASSWORD= celery
+CELERY_BACKEND= database
+DATABASE_ENGINE= sqlite3
+DATABASE_NAME= %(here)s/test.db
+tasks=%(here)s/mytasks.py

tests/test_run.py

+from celerypaste import set_loader
+from threading import Thread
+import sys
+
+CELERY_CONFIG ={"BROKER_HOST": "localhost",
+                "BROKER_PORT": 5672,
+                "BROKER_VHOST": "celeryTst",
+                "BROKER_USER": "celery",
+                "BROKER_PASSWORD": "celery",
+                "CELERY_BACKEND": "database",
+                "DATABASE_ENGINE": "sqlite3",
+                "DATABASE_NAME": "test.db",
+                }
+
+
+set_loader(CELERY_CONFIG)
+
+from celery.registry import tasks
+from celery.task import Task
+
+
+class AddTask(Task):
+
+    def run(self, x, y):
+        return x+y
+
+tasks.register(AddTask)
+
+
+class CeleryWorker(Thread):
+
+    def run(self):
+        from celery.bin.celeryd import run_worker
+        run_worker(**vars())
+
+
+def setup_db():
+    from celery.loaders import current_loader
+    loader = current_loader()
+    conf = loader.read_configuration()
+    from django.core.management import call_command, setup_environ
+    sys.stderr.write("Creating database tables...\n")
+    setup_environ(conf)
+    call_command("syncdb")
+
+
+def test_run():
+    setup_db()
+    w = CeleryWorker()
+    w.start()
+    t = AddTask()
+    result = t.apply_async(args=[10, 10], countdown=3)
+    assert result.get()==20, "didn't get expected result"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.