netunicorn.base.task.Task

class Task(name=None)[source]

Bases: ABC

This is a base class for all tasks. All new task classes should inherit from this class.
The task instance should encapsulate all the logic and data needed to execute the task.
Task entrypoint is the run() method.
Task class can have requirements - commands to be executed to change environment to support this task.
These requirements would be executed with OS shell during environment setup.
Each task is to be implemented for a specific architecture, platform, or combination (like Linux + arm64).
TaskDispatcher can be used for selecting a specific task for the given architecture, platform, or combination.
Task always returns a Result object.
- If the task’s run method returns Result object by itself, you’ll receive this Result object
- If the task’s run method returns any other object, you’ll receive a Success with returned_value encapsulated
- If the task’s run method fires an exception, you’ll receive a Failure with the exception encapsulated
When creating your own tasks, please, do not forget to call super().__init__(*args, **kwargs) in your implementation to properly initialize the base class.
Parameters:

name (Optional[str], default: None) – Name of the task. If not provided, a random UUID will be used.

Attributes

previous_steps

Stores results of previous steps with task name as a key and list of results as a value.

requirements

A list of commands to be executed to change environment to support this task.

Methods

previous_steps: Dict[str, List[Result[Any, Any]]] = {}[source]

Stores results of previous steps with task name as a key and list of results as a value. Several results could be stored for each task if it was executed several times.

requirements: List[str] = [][source]

A list of commands to be executed to change environment to support this task.

__call__()[source]

This method is called when you call the task instance as a function. It’s a shortcut for run method.

Returns:

Any – Result of the execution

add_requirement(command)[source]
This method adds a requirement to the requirements of the instance of the task.
Please, note that the requirement would be added to the instance, not to all instances of the class.
Use it to provide additional requirements that should be executed only once despite the number of instances.
Parameters:

command (str) – Command to be executed to change environment to support this task.

Returns:

Task – self

abstract run()[source]
## This method is to be overridden by your implementation. ##
This is the entrypoint for the task.
This method should never have any arguments except self. Any arguments that task would use for execution should be provided to the constructor and used later by this method.
This method will always return a Result object. If this method doesn’t return a Result object, it will be encapsulated into a Result object.
Returns:

Any – Result of the execution