django-carrot utilities

This module contains a number of helper functions for performing basic Carrot functions, e.g. publish, schedule and consume

Most users should use the functions defined in this module, rather than attempting to subclass the base level objects

carrot.utilities.create_class_view(view: Any, decorator: Any) → object

Applies a decorator to the dispatch method of a given class based view. Can be chained

carrot.utilities.create_function_view(view: Callable, decorator: Callable) → Callable

Similar to create_class_view(), but attaches a decorator to a function based view, instead of a class-based one

carrot.utilities.create_message(task: Union[str, Callable], queue: str, priority: int = 0, task_args: tuple = (), exchange: str = '', routing_key: str = None, task_kwargs: dict = None) → carrot.objects.Message

Creates a carrot.objects.Message object without publishing it

The task to execute (as a string or a callable) needs to be supplied. All other arguments are optional

carrot.utilities.create_scheduled_task(task: Union[str, Callable], interval: Dict[str, int], task_name: str = None, queue: str = None, **kwargs) → carrot.models.ScheduledTask

Helper function for creating a carrot.models.ScheduledTask

carrot.utilities.decorate_class_view(view_class: object, decorators: List[str] = None) → Any

Loop through a list of string paths to decorator functions, and call create_class_view() for each one

carrot.utilities.decorate_function_view(view: Any, decorators: List[str] = None) → Any

Similar to decorate_class_view(), but for function based views

carrot.utilities.get_host_from_name(name: str) → carrot.objects.VirtualHost

Gets a host object from a given queue name based on the Django configuration

If no queue name is provided (as may be the case from some callers), this function returns a VirtualHost based on the CARROT.default_broker value.

May raise an exception if the given queue name is not registered in the settings.

carrot.utilities.get_mixin(decorator: Callable) → Type[object]

Helper function that allows dynamic application of decorators to a class-based views

Parameters:decorator (func) – the decorator to apply to the view
carrot.utilities.publish_message(task: Union[str, Callable], *task_args, priority: int = 0, queue: str = None, exchange: str = '', routing_key: str = None, **task_kwargs) → carrot.models.MessageLog

Wrapped for create_message(), which publishes the task to the queue

This function is the primary method of publishing tasks to a message queue

carrot.utilities.purge_queue() → None

Deletes all MessageLog objects with status IN_PROGRESS or PUBLISHED add iterate through and purge all RabbitMQ queues

carrot.utilities.requeue_all() → None

Requeues all pending MessageLogs

carrot.utilities.validate_task(task: Union[str, Callable]) → str

Helper function for dealing with task inputs which may either be a callable, or a path to a callable as a string

In case of a string being provided, this function checks whether the import path leads to a valid callable

Otherwise, the callable is converted back into a string (as the carrot.objects.Message requires a string input)

This function is used by the following other utility functions: - create_scheduled_task() - create_message()