Reference¶
django_celery_extensions.config¶
django_celery_extensions.task¶
-
class
django_celery_extensions.task.DjangoTask[source]¶ Bases:
celery.app.task.Task-
apply(args=None, kwargs=None, **options)[source]¶ Execute this task locally, by blocking until the task returns.
Parameters: - args (Tuple) – positional arguments passed on to the task.
- kwargs (Dict) – keyword arguments passed on to the task.
- throw (bool) – Re-raise task exceptions. Defaults to the :setting:`task_eager_propagates` setting.
Returns: pre-evaluated result.
Return type: celery.result.EagerResult
-
apply_async(args=None, kwargs=None, **options)[source]¶ Apply tasks asynchronously by sending a message.
Parameters: - args (Tuple) – The positional arguments to pass on to the task.
- kwargs (Dict) – The keyword arguments to pass on to the task.
- countdown (float) – Number of seconds into the future that the task should execute. Defaults to immediate execution.
- eta (datetime) – Absolute time and date of when the task should be executed. May not be specified if countdown is also supplied.
- expires (float, datetime) – Datetime or seconds in the future for the task should expire. The task won’t be executed after the expiration time.
- shadow (str) – Override task name used in logs/monitoring.
Default is retrieved from
shadow_name(). - connection (kombu.Connection) – Re-use existing broker connection instead of acquiring one from the connection pool.
- retry (bool) – If enabled sending of the task message will be retried in the event of connection loss or failure. Default is taken from the :setting:`task_publish_retry` setting. Note that you need to handle the producer/connection manually for this to work.
- retry_policy (Mapping) – Override the retry policy used. See the :setting:`task_publish_retry_policy` setting.
- time_limit (int) – If set, overrides the default time limit.
- soft_time_limit (int) – If set, overrides the default soft time limit.
- queue (str, kombu.Queue) – The queue to route the task to. This must be a key present in :setting:`task_queues`, or :setting:`task_create_missing_queues` must be enabled. See guide-routing for more information.
- exchange (str, kombu.Exchange) – Named custom exchange to send the
task to. Usually not used in combination with the
queueargument. - routing_key (str) – Custom routing key used to route the task to a
worker server. If in combination with a
queueargument only used to specify custom routing keys to topic exchanges. - priority (int) – The task priority, a number between 0 and 9.
Defaults to the
priorityattribute. - serializer (str) – Serialization method to use.
Can be pickle, json, yaml, msgpack or any custom
serialization method that’s been registered
with
kombu.serialization.registry. Defaults to theserializerattribute. - compression (str) – Optional compression method
to use. Can be one of
zlib,bzip2, or any custom compression methods registered withkombu.compression.register(). Defaults to the :setting:`task_compression` setting. - link (Signature) – A single, or a list of tasks signatures to apply if the task returns successfully.
- link_error (Signature) – A single, or a list of task signatures to apply if an error occurs while executing the task.
- producer (kombu.Producer) – custom producer to use when publishing the task.
- add_to_parent (bool) – If set to True (default) and the task
is applied while executing another task, then the result
will be appended to the parent tasks
request.childrenattribute. Trailing can also be disabled by default using thetrailattribute - ignore_result (bool) – If set to False (default) the result
of a task will be stored in the backend. If set to True
the result will not be stored. This can also be set
using the
ignore_resultin the app.task decorator. - publisher (kombu.Producer) – Deprecated alias to
producer. - headers (Dict) – Message headers to be included in the message.
Returns: Promise of future evaluation.
Return type: celery.result.AsyncResult
Raises: TypeError– If not enough arguments are passed, or too many arguments are passed. Note that signature checks may be disabled by specifying@task(typing=False).kombu.exceptions.OperationalError– If a connection to the transport cannot be made, or if the connection is lost.
Note
Also supports all keyword arguments supported by
kombu.Producer.publish().
-
apply_async_and_get_result(args=None, kwargs=None, timeout=None, propagate=True, **options)[source]¶ Apply task in an asynchronous way, wait defined timeout and get AsyncResult or TimeoutError :param args: task args :param kwargs: task kwargs :param timeout: timout in seconds to wait for result :param propagate: propagate or not exceptions from celery task :param options: apply_async method options :return: AsyncResult or TimeoutError
-
on_failure(exc, task_id, args, kwargs, einfo)[source]¶ Error handler.
This is run by the worker when the task fails.
Parameters: - exc (Exception) – The exception raised by the task.
- task_id (str) – Unique id of the failed task.
- args (Tuple) – Original arguments for the task that failed.
- kwargs (Dict) – Original keyword arguments for the task that failed.
- einfo (ExceptionInfo) – Exception information.
Returns: The return value of this handler is ignored.
Return type: None
-
on_invocation_apply(invocation_id, args, kwargs, options, result)[source]¶ Method is called when task was applied with the requester. :param invocation_id: UUID of the requester invocation :param args: input task args :param kwargs: input task kwargs :param options: input task options :param result: result which will be finally returned
-
on_invocation_ignored(invocation_id, args, kwargs, task_id, options, result)[source]¶ Task has been triggered but the task has set ignore_task_timedelta and task was sucessfully completed in this timeout. Therefore no new task is invoked. :param invocation_id: UUID of the requester invocation :param args: input task args :param kwargs: input task kwargs :param task_id: UUID of the celery task :param options: input task options :param result: result which will be finally returned
-
on_invocation_timeout(invocation_id, args, kwargs, task_id, ex, options, result)[source]¶ Task has been joined to another unique async result. :param invocation_id: UUID of the requester invocation :param args: input task args :param kwargs: input task kwargs :param task_id: UUID of the celery task :param ex: celery TimeoutError :param options: input task options :param result: result which will be finally returned
-
on_invocation_trigger(invocation_id, args, kwargs, task_id, options, result)[source]¶ Task has been triggered and placed in the queue. :param invocation_id: UUID of the requester invocation :param args: input task args :param kwargs: input task kwargs :param task_id: UUID of the celery task :param options: input task options :param result: result which will be finally returned
-
on_invocation_unique(invocation_id, args, kwargs, task_id, options, result)[source]¶ Task has been triggered but the same task is already active. Therefore only pointer to the active task is returned. :param invocation_id: UUID of the requester invocation :param args: input task args :param kwargs: input task kwargs :param task_id: UUID of the celery task :param options: input task options :param result: result which will be finally returned
-
on_success(retval, task_id, args, kwargs)[source]¶ Success handler.
Run by the worker if the task executes successfully.
Parameters: - retval (Any) – The return value of the task.
- task_id (str) – Unique id of the executed task.
- args (Tuple) – Original arguments for the executed task.
- kwargs (Dict) – Original keyword arguments for the executed task.
Returns: The return value of this handler is ignored.
Return type: None
-
on_task_failure(task_id, args, kwargs, exc, einfo)[source]¶ Task failed and will not be retried. :param task_id: UUID of the celery task :param args: task args :param kwargs: task kwargs :param exc: raised exception :param einfo: exception traceback
-
on_task_retry(task_id, args, kwargs, exc, eta)[source]¶ Task failed but will be retried. :param task_id: UUID of the celery task :param args: task args :param kwargs: task kwargs :param exc: raised exception which caused retry :param eta: time to next retry
-
on_task_start(task_id, args, kwargs)[source]¶ Task has been started with worker. :param task_id: UUID of the celery task :param args: input task args :param kwargs: input task kwargs
-
on_task_success(task_id, args, kwargs, retval)[source]¶ Task was successful. :param task_id: UUID of the celery task :param args: task args :param kwargs: task kwargs :param retval: task result
-
retry(args=None, kwargs=None, exc=None, throw=True, eta=None, countdown=None, max_retries=None, default_retry_delays=None, headers=None, **options)[source]¶ Retry the task, adding it to the back of the queue.
Example
>>> from imaginary_twitter_lib import Twitter >>> from proj.celery import app
>>> @app.task(bind=True) ... def tweet(self, auth, message): ... twitter = Twitter(oauth=auth) ... try: ... twitter.post_status_update(message) ... except twitter.FailWhale as exc: ... # Retry in 5 minutes. ... self.retry(countdown=60 * 5, exc=exc)
Note
Although the task will never return above as retry raises an exception to notify the worker, we use raise in front of the retry to convey that the rest of the block won’t be executed.
Parameters: - args (Tuple) – Positional arguments to retry with.
- kwargs (Dict) – Keyword arguments to retry with.
- exc (Exception) –
Custom exception to report when the max retry limit has been exceeded (default:
@MaxRetriesExceededError).If this argument is set and retry is called while an exception was raised (
sys.exc_info()is set) it will attempt to re-raise the current exception.If no exception was raised it will raise the
excargument provided. - countdown (float) – Time in seconds to delay the retry for.
- eta (datetime) – Explicit time and date to run the retry at.
- max_retries (int) – If set, overrides the default retry limit for
this execution. Changes to this parameter don’t propagate to
subsequent task retry attempts. A value of
None, means “use the default”, so if you want infinite retries you’d have to set themax_retriesattribute of the task toNonefirst. - time_limit (int) – If set, overrides the default time limit.
- soft_time_limit (int) – If set, overrides the default soft time limit.
- throw (bool) – If this is
False, don’t raise the@Retryexception, that tells the worker to mark the task as being retried. Note that this means the task will be marked as failed if the task raises an exception, or successful if it returns after the retry call. - **options (Any) – Extra options to pass on to
apply_async().
Raises: celery.exceptions.Retry– To tell the worker that the task has been re-sent for retry. This always happens, unless the throw keyword argument has been explicitly set toFalse, and is considered normal operation.
-