示例#1
0
文件: task.py 项目: public/celery
    def __new__(cls, name, bases, attrs):
        new = super(TaskType, cls).__new__
        task_module = attrs.get('__module__') or '__main__'

        # - Abstract class: abstract attribute should not be inherited.
        if attrs.pop('abstract', None) or not attrs.get('autoregister', True):
            return new(cls, name, bases, attrs)

        # The 'app' attribute is now a property, with the real app located
        # in the '_app' attribute.  Previously this was a regular attribute,
        # so we should support classes defining it.
        _app1, _app2 = attrs.pop('_app', None), attrs.pop('app', None)
        app = attrs['_app'] = _app1 or _app2 or current_app

        # - Automatically generate missing/empty name.
        task_name = attrs.get('name')
        if not task_name:
            attrs['name'] = task_name = gen_task_name(app, name, task_module)

        # - Create and register class.
        # Because of the way import happens (recursively)
        # we may or may not be the first time the task tries to register
        # with the framework.  There should only be one class for each task
        # name, so we always return the registered version.
        tasks = app._tasks
        if task_name not in tasks:
            tasks.register(new(cls, name, bases, attrs))
        instance = tasks[task_name]
        instance.bind(app)
        return instance.__class__
示例#2
0
    def __new__(cls, name, bases, attrs):
        new = super(TaskType, cls).__new__
        task_module = attrs.get('__module__') or '__main__'

        # - Abstract class: abstract attribute should not be inherited.
        abstract = attrs.pop('abstract', None)
        if abstract or not attrs.get('autoregister', True):
            return new(cls, name, bases, attrs)

        # The 'app' attribute is now a property, with the real app located
        # in the '_app' attribute.  Previously this was a regular attribute,
        # so we should support classes defining it.
        app = attrs.pop('_app', None) or attrs.pop('app', None)

        # Attempt to inherit app from one the bases
        if not isinstance(app, Proxy) and app is None:
            for base in bases:
                if getattr(base, '_app', None):
                    app = base._app
                    break
            else:
                app = current_app._get_current_object()
        attrs['_app'] = app

        # - Automatically generate missing/empty name.
        task_name = attrs.get('name')
        if not task_name:
            attrs['name'] = task_name = gen_task_name(app, name, task_module)

        if not attrs.get('_decorated'):
            # non decorated tasks must also be shared in case
            # an app is created multiple times due to modules
            # imported under multiple names.
            # Hairy stuff,  here to be compatible with 2.x.
            # People should not use non-abstract task classes anymore,
            # use the task decorator.
            from celery._state import connect_on_app_finalize
            unique_name = '.'.join([task_module, name])
            if unique_name not in cls._creation_count:
                # the creation count is used as a safety
                # so that the same task is not added recursively
                # to the set of constructors.
                cls._creation_count[unique_name] = 1
                connect_on_app_finalize(_CompatShared(
                    unique_name,
                    lambda app: TaskType.__new__(cls, name, bases,
                                                 dict(attrs, _app=app)),
                ))

        # - Create and register class.
        # Because of the way import happens (recursively)
        # we may or may not be the first time the task tries to register
        # with the framework.  There should only be one class for each task
        # name, so we always return the registered version.
        tasks = app._tasks
        if task_name not in tasks:
            tasks.register(new(cls, name, bases, attrs))
        instance = tasks[task_name]
        instance.bind(app)
        return instance.__class__
示例#3
0
 def _decorator(task_fn):
     kwargs.setdefault("retries", 3)
     kwargs["bind"] = True
     kwargs["name"] = gen_task_name(app, task_fn.__name__, task_fn.__module__)
     @app.task(*args, **kwargs)
     def _task(self, audio_id, *args, **kwargs):
         try:
             audio = Audio.objects.get(pk=audio_id)
         except Audio.DoesNotExist:
             logger.warn("Can't find Audio object to transcode, maybe it's been deleted?")
             return 
         return task_fn(self, audio, *args, **kwargs)
     return _task
