The stand-in task and the relay
The problem this design solves
A Nornir runner must do much more than run a function for each host. It must record each
host's result, apply the deadlines, deliver cancellation, call the processors and mark failed
hosts. Nornir's own runners, ThreadedRunner and AsyncioRunner, already do all of this.
The ray runner does not write this logic again. It gives Nornir's own runners a different
task to run: a stand-in task. For each host, the stand-in task does not run your function.
It asks a worker to run it and waits for the result.
The result: deadlines, cancellation, processors and results follow the rules of the built-in runners, because the built-in runners apply them.
The stand-in task
The stand-in task is the class RemoteTask. It is a Nornir Task that wraps a copy of your
task. When Nornir starts it for a host, it:
- prepares the call on the driver. For an Operation, Nornir selects the task for the host's platform and builds its input here;
- runs the relay function in place of your function;
- checks the result on the driver. For an Operation, the check of the output runs here.
The stand-in keeps the name and the parameters of your task, so processors receive the same task name and parameters as with another runner.
For a sync run, the runner starts ThreadedRunner with as many threads as the workers have
places for hosts. Each thread on the driver only waits for a worker, so these threads use
little processor time.
The call sent to the worker
The relay sends one TaskCall for each host. It holds:
| Field | Content |
|---|---|
| Call identity | A unique identifier, used later to cancel or abandon the call |
| Host | A copy of the host without its open sessions |
| Fingerprint | A hash of that copy, used for session reuse |
| Site | The host's site, if it has one |
| Task | The task function and its parameters. For an Operation: the Operation name, the platform, the connection, the name of the selected task and the built input |
| Task details | The task's name, its severity level, the dry-run setting, and whether the task is async |
| Configuration | The Nornir configuration, used to build a Nornir object on the worker |
The events the worker sends back
The worker answers with a stream of events, in order:
| Event | What the relay does on the driver |
|---|---|
| Partial result | Yields a Nornir PartialResult for the host |
| Subtask started | Calls subtask_instance_started on the processors of the run |
| Subtask completed | Calls subtask_instance_completed with the rebuilt results |
| Final result | Adds the results of the subtasks to the task and returns the top result, with the ray attribute set |
The worker numbers each subtask of a call when it starts: 1, 2, and so on. Each event carries the number of its subtask. The relay matches events to subtasks by this number, so two subtasks that run at the same time or share a name do not get mixed.
How a failure travels
A task failure does not make the call to the worker fail. The failure is inside the final result.
The worker sends the exception your task raised to the driver in one of three ways, tried in this order:
- as the exception itself, when Python can serialize it and rebuild it;
- as its type and attributes, rebuilt on the driver without calling its constructor. Nornir's own exceptions need this;
- as a
RemoteTaskErrorthat holds the type name and the message of the original exception.
Changes to host.data
When the task changed host.data on the worker, the final result holds the host's new data.
The relay puts it into the driver's inventory.
The change alters the host's fingerprint. Without more care, the worker would treat the next call as a changed host and close its sessions. The driver therefore records the fingerprint before and after the change, and the host's next call tells the worker which fingerprint it replaces. The worker, which already holds the changed data, keeps the host and its sessions.