alligator.tasks

class alligator.tasks.Task(task_id=None, retries=0, async=True, on_start=None, on_success=None, on_error=None, depends_on=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 & async.

Parameters:data (string) – A JSON-serialized string of the task data
Returns:A populated task
Return type:A Task instance
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, the result is returned.

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 strong of the task data.
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_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_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.