pub struct TaskDefinition { /* private fields */ }Expand description
A wrapper around a task implementation that enforces the task trait flow.
TaskDefinition encapsulates a task that implements the Task trait, providing
a unified interface for task execution and management. This wrapper enables
polymorphic task handling while maintaining type safety through trait objects.
The wrapper provides access to the underlying task through trait object references, allowing the task to be executed and queried without knowing its concrete type. This is particularly useful for storing heterogeneous collections of tasks and executing them uniformly.
§Fields
inner- A boxed trait object containing the actual task implementation. The task must implement theTasktrait, providing metadata, execution logic, and sub-task management.
§Example
use genja_core::genja_task;
use genja_core::inventory::Host;
use genja_core::task::{
HostTaskResult, Task, TaskDefinition, TaskInfo, TaskRuntimeContext, TaskSuccess,
};
struct MyTask;
#[genja_task(name = "deploy", connection_plugin_name = "ssh")]
impl MyTask {
async fn start_async(
&self,
_host: &Host,
_context: &TaskRuntimeContext,
) -> Result<HostTaskResult, genja_core::task::TaskError> {
Ok(HostTaskResult::passed(TaskSuccess::new()))
}
}
let task = MyTask;
let definition = TaskDefinition::new(task);
assert_eq!(definition.name(), "deploy");Implementations§
Source§impl TaskDefinition
impl TaskDefinition
Sourcepub fn new<T: Task + 'static>(task: T) -> Self
pub fn new<T: Task + 'static>(task: T) -> Self
Wrap a user-defined task that implements the Task trait.
Sourcepub fn with_processor(self, processor_name: impl Into<String>) -> Self
pub fn with_processor(self, processor_name: impl Into<String>) -> Self
Select a processor for the root task definition.
Sub-tasks do not inherit this selection. They select processors through
their own TaskInfo::processor_names implementation.
Sourcepub fn with_processors<I, S>(self, processor_names: I) -> Self
pub fn with_processors<I, S>(self, processor_names: I) -> Self
Select multiple processors for the root task definition.
Sourcepub fn with_processor_resolver(
self,
processor_resolver: Arc<dyn TaskProcessorResolver>,
) -> Self
pub fn with_processor_resolver( self, processor_resolver: Arc<dyn TaskProcessorResolver>, ) -> Self
Attach the processor resolver used during execution.
Sourcepub fn processor_names(&self) -> Vec<&str>
pub fn processor_names(&self) -> Vec<&str>
Returns the root task’s selected processor names.
Source§impl TaskDefinition
impl TaskDefinition
Sourcepub async fn start(
&self,
hostname: &str,
host: &Host,
results: &mut TaskResults,
max_depth: usize,
) -> Result<(), GenjaError>
pub async fn start( &self, hostname: &str, host: &Host, results: &mut TaskResults, max_depth: usize, ) -> Result<(), GenjaError>
Execute this task and all its sub-tasks recursively up to a maximum depth.
This method starts the task execution by calling the task’s start() method,
then recursively executes all sub-tasks returned by sub_tasks(). The recursion
is limited by the max_depth parameter to prevent infinite loops or excessive
nesting.
§Parameters
max_depth- The maximum depth of task nesting allowed. Depth is zero-based: the root task runs at depth0, its immediate sub-tasks at depth1, and so on. This meansmax_depth = 0allows only the root task,max_depth = 1allows the root task plus one level of sub-tasks, and so on.
§Returns
Inserts the provided host’s result into the shared TaskResults tree and
recursively does the same for any sub-tasks. The parent task result is recorded
before sub-task execution starts.
§Errors
This method currently does not return an error for depth overflow. When task
nesting exceeds max_depth, it records an internal failed host result for that
task node and returns Ok(()).
Sourcepub async fn start_with_connection_resolver(
&self,
hostname: &str,
host: &Host,
results: &mut TaskResults,
connection_resolver: Option<&dyn TaskConnectionResolver>,
max_depth: usize,
) -> Result<(), GenjaError>
pub async fn start_with_connection_resolver( &self, hostname: &str, host: &Host, results: &mut TaskResults, connection_resolver: Option<&dyn TaskConnectionResolver>, max_depth: usize, ) -> Result<(), GenjaError>
Executes this task definition while ensuring task-scoped connections are opened.
Sourcepub fn process_task_start(
&self,
results: &mut TaskResults,
) -> Result<(), GenjaError>
pub fn process_task_start( &self, results: &mut TaskResults, ) -> Result<(), GenjaError>
Run aggregate task-start processors for this definition.
Sourcepub fn process_task_finish(
&self,
results: &mut TaskResults,
) -> Result<(), GenjaError>
pub fn process_task_finish( &self, results: &mut TaskResults, ) -> Result<(), GenjaError>
Run aggregate task-finish processors for this definition.
Trait Implementations§
Source§impl Clone for TaskDefinition
impl Clone for TaskDefinition
Source§fn clone(&self) -> TaskDefinition
fn clone(&self) -> TaskDefinition
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl TaskInfo for TaskDefinition
impl TaskInfo for TaskDefinition
Source§fn name(&self) -> &str
fn name(&self) -> &str
Returns the name of the task.
This method delegates to the inner task’s name() implementation, providing
access to the task’s identifier through the TaskDefinition wrapper.
§Returns
A string slice containing the task’s name.
Source§fn connection_plugin_name(&self) -> Option<&str>
fn connection_plugin_name(&self) -> Option<&str>
Returns the name of the plugin associated with this task.
This method delegates to the inner task’s connection_plugin_name() implementation,
providing access to the plugin identifier that will handle the task’s execution.
§Returns
A string slice containing the connection plugin’s name (e.g., “ssh”, “netconf”, “restconf”).
Source§fn get_connection_key(&self, hostname: &str) -> Option<ConnectionKey>
fn get_connection_key(&self, hostname: &str) -> Option<ConnectionKey>
Builds a connection key for the specified host.
This method delegates to the inner task’s get_connection_key() implementation,
constructing a unique identifier that combines the hostname with the plugin name
to identify the connection to be used for task execution.
§Parameters
hostname- The name of the host for which to build the connection key.
§Returns
An optional ConnectionKey that uniquely identifies the connection to the
specified host when this task declares a connection plugin.
Source§fn options(&self) -> Option<&Value>
fn options(&self) -> Option<&Value>
Returns the task’s options payload, if available.
This method delegates to the inner task’s options() implementation, providing
access to any structured configuration or parameters associated with the task.
§Returns
Some(&Value) if the task has options configured, None otherwise.