示例#4
0
 def _decorator(task_fn):
     kwargs.setdefault("retries", 3)
     kwargs["bind"] = True
     kwargs["name"] = gen_task_name(app, task_fn.__name__, task_fn.__module__)
     @app.task(*args, **kwargs)
     def _task(self, snippet_id, *args, **kwargs):
         try:
             snippet = models.Snippet.objects.get(pk=snippet_id)
         except models.Snippet.DoesNotExist:
             logger.warn("Can't find Snippet object to transcode, maybe it's been deleted?")
             return 
         if snippet.state != "processing":
             logger.warn("Snippet is now longer in processing state (state=%s), ignoring" % (snippet.state,))
             return 
         return task_fn(self, snippet, *args, **kwargs)
     return _task
示例#5
0
文件: base.py 项目: Artificioo/celery
    def _task_from_fun(self, fun, name=None, base=None, bind=False, **options):
        if not self.finalized and not self.autofinalize:
            raise RuntimeError('Contract breach: app not finalized')
        name = name or gen_task_name(self, fun.__name__, fun.__module__)
        base = base or self.Task

        if name not in self._tasks:
            task = type(fun.__name__, (base, ), dict({
                'app': self,
                'name': name,
                'run': fun if bind else staticmethod(fun),
                '_decorated': True,
                '__doc__': fun.__doc__,
                '__module__': fun.__module__,
                '__wrapped__': fun}, **options))()
            self._tasks[task.name] = task
            task.bind(self)  # connects task to this app
        else:
            task = self._tasks[name]
        return task
示例#6
0
文件: base.py 项目: 277800076/celery
 def gen_task_name(self, name, module):
     return gen_task_name(self, name, module)
示例#7
0
 def task_by_cons():
     app = current_app()
     return app.tasks[
         name or gen_task_name(app, fun.__name__, fun.__module__)
     ]
示例#8
0
 def test_no_module(self):
     app = Mock()
     app.name == '__main__'
     self.assertTrue(gen_task_name(app, 'foo', 'axsadaewe'))
示例#9
0
文件: task.py 项目: wengole/celery
    def __new__(cls, name, bases, attrs):
        new = super(TaskType, cls).__new__
        task_module = attrs.get('__module__') or '__main__'

        # - Abstract class: abstract attribute should not be inherited.
        abstract = attrs.pop('abstract', None)
        if abstract or not attrs.get('autoregister', True):
            return new(cls, name, bases, attrs)

        # The 'app' attribute is now a property, with the real app located
        # in the '_app' attribute.  Previously this was a regular attribute,
        # so we should support classes defining it.
        app = attrs.pop('_app', None) or attrs.pop('app', None)

        # Attempt to inherit app from one the bases
        if not isinstance(app, Proxy) and app is None:
            for base in bases:
                if getattr(base, '_app', None):
                    app = base._app
                    break
            else:
                app = current_app._get_current_object()
        attrs['_app'] = app

        # - Automatically generate missing/empty name.
        task_name = attrs.get('name')
        if not task_name:
            attrs['name'] = task_name = gen_task_name(app, name, task_module)

        if not attrs.get('_decorated'):
            # non decorated tasks must also be shared in case
            # an app is created multiple times due to modules
            # imported under multiple names.
            # Hairy stuff,  here to be compatible with 2.x.
            # People should not use non-abstract task classes anymore,
            # use the task decorator.
            from celery._state import connect_on_app_finalize
            unique_name = '.'.join([task_module, name])
            if unique_name not in cls._creation_count:
                # the creation count is used as a safety
                # so that the same task is not added recursively
                # to the set of constructors.
                cls._creation_count[unique_name] = 1
                connect_on_app_finalize(
                    _CompatShared(
                        unique_name,
                        lambda app: TaskType.__new__(cls, name, bases,
                                                     dict(attrs, _app=app)),
                    ))

        # - Create and register class.
        # Because of the way import happens (recursively)
        # we may or may not be the first time the task tries to register
        # with the framework.  There should only be one class for each task
        # name, so we always return the registered version.
        tasks = app._tasks
        if task_name not in tasks:
            tasks.register(new(cls, name, bases, attrs))
        instance = tasks[task_name]
        instance.bind(app)
        return instance.__class__
示例#10
0
文件: base.py 项目: gjames2467/celery
 def gen_task_name(self, name, module):
     return gen_task_name(self, name, module)
示例#11
0
 def task_by_cons():
     app = current_app()
     return app.tasks[
         name or gen_task_name(app, fun.__name__, fun.__module__)]
示例#12
0
 def test_no_module(self):
     app = Mock()
     app.name == '__main__'
     self.assertTrue(gen_task_name(app, 'foo', 'axsadaewe'))