alligator.tasks¶
-
class
alligator.tasks.
Task
(task_id=None, retries=0, is_async=True, on_start=None, on_success=None, on_error=None, depends_on=None, delay_by=None, delay_until=None)¶ -
classmethod
deserialize
(data)¶ Given some data from the queue, deserializes it into a Task instance.
The data must be similar in format to what comes from Task.serialize (a JSON-serialized dictionary). Required keys are task_id, retries & is_async.
Parameters: data (str) – A JSON-serialized string of the task data Returns: A populated task Return type: Task
-
run
()¶ Runs the task.
This fires the on_start hook function first (if present), passing the task itself.
Then it runs the target function supplied via Task.to_call with its arguments & stores the result.
If the target function succeeded, the on_success hook function is called, passing both the task & the result to it.
If the target function failed (threw an exception), the on_error hook function is called, passing both the task & the exception to it. Then the exception is re-raised.
Finally, it returns itself, with Task.result set to the result from the target function’s execution.
Returns: The processed Task. Return type: Task
-
serialize
()¶ Serializes the Task data for storing in the queue.
All data must be JSON-serializable in order to be stored properly.
Returns: A JSON string of the task data. Return type: str
-
to_call
(func, *args, **kwargs)¶ Sets the function & its arguments to be called when the task is processed.
Ex:
task.to_call(my_function, 1, 'c', another=True)
Parameters: - func (callable) – The callable with business logic to execute
- args (list) – Positional arguments to pass to the callable task
- kwargs (dict) – Keyword arguments to pass to the callable task
-
to_canceled
()¶ Sets the task’s status as “canceled”.
Useful for the on_start/on_success/on_failed hook methods for figuring out what the status of the task is.
-
to_failed
()¶ Sets the task’s status as “failed”.
Useful for the on_start/on_success/on_failed hook methods for figuring out what the status of the task is.
-
to_retrying
()¶ Sets the task’s status as “retrying”.
Useful for the on_start/on_success/on_failed hook methods for figuring out what the status of the task is.
-
to_success
()¶ Sets the task’s status as “success”.
Useful for the on_start/on_success/on_failed hook methods for figuring out what the status of the task is.
-
to_waiting
()¶ Sets the task’s status as “waiting”.
Useful for the on_start/on_success/on_failed hook methods for figuring out what the status of the task is.
-
classmethod