Skip to main content

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:

  1. prepares the call on the driver. For an Operation, Nornir selects the task for the host's platform and builds its input here;
  2. runs the relay function in place of your function;
  3. 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:

FieldContent
Call identityA unique identifier, used later to cancel or abandon the call
HostA copy of the host without its open sessions
FingerprintA hash of that copy, used for session reuse
SiteThe host's site, if it has one
TaskThe 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 detailsThe task's name, its severity level, the dry-run setting, and whether the task is async
ConfigurationThe 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:

EventWhat the relay does on the driver
Partial resultYields a Nornir PartialResult for the host
Subtask startedCalls subtask_instance_started on the processors of the run
Subtask completedCalls subtask_instance_completed with the rebuilt results
Final resultAdds 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:

  1. as the exception itself, when Python can serialize it and rebuild it;
  2. as its type and attributes, rebuilt on the driver without calling its constructor. Nornir's own exceptions need this;
  3. as a RemoteTaskError that 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.