Beispiel #1
0
def task(*args, **kwargs):
    """Deprecated decorator, please use :func:`celery.task`."""
    return current_app.task(
        *args, **dict({
            'accept_magic_kwargs': False,
            'base': Task
        }, **kwargs))
Beispiel #2
0
def task(*args, **kwargs):
    """Decorator to create a task class out of any callable.

    **Examples**

    .. code-block:: python

        @task()
        def refresh_feed(url):
            return Feed.objects.get(url=url).refresh()

    With setting extra options and using retry.

    .. code-block:: python

        @task(max_retries=10)
        def refresh_feed(url):
            try:
                return Feed.objects.get(url=url).refresh()
            except socket.error as exc:
                refresh_feed.retry(exc=exc)

    Calling the resulting task:

            >>> refresh_feed('http://example.com/rss') # Regular
            <Feed: http://example.com/rss>
            >>> refresh_feed.delay('http://example.com/rss') # Async
            <AsyncResult: 8998d0f4-da0b-4669-ba03-d5ab5ac6ad5d>
    """
    return current_app.task(*args, **dict({'accept_magic_kwargs': False,
                                           'base': Task}, **kwargs))
Beispiel #3
0
def shTask(*args, **kwargs):
    """Task decorator."""
    return current_app.task(
        *args, **dict({
            'accept_magic_kwargs': False,
            'base': ShTask
        }, **kwargs))
Beispiel #4
0
    def _register_pipeline(self, pipeline_name, pipeline):
        """
        Register a pipeline with the pipeline registry.

        Durring the process of registration, all ordered members
        (hasattr(x, "order") == True) will be converted to method
        tasks.
        """

        pipeline_tasks = []

        for name, method in pipeline.__dict__.iteritems():
            is_callback = hasattr(
                method, "task_type") and method.task_type == "callback"
            if hasattr(method, "order") or is_callback:
                task = current_app.task(method,
                                        filter=task_method,
                                        default_retry_delay=1,
                                        name="%s.%s" % (pipeline.name, name))
                setattr(pipeline, name, task)

                if is_callback:
                    pipeline.callback = task
                else:
                    pipeline_tasks.append((method.order, name))

        sorted_pipeline_tasks = [
            p[1] for p in sorted(pipeline_tasks, key=lambda p: p[0])
        ]

        pipeline.identifier = pipeline_name
        pipeline.tasks = sorted_pipeline_tasks
        self._pipeline_registry[pipeline_name] = pipeline
        return pipeline
Beispiel #5
0
    def _register_pipeline(self, pipeline_name, pipeline):
        """
        Register a pipeline with the pipeline registry.

        Durring the process of registration, all ordered members
        (hasattr(x, "order") == True) will be converted to method
        tasks.
        """

        pipeline_tasks = []

        for name, method in pipeline.__dict__.iteritems():
            is_callback = hasattr(method, "task_type") and method.task_type == "callback"
            if hasattr(method, "order") or is_callback:
                task = current_app.task(
                    method, filter=task_method, default_retry_delay=1, name="%s.%s" % (pipeline.name, name))
                setattr(pipeline, name, task)

                if is_callback:
                    pipeline.callback = task
                else:
                    pipeline_tasks.append((method.order, name))

        sorted_pipeline_tasks = [p[1]
                                 for p in sorted(pipeline_tasks, key=lambda p: p[0])]

        pipeline.identifier = pipeline_name
        pipeline.tasks = sorted_pipeline_tasks
        self._pipeline_registry[pipeline_name] = pipeline
        return pipeline
Beispiel #6
0
def task(*args, **kwargs):
    """Decorator to create a task class out of any callable.

    **Examples**

    .. code-block:: python

        @task()
        def refresh_feed(url):
            return Feed.objects.get(url=url).refresh()

    With setting extra options and using retry.

    .. code-block:: python

        @task(max_retries=10)
        def refresh_feed(url):
            try:
                return Feed.objects.get(url=url).refresh()
            except socket.error, exc:
                refresh_feed.retry(exc=exc)

    Calling the resulting task:

            >>> refresh_feed("http://example.com/rss") # Regular
            <Feed: http://example.com/rss>
            >>> refresh_feed.delay("http://example.com/rss") # Async
            <AsyncResult: 8998d0f4-da0b-4669-ba03-d5ab5ac6ad5d>
    """
    return current_app.task(
        *args, **dict({
            "accept_magic_kwargs": False,
            "base": Task
        }, **kwargs))
