Skip to main content

TaskDefinition

Struct TaskDefinition 

Source
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 the Task trait, 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

Source

pub fn new<T: Task + 'static>(task: T) -> Self

Wrap a user-defined task that implements the Task trait.

Source

pub fn as_task(&self) -> &dyn Task

Borrow the inner task as a trait object.

Source

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.

Source

pub fn with_processors<I, S>(self, processor_names: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Select multiple processors for the root task definition.

Source

pub fn with_processor_resolver( self, processor_resolver: Arc<dyn TaskProcessorResolver>, ) -> Self

Attach the processor resolver used during execution.

Source

pub fn processor_names(&self) -> Vec<&str>

Returns the root task’s selected processor names.

Source§

impl TaskDefinition

Source

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 depth 0, its immediate sub-tasks at depth 1, and so on. This means max_depth = 0 allows only the root task, max_depth = 1 allows 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(()).

Source

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.

Source

pub fn process_task_start( &self, results: &mut TaskResults, ) -> Result<(), GenjaError>

Run aggregate task-start processors for this definition.

Source

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

Source§

fn clone(&self) -> TaskDefinition

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl TaskInfo for TaskDefinition

Source§

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>

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>

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>

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.

Source§

fn processor_names(&self) -> Vec<&str>

Return processor plugin names selected for this task.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.