Example #1
0
class Task(BaseTask):
    abstract = True
    __bound__ = False

    # In old Celery the @task decorator didn't exist, so one would create
    # classes instead and use them directly (e.g. MyTask.apply_async()).
    # the use of classmethods was a hack so that it was not necessary
    # to instantiate the class before using it, but it has only
    # given us pain (like all magic).
    for name in _COMPAT_CLASSMETHODS:
        locals()[name] = reclassmethod(getattr(BaseTask, name))
    app = class_property(_get_app, bind)  # noqa
Example #2
0
class Task(BaseTask):
    """Deprecated Task base class.

    Modern applications should use :class:`celery.Task` instead.

    """
    abstract = True
    __bound__ = False

    #- Deprecated compat. attributes -:

    queue = None
    routing_key = None
    exchange = None
    exchange_type = None
    delivery_mode = None
    mandatory = False
    immediate = False
    priority = None
    type = "regular"
    error_whitelist = ()
    disable_error_emails = False

    from_config = BaseTask.from_config + (
        ("exchange_type", "CELERY_DEFAULT_EXCHANGE_TYPE"),
        ("delivery_mode", "CELERY_DEFAULT_DELIVERY_MODE"),
        ("error_whitelist", "CELERY_TASK_ERROR_WHITELIST"),
    )

    # In old Celery the @task decorator didn't exist, so one would create
    # classes instead and use them directly (e.g. MyTask.apply_async()).
    # the use of classmethods was a hack so that it was not necessary
    # to instantiate the class before using it, but it has only
    # given us pain (like all magic).
    for name in _COMPAT_CLASSMETHODS:
        locals()[name] = reclassmethod(getattr(BaseTask, name))

    @classmethod
    def _get_request(self):
        return self.request_stack.top

    request = class_property(_get_request)

    #: Deprecated alias to :attr:`logger``.
    get_logger = reclassmethod(BaseTask._get_logger)

    @classmethod
    def establish_connection(self, connect_timeout=None):
        """Deprecated method used to get a broker connection.

        Should be replaced with :meth:`@Celery.broker_connection`
        instead, or by acquiring connections from the connection pool:

        .. code-block:: python

            # using the connection pool
            with celery.pool.acquire(block=True) as conn:
                ...

            # establish fresh connection
            with celery.broker_connection() as conn:
                ...
        """
        return self._get_app().broker_connection(
            connect_timeout=connect_timeout)

    def get_publisher(self,
                      connection=None,
                      exchange=None,
                      connect_timeout=None,
                      exchange_type=None,
                      **options):
        """Deprecated method to get the task publisher (now called producer).

        Should be replaced with :class:`@amqp.TaskProducer`:

        .. code-block:: python

            with celery.broker_connection() as conn:
                with celery.amqp.TaskProducer(conn) as prod:
                    my_task.apply_async(producer=prod)

        """
        exchange = self.exchange if exchange is None else exchange
        if exchange_type is None:
            exchange_type = self.exchange_type
        connection = connection or self.establish_connection(connect_timeout)
        return self._get_app().amqp.TaskProducer(
            connection,
            exchange=exchange and Exchange(exchange, exchange_type),
            routing_key=self.routing_key,
            **options)

    @classmethod
    def get_consumer(self, connection=None, queues=None, **kwargs):
        """Deprecated method used to get consumer for the queue
        this task is sent to.

        Should be replaced with :class:`@amqp.TaskConsumer` instead:

        """
        Q = self._get_app().amqp
        connection = connection or self.establish_connection()
        if queues is None:
            queues = Q.queues[self.queue] if self.queue else Q.default_queue
        return Q.TaskConsumer(connection, queues, **kwargs)