Beispiel #7
0
def shTask(*args, **kwargs):
    """Task decorator."""
    return current_app.task(
        *args,
        **dict(
            {'accept_magic_kwargs': False, 'base': ShTask},
            **kwargs
        )
    )
Beispiel #8
0
def create_replacement_task(original, name_postfix, sigs):
    new_name = original.name + name_postfix
    bound = inspect.ismethod(original.undecorated)
    func = original.run if not bound else original.run.__func__
    options = {
        key: getattr(original, key)
        for key in [
            "acks_late", "default_retry_delay", "expires", "ignore_result",
            "max_retries", "reject_on_worker_lost", "resultrepr_maxsize",
            "soft_time_limit", "store_errors_even_if_ignored",
            "soft_time_limit", "time_limit", "track_started", "trail",
            "typing", "returns", "flame", "pending_child_strategy"
        ] if key in dir(original)
    }

    from celery import current_app
    new_task = current_app.task(name=new_name,
                                bind=bound,
                                base=inspect.getmro(original.__class__)[1],
                                check_name_for_override_posfix=False,
                                **options)(fun=func)
    if hasattr(original, "orig"):
        new_task.orig = original.orig
    if hasattr(original, "report_meta"):
        new_task.report_meta = original.report_meta

    try:
        # there is no way of copying the signals without coupling with the internals of celery signals
        # noinspection PyProtectedMember
        from celery.utils.dispatch.signal import _make_id
        orig_task_id = _make_id(original)
        for s, receivers in sigs.items():
            for r in receivers:
                # format is ((id(receiver), id(sender)), ref(receiver))
                # locate any registered signal against the original microservice
                if r[0][1] == orig_task_id:
                    # new entry only replaces the
                    entry = ((r[0][0], _make_id(new_task)), r[1])
                    s.receivers.append(entry)
    except Exception as e:
        logger.error("Unable to copy signals while overriding %s:\n%s" %
                     (original.name, str(e)))
    return new_task
Beispiel #9
0
def _compat_task_decorator(*args, **kwargs):
    from celery import current_app
    kwargs.setdefault('accept_magic_kwargs', True)
    return current_app.task(*args, **kwargs)
Beispiel #10
0
def task(*args, **kwargs):
    """Deprecated decorator, please use :func:`celery.task`."""
    return current_app.task(*args, **dict({'accept_magic_kwargs': False,
                                           'base': Task}, **kwargs))
Beispiel #11
0
def task(*args, **kwargs):
    """Deprecated decorator, please use :func:`celery.task`."""
    return current_app.task(*args, **dict({"base": Task}, **kwargs))
Beispiel #12
0
def _compat_task_decorator(*args, **kwargs):
    from celery import current_app
    kwargs.setdefault('accept_magic_kwargs', True)
    return current_app.task(*args, **kwargs)
Beispiel #13
0
def task(*args, **kwargs):
    return current_app.task(*args, **dict(kwargs, filter=task_method))
def task(*args, **kwargs):
    """Deprecated decorator, please use :func:`celery.task`."""
    return current_app.task(*args, **dict({"base": Task}, **kwargs))
Beispiel #15
0
def task(*args, **kwargs):
    return current_app.task(*args, **dict(kwargs, filter=task_method))
Beispiel #16
0
def shPeriodicTask(*args, **options):
    """Periodic task decorator."""
    return current_app.task(**dict({'base': ShPeriodicTask}, **options))
Beispiel #17
0
def shPeriodicTask(*args, **options):
    """Periodic task decorator."""
    return current_app.task(**dict({'base': ShPeriodicTask}, **options))