def revoke(task_id, connection=None, connect_timeout=None): """Revoke a task by id. Revoked tasks will not be executed after all. """ def _revoke(connection): broadcast = BroadcastPublisher(connection) try: broadcast.revoke(task_id) finally: broadcast.close() return with_connection(_revoke, connection=connection, connect_timeout=connect_timeout)
def discard_all(connect_timeout=AMQP_CONNECTION_TIMEOUT): """Discard all waiting tasks. This will ignore all tasks waiting for execution, and they will be deleted from the messaging server. :returns: the number of tasks discarded. :rtype: int """ def _discard(connection): consumer = TaskConsumer(connection=connection) try: return consumer.discard_all() finally: consumer.close() return with_connection(_discard, connect_timeout=connect_timeout)
def test_with_connection(self): def foo(**kwargs): pass self.assertTrue(messaging.with_connection(foo))
def apply_async(task, args=None, kwargs=None, countdown=None, eta=None, task_id=None, publisher=None, connection=None, connect_timeout=None, **options): """Run a task asynchronously by the celery daemon(s). :param task: The task to run (a callable object, or a :class:`Task` instance :keyword args: The positional arguments to pass on to the task (a ``list``). :keyword kwargs: The keyword arguments to pass on to the task (a ``dict``) :keyword countdown: Number of seconds into the future that the task should execute. Defaults to immediate delivery (Do not confuse that with the ``immediate`` setting, they are unrelated). :keyword eta: A :class:`datetime.datetime` object that describes the absolute time when the task should execute. May not be specified if ``countdown`` is also supplied. (Do not confuse this with the ``immediate`` setting, they are unrelated). :keyword routing_key: The routing key used to route the task to a worker server. :keyword exchange: The named exchange to send the task to. Defaults to :attr:`celery.task.base.Task.exchange`. :keyword immediate: Request immediate delivery. Will raise an exception if the task cannot be routed to a worker immediately. (Do not confuse this parameter with the ``countdown`` and ``eta`` settings, as they are unrelated). :keyword mandatory: Mandatory routing. Raises an exception if there's no running workers able to take on this task. :keyword connection: Re-use existing AMQP connection. The ``connect_timeout`` argument is not respected if this is set. :keyword connect_timeout: The timeout in seconds, before we give up on establishing a connection to the AMQP server. :keyword priority: The task priority, a number between ``0`` and ``9``. :keyword serializer: A string identifying the default serialization method to use. Defaults to the ``CELERY_TASK_SERIALIZER`` setting. Can be ``pickle`` ``json``, ``yaml``, or any custom serialization methods that have been registered with :mod:`carrot.serialization.registry`. **Note**: If the ``CELERY_ALWAYS_EAGER`` setting is set, it will be replaced by a local :func:`apply` call instead. """ from celery.conf import ALWAYS_EAGER if ALWAYS_EAGER: return apply(task, args, kwargs) for option_name in TASK_EXEC_OPTIONS: if option_name not in options: options[option_name] = getattr(task, option_name, None) if countdown: # Convert countdown to ETA. eta = datetime.now() + timedelta(seconds=countdown) def _delay_task(connection): publish = publisher or TaskPublisher(connection) try: return publish.delay_task(task.name, args or [], kwargs or {}, task_id=task_id, eta=eta, **options) finally: publisher or publish.close() task_id = with_connection(_delay_task, connection=connection, connect_timeout=connect_timeout) return AsyncResult(task_id)