|
@@ -117,56 +117,56 @@ class BaseTask(object):
|
|
|
|
|
|
MaxRetriesExceededError = MaxRetriesExceededError
|
|
|
|
|
|
- #: the application instance associated with this task class.
|
|
|
+ #: The application instance associated with this task class.
|
|
|
app = None
|
|
|
|
|
|
- #: name of the task.
|
|
|
+ #: Name of the task.
|
|
|
name = None
|
|
|
|
|
|
- #: if :const:`True` the task is an abstract base class.
|
|
|
+ #: If :const:`True` the task is an abstract base class.
|
|
|
abstract = True
|
|
|
|
|
|
- #: if disabled the worker will not forward magic keyword arguments.
|
|
|
+ #: If disabled the worker will not forward magic keyword arguments.
|
|
|
accept_magic_kwargs = True
|
|
|
|
|
|
- #: current request context when task is being executed.
|
|
|
+ #: Current request context (when task is executed).
|
|
|
request = Context()
|
|
|
|
|
|
- #: select a destination queue for this task. The queue needs to exist
|
|
|
+ #: Destination queue. The queue needs to exist
|
|
|
#: in :setting:`CELERY_QUEUES`. The `routing_key`, `exchange` and
|
|
|
#: `exchange_type` attributes will be ignored if this is set.
|
|
|
queue = None
|
|
|
|
|
|
- #: override the apps default `routing_key` for this task.
|
|
|
+ #: Overrides the apps default `routing_key` for this task.
|
|
|
routing_key = None
|
|
|
|
|
|
- #: override the apps default `exchange` for this task.
|
|
|
+ #: Overrides the apps default `exchange` for this task.
|
|
|
exchange = None
|
|
|
|
|
|
- #: override the apps default exchange type for this task.
|
|
|
+ #: Overrides the apps default exchange type for this task.
|
|
|
exchange_type = None
|
|
|
|
|
|
- #: override the apps default delivery mode for this task. Default is
|
|
|
+ #: Override the apps default delivery mode for this task. Default is
|
|
|
#: `"persistent"`, but you can change this to `"transient"`, which means
|
|
|
#: messages will be lost if the broker is restarted. Consult your broker
|
|
|
#: manual for any additional delivery modes.
|
|
|
delivery_mode = None
|
|
|
|
|
|
- #: mandatory message routing.
|
|
|
+ #: Mandatory message routing.
|
|
|
mandatory = False
|
|
|
|
|
|
- #: request immediate delivery.
|
|
|
+ #: Request immediate delivery.
|
|
|
immediate = False
|
|
|
|
|
|
- #: default message priority. A number between 0 to 9, where 0 is the
|
|
|
+ #: Default message priority. A number between 0 to 9, where 0 is the
|
|
|
#: highest. Note that RabbitMQ does not support priorities.
|
|
|
priority = None
|
|
|
|
|
|
- #: maximum number of retries before giving up. If set to :const:`None`,
|
|
|
+ #: Maximum number of retries before giving up. If set to :const:`None`,
|
|
|
#: it will **never** stop retrying.
|
|
|
max_retries = 3
|
|
|
|
|
|
- #: default time in seconds before a retry of the task should be
|
|
|
+ #: Default time in seconds before a retry of the task should be
|
|
|
#: executed. 3 minutes by default.
|
|
|
default_retry_delay = 3 * 60
|
|
|
|
|
@@ -175,12 +175,12 @@ class BaseTask(object):
|
|
|
#: a minute),`"100/h"` (hundred tasks an hour)
|
|
|
rate_limit = None
|
|
|
|
|
|
- #: if enabled the worker will not store task state and return values
|
|
|
+ #: If enabled the worker will not store task state and return values
|
|
|
#: for this task. Defaults to the :setting:`CELERY_IGNORE_RESULT`
|
|
|
#: setting.
|
|
|
ignore_result = False
|
|
|
|
|
|
- #: when enabled errors will be stored even if the task is otherwise
|
|
|
+ #: When enabled errors will be stored even if the task is otherwise
|
|
|
#: configured to ignore results.
|
|
|
store_errors_even_if_ignored = False
|
|
|
|
|
@@ -190,21 +190,21 @@ class BaseTask(object):
|
|
|
|
|
|
disable_error_emails = False # FIXME
|
|
|
|
|
|
- #: list of exception types to send error e-mails for.
|
|
|
+ #: List of exception types to send error e-mails for.
|
|
|
error_whitelist = ()
|
|
|
|
|
|
- #: the name of a serializer that has been registered with
|
|
|
+ #: The name of a serializer that has been registered with
|
|
|
#: :mod:`carrot.serialization.registry`. Example: `"json"`.
|
|
|
serializer = "pickle"
|
|
|
|
|
|
- #: the result store backend used for this task.
|
|
|
+ #: The result store backend used for this task.
|
|
|
backend = None
|
|
|
|
|
|
- #: if disabled the task will not be automatically registered
|
|
|
+ #: If disabled the task will not be automatically registered
|
|
|
#: in the task registry.
|
|
|
autoregister = True
|
|
|
|
|
|
- #: if enabled the task will report its status as "started" when the task
|
|
|
+ #: If enabled the task will report its status as "started" when the task
|
|
|
#: is executed by a worker. Disabled by default as the normal behaviour
|
|
|
#: is to not report that level of granularity. Tasks are either pending,
|
|
|
#: finished, or waiting to be retried.
|
|
@@ -217,7 +217,7 @@ class BaseTask(object):
|
|
|
#: :setting:`CELERY_TRACK_STARTED` setting.
|
|
|
track_started = False
|
|
|
|
|
|
- #: when enabled messages for this task will be acknowledged **after**
|
|
|
+ #: When enabled messages for this task will be acknowledged **after**
|
|
|
#: the task has been executed, and not *just before* which is the
|
|
|
#: default behavior.
|
|
|
#:
|
|
@@ -229,10 +229,10 @@ class BaseTask(object):
|
|
|
#: :setting:`CELERY_ACKS_LATE` setting.
|
|
|
acks_late = False
|
|
|
|
|
|
- #: default task expiry time.
|
|
|
+ #: Default task expiry time.
|
|
|
expires = None
|
|
|
|
|
|
- #: the type of task *(no longer used)*.
|
|
|
+ #: The type of task *(no longer used)*.
|
|
|
type = "regular"
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
@@ -248,19 +248,22 @@ class BaseTask(object):
|
|
|
automatically passed by the worker if the function/method
|
|
|
supports them:
|
|
|
|
|
|
- * task_id
|
|
|
- * task_name
|
|
|
- * task_retries
|
|
|
- * task_is_eager
|
|
|
- * logfile
|
|
|
- * loglevel
|
|
|
- * delivery_info
|
|
|
+ * `task_id`
|
|
|
+ * `task_name`
|
|
|
+ * `task_retries
|
|
|
+ * `task_is_eager`
|
|
|
+ * `logfile`
|
|
|
+ * `loglevel`
|
|
|
+ * `delivery_info`
|
|
|
|
|
|
- Additional standard keyword arguments may be added in the future.
|
|
|
To take these default arguments, the task can either list the ones
|
|
|
it wants explicitly or just take an arbitrary list of keyword
|
|
|
arguments (\*\*kwargs).
|
|
|
|
|
|
+ Magic keyword arguments can be disabled using the
|
|
|
+ :attr:`accept_magic_kwargs` flag. The information can then
|
|
|
+ be found in the :attr:`request` attribute.
|
|
|
+
|
|
|
"""
|
|
|
raise NotImplementedError("Tasks must define the run method.")
|
|
|
|
|
@@ -291,11 +294,11 @@ class BaseTask(object):
|
|
|
|
|
|
:rtype :class:`~celery.app.amqp.TaskPublisher`:
|
|
|
|
|
|
- Please be sure to close the AMQP connection when you're done
|
|
|
- with this object, i.e.:
|
|
|
+ Please be sure to close the AMQP connection after you're done
|
|
|
+ with this object. Example::
|
|
|
|
|
|
>>> publisher = self.get_publisher()
|
|
|
- >>> # do something with publisher
|
|
|
+ >>> # ... do something with publisher
|
|
|
>>> publisher.connection.close()
|
|
|
|
|
|
"""
|
|
@@ -311,12 +314,12 @@ class BaseTask(object):
|
|
|
|
|
|
@classmethod
|
|
|
def get_consumer(self, connection=None, connect_timeout=None):
|
|
|
- """Get a celery task message consumer.
|
|
|
+ """Get message consumer.
|
|
|
|
|
|
:rtype :class:`~celery.app.amqp.TaskConsumer`:
|
|
|
|
|
|
Please be sure to close the AMQP connection when you're done
|
|
|
- with this object. i.e.:
|
|
|
+ with this object. Example::
|
|
|
|
|
|
>>> consumer = self.get_consumer()
|
|
|
>>> # do something with consumer
|
|
@@ -330,8 +333,8 @@ class BaseTask(object):
|
|
|
|
|
|
@classmethod
|
|
|
def delay(self, *args, **kwargs):
|
|
|
- """Shortcut to :meth:`apply_async`, with star arguments,
|
|
|
- but doesn't support the extra options.
|
|
|
+ """Shortcut to :meth:`apply_async` giving star arguments, but without
|
|
|
+ options.
|
|
|
|
|
|
:param \*args: positional arguments passed on to the task.
|
|
|
:param \*\*kwargs: keyword arguments passed on to the task.
|
|
@@ -349,68 +352,71 @@ class BaseTask(object):
|
|
|
"""Run a task asynchronously by the celery daemon(s).
|
|
|
|
|
|
:keyword args: The positional arguments to pass on to the
|
|
|
- task (a :class:`list` or :class:`tuple`).
|
|
|
+ task (a :class:`list` or :class:`tuple`).
|
|
|
|
|
|
:keyword kwargs: The keyword arguments to pass on to the
|
|
|
- task (a :class:`dict`)
|
|
|
+ task (a :class:`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).
|
|
|
+ task should execute. Defaults to immediate
|
|
|
+ delivery (do not confuse with the
|
|
|
+ `immediate` flag, as they are unrelated).
|
|
|
|
|
|
- :keyword eta: A :class:`~datetime.datetime` object that describes
|
|
|
- the absolute time and date of 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 eta: A :class:`~datetime.datetime` object describing
|
|
|
+ the absolute time and date of when the task should
|
|
|
+ be executed. May not be specified if `countdown`
|
|
|
+ is also supplied. (Do not confuse this with the
|
|
|
+ `immediate` flag, as they are unrelated).
|
|
|
|
|
|
:keyword expires: Either a :class:`int`, describing the number of
|
|
|
- seconds, or a :class:`~datetime.datetime` object that
|
|
|
- describes the absolute time and date of when the task should
|
|
|
- expire. The task will not be executed after the
|
|
|
- expiration time.
|
|
|
+ seconds, or a :class:`~datetime.datetime` object
|
|
|
+ that describes the absolute time and date of when
|
|
|
+ the task should expire. The task will not be
|
|
|
+ executed after the expiration time.
|
|
|
|
|
|
:keyword connection: Re-use existing broker connection instead
|
|
|
- of establishing a new one. The `connect_timeout` argument
|
|
|
- is not respected if this is set.
|
|
|
+ of establishing a new one. 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 connect_timeout: The timeout in seconds, before we give up
|
|
|
+ on establishing a connection to the AMQP
|
|
|
+ server.
|
|
|
|
|
|
:keyword routing_key: The routing key used to route the task to a
|
|
|
- worker server. Defaults to the tasks
|
|
|
- :attr:`routing_key` attribute.
|
|
|
+ worker server. Defaults to the
|
|
|
+ :attr:`routing_key` attribute.
|
|
|
|
|
|
:keyword exchange: The named exchange to send the task to.
|
|
|
- Defaults to the tasks :attr:`exchange` attribute.
|
|
|
+ Defaults to the :attr:`exchange` attribute.
|
|
|
|
|
|
- :keyword exchange_type: The exchange type to initalize the
|
|
|
- exchange if not already declared. Defaults to the tasks
|
|
|
- :attr:`exchange_type` attribute.
|
|
|
+ :keyword exchange_type: The exchange type to initalize the exchange
|
|
|
+ if not already declared. Defaults to the
|
|
|
+ :attr:`exchange_type` attribute.
|
|
|
|
|
|
- :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). Defaults to the tasks :attr:`immediate` attribute.
|
|
|
+ :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). Defaults to the :attr:`immediate`
|
|
|
+ attribute.
|
|
|
|
|
|
:keyword mandatory: Mandatory routing. Raises an exception if
|
|
|
- there's no running workers able to take on this task.
|
|
|
- Defaults to the tasks :attr:`mandatory` attribute.
|
|
|
+ there's no running workers able to take on this
|
|
|
+ task. Defaults to the :attr:`mandatory`
|
|
|
+ attribute.
|
|
|
|
|
|
:keyword priority: The task priority, a number between 0 and 9.
|
|
|
- Defaults to the tasks :attr:`priority` attribute.
|
|
|
+ Defaults to the :attr:`priority` attribute.
|
|
|
|
|
|
- :keyword serializer: A string identifying the default
|
|
|
- serialization method to use. Defaults to the
|
|
|
- :setting:`CELERY_TASK_SERIALIZER` setting. Can be `pickle`,
|
|
|
- `json`, `yaml`, or any custom serialization method
|
|
|
- that has been registered with
|
|
|
- :mod:`carrot.serialization.registry`. Defaults to the tasks
|
|
|
- :attr:`serializer` attribute.
|
|
|
+ :keyword serializer: A string identifying the default serialization
|
|
|
+ method to use. Can be `pickle`, `json`, `yaml`,
|
|
|
+ `msgpack` or any custom serialization method
|
|
|
+ that has been registered with
|
|
|
+ :mod:`carrot.serialization.registry`.
|
|
|
+ Defaults to :attr:`serializer` attribute.
|
|
|
|
|
|
- **Note**: If the :setting:`CELERY_ALWAYS_EAGER` setting is set, it will
|
|
|
+ .. note::
|
|
|
+ If the :setting:`CELERY_ALWAYS_EAGER` setting is set, it will
|
|
|
be replaced by a local :func:`apply` call instead.
|
|
|
|
|
|
"""
|
|
@@ -450,38 +456,41 @@ class BaseTask(object):
|
|
|
:param args: Positional arguments to retry with.
|
|
|
:param kwargs: Keyword arguments to retry with.
|
|
|
:keyword exc: Optional exception to raise instead of
|
|
|
- :exc:`~celery.exceptions.MaxRetriesExceededError` when the max
|
|
|
- restart limit has been exceeded.
|
|
|
+ :exc:`~celery.exceptions.MaxRetriesExceededError`
|
|
|
+ when the max restart limit has been exceeded.
|
|
|
:keyword countdown: Time in seconds to delay the retry for.
|
|
|
:keyword eta: Explicit time and date to run the retry at
|
|
|
- (must be a :class:`~datetime.datetime` instance).
|
|
|
+ (must be a :class:`~datetime.datetime` instance).
|
|
|
:keyword \*\*options: Any extra options to pass on to
|
|
|
- meth:`apply_async`. See :func:`celery.execute.apply_async`.
|
|
|
+ meth:`apply_async`.
|
|
|
:keyword throw: If this is :const:`False`, do not raise the
|
|
|
- :exc:`~celery.exceptions.RetryTaskError` exception,
|
|
|
- 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.
|
|
|
+ :exc:`~celery.exceptions.RetryTaskError` exception,
|
|
|
+ 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.
|
|
|
|
|
|
:raises celery.exceptions.RetryTaskError: 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
|
|
|
to :const:`False`, and is considered normal operation.
|
|
|
|
|
|
- Example
|
|
|
+ **Example**
|
|
|
+
|
|
|
+ .. code-block:: python
|
|
|
|
|
|
- >>> class TwitterPostStatusTask(Task):
|
|
|
- ...
|
|
|
- ... def run(self, username, password, message, **kwargs):
|
|
|
- ... twitter = Twitter(username, password)
|
|
|
- ... try:
|
|
|
- ... twitter.post_status(message)
|
|
|
- ... except twitter.FailWhale, exc:
|
|
|
- ... # Retry in 5 minutes.
|
|
|
- ... self.retry([username, password, message],
|
|
|
- ... kwargs,
|
|
|
- ... countdown=60 * 5, exc=exc)
|
|
|
+ >>> @task
|
|
|
+ >>> def tweet(auth, message):
|
|
|
+ ... twitter = Twitter(oauth=auth)
|
|
|
+ ... try:
|
|
|
+ ... twitter.post_status_update(message)
|
|
|
+ ... except twitter.FailWhale, exc:
|
|
|
+ ... # Retry in 5 minutes.
|
|
|
+ ... return tweet.retry(countdown=60 * 5, exc=exc)
|
|
|
+
|
|
|
+ Although the task will never return above as `retry` raises an
|
|
|
+ exception to notify the worker, we use `return` in front of the retry
|
|
|
+ to convey that the rest of the block will not be executed.
|
|
|
|
|
|
"""
|
|
|
request = self.request
|
|
@@ -498,7 +507,7 @@ class BaseTask(object):
|
|
|
options["retries"] = request.retries + 1
|
|
|
options["task_id"] = request.id
|
|
|
options["countdown"] = options.get("countdown",
|
|
|
- self.default_retry_delay)
|
|
|
+ self.default_retry_delay)
|
|
|
max_exc = exc or self.MaxRetriesExceededError(
|
|
|
"Can't retry %s[%s] args:%s kwargs:%s" % (
|
|
|
self.name, options["task_id"], args, kwargs))
|
|
@@ -527,13 +536,12 @@ class BaseTask(object):
|
|
|
|
|
|
:param args: positional arguments passed on to the task.
|
|
|
:param kwargs: keyword arguments passed on to the task.
|
|
|
- :keyword throw: Re-raise task exceptions. Defaults to
|
|
|
- the :setting:`CELERY_EAGER_PROPAGATES_EXCEPTIONS` setting.
|
|
|
+ :keyword throw: Re-raise task exceptions. Defaults to
|
|
|
+ the :setting:`CELERY_EAGER_PROPAGATES_EXCEPTIONS`
|
|
|
+ setting.
|
|
|
|
|
|
:rtype :class:`celery.result.EagerResult`:
|
|
|
|
|
|
- See :func:`celery.execute.apply`.
|
|
|
-
|
|
|
"""
|
|
|
args = args or []
|
|
|
kwargs = kwargs or {}
|
|
@@ -607,7 +615,7 @@ class BaseTask(object):
|
|
|
:param kwargs: Original keyword arguments for the retried task.
|
|
|
|
|
|
:keyword einfo: :class:`~celery.datastructures.ExceptionInfo`
|
|
|
- instance, containing the traceback.
|
|
|
+ instance, containing the traceback.
|
|
|
|
|
|
The return value of this handler is ignored.
|
|
|
|
|
@@ -623,10 +631,10 @@ class BaseTask(object):
|
|
|
:param task_id: Unique id of the task.
|
|
|
:param args: Original arguments for the task that failed.
|
|
|
:param kwargs: Original keyword arguments for the task
|
|
|
- that failed.
|
|
|
+ that failed.
|
|
|
|
|
|
:keyword einfo: :class:`~celery.datastructures.ExceptionInfo`
|
|
|
- instance, containing the traceback (if any).
|
|
|
+ instance, containing the traceback (if any).
|
|
|
|
|
|
The return value of this handler is ignored.
|
|
|
|
|
@@ -642,10 +650,10 @@ class BaseTask(object):
|
|
|
:param task_id: Unique id of the failed task.
|
|
|
:param args: Original arguments for the task that failed.
|
|
|
:param kwargs: Original keyword arguments for the task
|
|
|
- that failed.
|
|
|
+ that failed.
|
|
|
|
|
|
:keyword einfo: :class:`~celery.datastructures.ExceptionInfo`
|
|
|
- instance, containing the traceback.
|
|
|
+ instance, containing the traceback.
|
|
|
|
|
|
The return value of this handler is ignored.
|
|
|
|
|
@@ -679,7 +687,7 @@ class BaseTask(object):
|
|
|
wrapper.execute_using_pool(pool, loglevel, logfile)
|
|
|
|
|
|
def __repr__(self):
|
|
|
- """repr(task)"""
|
|
|
+ """`repr(task)`"""
|
|
|
try:
|
|
|
kind = self.__class__.mro()[1].__name__
|
|
|
except (AttributeError, IndexError): # pragma: no cover
|
|
@@ -688,8 +696,8 @@ class BaseTask(object):
|
|
|
|
|
|
@classmethod
|
|
|
def subtask(cls, *args, **kwargs):
|
|
|
- """Returns a :class:`~celery.task.sets.subtask` object for
|
|
|
- this task that wraps arguments and execution options
|
|
|
+ """Returns :class:`~celery.task.sets.subtask` object for
|
|
|
+ this task, wrapping arguments and execution options
|
|
|
for a single task invocation."""
|
|
|
return subtask(cls, *args, **kwargs)
|
|
|
|