pub struct State { /* private fields */ }Expand description
Per-host execution state for the current Genja instance.
This structure maintains thread-safe state tracking for hosts, connections, and tasks
within a Genja runtime. It uses concurrent hash maps (DashMap) to allow safe
concurrent access from multiple threads without requiring external synchronization.
§State Categories
The state is organized into three primary categories:
-
Host Status - Tracks whether hosts are in scope or have failed, allowing the runtime to exclude failed hosts from operations until they are explicitly restored.
-
Connection State - Records connection attempt history for each host/plugin pair, including the current connection status, number of attempts, and any error messages from failed attempts.
-
Task State - Tracks task execution state for each host/task pair, including execution status, attempt counts, and error information.
§Thread Safety
All state maps use DashMap internally, which provides lock-free concurrent access
for most operations. This allows multiple threads to safely read and update state
without explicit locking.
§Examples
let state = State::new();
// Track host failures
state.mark_failed("router1");
assert!(!state.is_in_scope("router1"));
// Track connection attempts
state.begin_connection_attempt("router2", "ssh");
state.mark_connection_connected("router2", "ssh");
// Restore failed hosts
state.mark_in_scope("router1");
assert!(state.is_in_scope("router1"));Implementations§
Source§impl State
impl State
Sourcepub fn mark_failed<K>(&self, name: K)
pub fn mark_failed<K>(&self, name: K)
Marks a host as failed and removes it from the active scope.
When a host is marked as failed, it will be excluded from host views and
operations until it is explicitly restored using mark_in_scope
or mark_in_scope_key.
§Parameters
name- The hostname to mark as failed. Can be any type that converts into aNatString, such as&str,String, orNatString.
§Examples
let state = State::new();
state.mark_failed("router1");
assert!(!state.is_in_scope("router1"));Sourcepub fn mark_in_scope<K>(&self, name: K)
pub fn mark_in_scope<K>(&self, name: K)
Marks a host as back in scope and restores it to the active host view.
When a host is marked as in scope, it will be included in host views and
operations. This is typically used to restore a host that was previously
marked as failed using mark_failed.
§Parameters
name- The hostname to mark as in scope. Can be any type that converts into aNatString, such as&str,String, orNatString.
§Examples
let state = State::new();
state.mark_failed("router1");
state.mark_in_scope("router1");
assert!(state.is_in_scope("router1"));Sourcepub fn mark_in_scope_key(&self, key: &NatString)
pub fn mark_in_scope_key(&self, key: &NatString)
Marks a host as back in scope using an existing NatString key.
This is a more efficient variant of mark_in_scope when you
already have a NatString reference, as it avoids an additional conversion.
§Parameters
key- A reference to theNatStringkey representing the hostname to mark as in scope.
§Examples
let state = State::new();
let host = NatString::from("router1");
state.mark_failed(host.clone());
state.mark_in_scope_key(&host);
assert!(state.is_in_scope_key(&host));Sourcepub fn is_in_scope<K>(&self, name: K) -> bool
pub fn is_in_scope<K>(&self, name: K) -> bool
Checks if a host is currently in scope and available for operations.
A host is considered in scope unless it has been explicitly marked as failed
using mark_failed. Hosts that have never been tracked
are considered in scope by default.
§Parameters
name- The hostname to check. Can be any type that converts into aNatString, such as&str,String, orNatString.
§Returns
Returns true if the host is in scope (either explicitly marked as in scope or
never tracked), false if the host has been marked as failed.
§Examples
let state = State::new();
// Untracked hosts are in scope by default
assert!(state.is_in_scope("router1"));
// Failed hosts are not in scope
state.mark_failed("router1");
assert!(!state.is_in_scope("router1"));
// Restored hosts are back in scope
state.mark_in_scope("router1");
assert!(state.is_in_scope("router1"));Sourcepub fn host_status<K>(&self, name: K) -> Option<HostStatus>
pub fn host_status<K>(&self, name: K) -> Option<HostStatus>
Returns the tracked host status for a host, if it has been explicitly set.
This method retrieves the current status of a host if it has been tracked
(i.e., marked as failed or explicitly set as in scope). If the host has never
been tracked, this method returns None.
§Parameters
name- The hostname to query. Can be any type that converts into aNatString, such as&str,String, orNatString.
§Returns
Returns Some(HostStatus) if the host has been explicitly tracked, or None
if the host has never been marked with a status. Note that returning None
does not mean the host is out of scope; untracked hosts are considered in
scope by default (see is_in_scope).
§Examples
let state = State::new();
// Untracked hosts return None
assert_eq!(state.host_status("router1"), None);
// Tracked hosts return their status
state.mark_failed("router1");
assert_eq!(state.host_status("router1"), Some(HostStatus::Failed));
state.mark_in_scope("router1");
assert_eq!(state.host_status("router1"), Some(HostStatus::InScope));Sourcepub fn host_status_key(&self, key: &NatString) -> Option<HostStatus>
pub fn host_status_key(&self, key: &NatString) -> Option<HostStatus>
Returns the tracked host status for a host using an existing NatString key.
This is a more efficient variant of host_status when you
already have a NatString reference, as it avoids an additional conversion.
§Parameters
key- A reference to theNatStringkey representing the hostname to query.
§Returns
Returns Some(HostStatus) if the host has been explicitly tracked, or None
if the host has never been marked with a status. Note that returning None
does not mean the host is out of scope; untracked hosts are considered in
scope by default (see is_in_scope).
§Examples
let state = State::new();
let host = NatString::from("router1");
// Untracked hosts return None
assert_eq!(state.host_status_key(&host), None);
// Tracked hosts return their status
state.mark_failed(host.clone());
assert_eq!(state.host_status_key(&host), Some(HostStatus::Failed));
state.mark_in_scope_key(&host);
assert_eq!(state.host_status_key(&host), Some(HostStatus::InScope));Sourcepub fn is_in_scope_key(&self, key: &NatString) -> bool
pub fn is_in_scope_key(&self, key: &NatString) -> bool
Checks if a host is currently in scope using an existing NatString key.
This is a more efficient variant of is_in_scope when you
already have a NatString reference, as it avoids an additional conversion.
A host is considered in scope unless it has been explicitly marked as failed
using mark_failed. Hosts that have never been tracked
are considered in scope by default.
§Parameters
key- A reference to theNatStringkey representing the hostname to check.
§Returns
Returns true if the host is in scope (either explicitly marked as in scope or
never tracked), false if the host has been marked as failed.
§Examples
let state = State::new();
let host = NatString::from("router1");
// Untracked hosts are in scope by default
assert!(state.is_in_scope_key(&host));
// Failed hosts are not in scope
state.mark_failed(host.clone());
assert!(!state.is_in_scope_key(&host));
// Restored hosts are back in scope
state.mark_in_scope_key(&host);
assert!(state.is_in_scope_key(&host));Sourcepub fn set_connection_state(
&self,
host: impl Into<String>,
plugin_name: impl Into<String>,
state: ConnectionAttemptState,
)
pub fn set_connection_state( &self, host: impl Into<String>, plugin_name: impl Into<String>, state: ConnectionAttemptState, )
Sets the connection attempt state for a specific host and plugin combination.
This method records the current state of a connection attempt, including the
connection status, number of attempts made, and any error information. The state
is stored using a ConnectionKey composed of the host and plugin name.
If the connection state indicates a failure, a warning will be logged with details about the failure kind and any associated error message.
§Parameters
-
host- The hostname for which to set the connection state. Can be any type that converts into aString, such as&str,String, or other string-like types. -
plugin_name- The name of the connection plugin being used. Can be any type that converts into aString, such as&str,String, or other string-like types. -
state- TheConnectionAttemptStateto record, containing the connection status, attempt count, and optional error information.
§Examples
let state = State::new();
// Record a successful connection
let connection_state = ConnectionAttemptState::new(ConnectionStatus::Connected)
.with_attempts(1);
state.set_connection_state("router1", "ssh", connection_state);
// Verify the state was recorded
assert_eq!(
state.connection_state("router1", "ssh").map(|s| s.status),
Some(ConnectionStatus::Connected)
);Sourcepub fn set_connection_state_key(
&self,
key: ConnectionKey,
state: ConnectionAttemptState,
)
pub fn set_connection_state_key( &self, key: ConnectionKey, state: ConnectionAttemptState, )
Sets the connection attempt state using an existing ConnectionKey.
This is a more efficient variant of set_connection_state
when you already have a ConnectionKey, as it avoids constructing a new key from
separate host and plugin name components.
If the connection state indicates a failure, a warning will be logged with details about the failure kind and any associated error message.
§Parameters
-
key- TheConnectionKeyidentifying the host and plugin combination for which to set the connection state. -
state- TheConnectionAttemptStateto record, containing the connection status, attempt count, and optional error information.
§Examples
let state = State::new();
let key = ConnectionKey::new("router1", "ssh");
// Record a successful connection
let connection_state = ConnectionAttemptState::new(ConnectionStatus::Connected)
.with_attempts(1);
state.set_connection_state_key(key.clone(), connection_state);
// Verify the state was recorded
assert_eq!(
state.connection_state_key(&key).map(|s| s.status),
Some(ConnectionStatus::Connected)
);Sourcepub fn connection_state(
&self,
host: &str,
plugin_name: &str,
) -> Option<ConnectionAttemptState>
pub fn connection_state( &self, host: &str, plugin_name: &str, ) -> Option<ConnectionAttemptState>
Retrieves the current connection attempt state for a specific host and plugin combination.
This method looks up the connection state using the provided host and plugin name, returning the current state if it exists. The state includes information about the connection status, number of attempts made, and any error from the last failed attempt.
§Parameters
-
host- The hostname for which to retrieve the connection state. -
plugin_name- The name of the connection plugin being used.
§Returns
Returns Some(ConnectionAttemptState) if a connection state has been recorded for
the given host and plugin combination, or None if no connection attempts have been
tracked for this combination.
§Examples
let state = State::new();
// No state recorded yet
assert_eq!(state.connection_state("router1", "ssh"), None);
// After recording a connection attempt
state.begin_connection_attempt("router1", "ssh");
let connection_state = state.connection_state("router1", "ssh");
assert!(connection_state.is_some());
assert_eq!(connection_state.unwrap().status, ConnectionStatus::Connecting);Sourcepub fn connection_state_key(
&self,
key: &ConnectionKey,
) -> Option<ConnectionAttemptState>
pub fn connection_state_key( &self, key: &ConnectionKey, ) -> Option<ConnectionAttemptState>
Retrieves the current connection attempt state using an existing ConnectionKey.
This is a more efficient variant of connection_state when you
already have a ConnectionKey, as it avoids constructing a new key from separate host
and plugin name components.
§Parameters
key- A reference to theConnectionKeyidentifying the host and plugin combination for which to retrieve the connection state.
§Returns
Returns Some(ConnectionAttemptState) if a connection state has been recorded for
the given key, or None if no connection attempts have been tracked for this
host and plugin combination.
§Examples
let state = State::new();
let key = ConnectionKey::new("router1", "ssh");
// No state recorded yet
assert_eq!(state.connection_state_key(&key), None);
// After recording a connection attempt
state.begin_connection_attempt_key(key.clone());
let connection_state = state.connection_state_key(&key);
assert!(connection_state.is_some());
assert_eq!(connection_state.unwrap().status, ConnectionStatus::Connecting);Sourcepub fn begin_connection_attempt(
&self,
host: impl Into<String>,
plugin_name: impl Into<String>,
)
pub fn begin_connection_attempt( &self, host: impl Into<String>, plugin_name: impl Into<String>, )
Records the start of a connection attempt and increments the attempt counter.
This method marks the beginning of a new connection attempt for a specific host and
plugin combination. It automatically increments the attempt counter, tracking how many
times a connection has been attempted for this host/plugin pair. The connection status
is set to ConnectionStatus::Connecting.
If this is the first connection attempt for the given host and plugin, the attempt counter starts at 1. For subsequent attempts, the counter is incremented from its previous value.
§Parameters
-
host- The hostname for which to record the connection attempt. Can be any type that converts into aString, such as&str,String, or other string-like types. -
plugin_name- The name of the connection plugin being used for the attempt. Can be any type that converts into aString, such as&str,String, or other string-like types.
§Examples
let state = State::new();
// First connection attempt
state.begin_connection_attempt("router1", "ssh");
let connection_state = state.connection_state("router1", "ssh").unwrap();
assert_eq!(connection_state.status, ConnectionStatus::Connecting);
assert_eq!(connection_state.attempts, 1);
// Second connection attempt
state.begin_connection_attempt("router1", "ssh");
let connection_state = state.connection_state("router1", "ssh").unwrap();
assert_eq!(connection_state.attempts, 2);Sourcepub fn begin_connection_attempt_key(&self, key: ConnectionKey)
pub fn begin_connection_attempt_key(&self, key: ConnectionKey)
Records the start of a connection attempt using an existing ConnectionKey and increments the attempt counter.
This is a more efficient variant of begin_connection_attempt
when you already have a ConnectionKey, as it avoids constructing a new key from separate
host and plugin name components.
This method marks the beginning of a new connection attempt for the specified connection key.
It automatically increments the attempt counter, tracking how many times a connection has been
attempted for this host/plugin pair. The connection status is set to ConnectionStatus::Connecting.
If this is the first connection attempt for the given key, the attempt counter starts at 1. For subsequent attempts, the counter is incremented from its previous value.
§Parameters
key- TheConnectionKeyidentifying the host and plugin combination for which to record the connection attempt.
§Examples
let state = State::new();
let key = ConnectionKey::new("router1", "ssh");
// First connection attempt
state.begin_connection_attempt_key(key.clone());
let connection_state = state.connection_state_key(&key).unwrap();
assert_eq!(connection_state.status, ConnectionStatus::Connecting);
assert_eq!(connection_state.attempts, 1);
// Second connection attempt
state.begin_connection_attempt_key(key.clone());
let connection_state = state.connection_state_key(&key).unwrap();
assert_eq!(connection_state.attempts, 2);Sourcepub fn mark_connection_connected(
&self,
host: impl Into<String>,
plugin_name: impl Into<String>,
)
pub fn mark_connection_connected( &self, host: impl Into<String>, plugin_name: impl Into<String>, )
Marks a connection as successfully established while preserving the attempt count.
This method updates the connection state to ConnectionStatus::Connected, indicating
that a connection has been successfully established for the specified host and plugin
combination. The attempt counter is preserved from any previous connection attempts,
allowing you to track how many attempts were needed before the connection succeeded.
If no previous connection attempts were recorded, the attempt count will be set to 0.
§Parameters
-
host- The hostname for which to mark the connection as connected. Can be any type that converts into aString, such as&str,String, or other string-like types. -
plugin_name- The name of the connection plugin that successfully established the connection. Can be any type that converts into aString, such as&str,String, or other string-like types.
§Examples
let state = State::new();
// Record connection attempts and then mark as connected
state.begin_connection_attempt("router1", "ssh");
state.begin_connection_attempt("router1", "ssh");
state.mark_connection_connected("router1", "ssh");
// Verify the connection is marked as connected with preserved attempt count
let connection_state = state.connection_state("router1", "ssh").unwrap();
assert_eq!(connection_state.status, ConnectionStatus::Connected);
assert_eq!(connection_state.attempts, 2);Sourcepub fn mark_connection_connected_key(&self, key: ConnectionKey)
pub fn mark_connection_connected_key(&self, key: ConnectionKey)
Marks a connection as successfully established using an existing ConnectionKey while preserving the attempt count.
This is a more efficient variant of mark_connection_connected
when you already have a ConnectionKey, as it avoids constructing a new key from separate
host and plugin name components.
This method updates the connection state to ConnectionStatus::Connected, indicating
that a connection has been successfully established. The attempt counter is preserved from
any previous connection attempts, allowing you to track how many attempts were needed
before the connection succeeded.
If no previous connection attempts were recorded, the attempt count will be set to 0.
§Parameters
key- TheConnectionKeyidentifying the host and plugin combination for which to mark the connection as connected.
§Examples
let state = State::new();
let key = ConnectionKey::new("router1", "ssh");
// Record connection attempts and then mark as connected
state.begin_connection_attempt_key(key.clone());
state.begin_connection_attempt_key(key.clone());
state.mark_connection_connected_key(key.clone());
// Verify the connection is marked as connected with preserved attempt count
let connection_state = state.connection_state_key(&key).unwrap();
assert_eq!(connection_state.status, ConnectionStatus::Connected);
assert_eq!(connection_state.attempts, 2);Sourcepub fn mark_connection_retry_pending(
&self,
host: impl Into<String>,
plugin_name: impl Into<String>,
last_error: impl Into<String>,
)
pub fn mark_connection_retry_pending( &self, host: impl Into<String>, plugin_name: impl Into<String>, last_error: impl Into<String>, )
Marks a connection as pending retry while preserving the attempt count and recording the error.
This method updates the connection state to ConnectionStatus::RetryPending, indicating
that a connection attempt has failed but will be retried. The attempt counter is preserved
from any previous connection attempts, and the provided error message is stored for
diagnostic purposes.
If no previous connection attempts were recorded, the attempt count will be set to 0.
§Parameters
-
host- The hostname for which to mark the connection as pending retry. Can be any type that converts into aString, such as&str,String, or other string-like types. -
plugin_name- The name of the connection plugin for which the retry is pending. Can be any type that converts into aString, such as&str,String, or other string-like types. -
last_error- The error message from the failed connection attempt that triggered the retry. Can be any type that converts into aString, such as&str,String, or other string-like types.
§Examples
let state = State::new();
// Record a connection attempt and mark as pending retry
state.begin_connection_attempt("router1", "ssh");
state.mark_connection_retry_pending("router1", "ssh", "connection timed out");
// Verify the connection is marked as retry pending with error
let connection_state = state.connection_state("router1", "ssh").unwrap();
assert_eq!(connection_state.status, ConnectionStatus::RetryPending);
assert_eq!(connection_state.attempts, 1);
assert_eq!(connection_state.last_error, Some("connection timed out".to_string()));Sourcepub fn mark_connection_retry_pending_key(
&self,
key: ConnectionKey,
last_error: impl Into<String>,
)
pub fn mark_connection_retry_pending_key( &self, key: ConnectionKey, last_error: impl Into<String>, )
Marks a connection as pending retry using an existing ConnectionKey while preserving the attempt count and recording the error.
This is a more efficient variant of mark_connection_retry_pending
when you already have a ConnectionKey, as it avoids constructing a new key from separate
host and plugin name components.
This method updates the connection state to ConnectionStatus::RetryPending, indicating
that a connection attempt has failed but will be retried. The attempt counter is preserved
from any previous connection attempts, and the provided error message is stored for
diagnostic purposes.
If no previous connection attempts were recorded, the attempt count will be set to 0.
§Parameters
-
key- TheConnectionKeyidentifying the host and plugin combination for which to mark the connection as pending retry. -
last_error- The error message from the failed connection attempt that triggered the retry. Can be any type that converts into aString, such as&str,String, or other string-like types.
§Examples
let state = State::new();
let key = ConnectionKey::new("router1", "ssh");
// Record a connection attempt and mark as pending retry
state.begin_connection_attempt_key(key.clone());
state.mark_connection_retry_pending_key(key.clone(), "connection timed out");
// Verify the connection is marked as retry pending with error
let connection_state = state.connection_state_key(&key).unwrap();
assert_eq!(connection_state.status, ConnectionStatus::RetryPending);
assert_eq!(connection_state.attempts, 1);
assert_eq!(connection_state.last_error, Some("connection timed out".to_string()));Sourcepub fn mark_connection_failed(
&self,
host: impl Into<String>,
plugin_name: impl Into<String>,
kind: ConnectionFailureKind,
last_error: impl Into<String>,
)
pub fn mark_connection_failed( &self, host: impl Into<String>, plugin_name: impl Into<String>, kind: ConnectionFailureKind, last_error: impl Into<String>, )
Marks a connection as terminally failed while preserving the attempt count and recording the error.
This method updates the connection state to ConnectionStatus::Failed with the specified
failure kind, indicating that a connection attempt has failed and will not be retried. The
attempt counter is preserved from any previous connection attempts, and the provided error
message is stored for diagnostic purposes.
If no previous connection attempts were recorded, the attempt count will be set to 0.
§Parameters
-
host- The hostname for which to mark the connection as failed. Can be any type that converts into aString, such as&str,String, or other string-like types. -
plugin_name- The name of the connection plugin for which the connection failed. Can be any type that converts into aString, such as&str,String, or other string-like types. -
kind- TheConnectionFailureKindclassifying the type of failure (e.g., timeout, authentication failure, DNS error). -
last_error- The error message from the failed connection attempt. Can be any type that converts into aString, such as&str,String, or other string-like types.
§Examples
let state = State::new();
// Record connection attempts and mark as failed
state.begin_connection_attempt("router1", "ssh");
state.begin_connection_attempt("router1", "ssh");
state.mark_connection_failed(
"router1",
"ssh",
ConnectionFailureKind::Timeout,
"connection timed out after 30 seconds"
);
// Verify the connection is marked as failed with error details
let connection_state = state.connection_state("router1", "ssh").unwrap();
assert_eq!(connection_state.status, ConnectionStatus::Failed(ConnectionFailureKind::Timeout));
assert_eq!(connection_state.attempts, 2);
assert_eq!(connection_state.last_error, Some("connection timed out after 30 seconds".to_string()));Sourcepub fn mark_connection_failed_key(
&self,
key: ConnectionKey,
kind: ConnectionFailureKind,
last_error: impl Into<String>,
)
pub fn mark_connection_failed_key( &self, key: ConnectionKey, kind: ConnectionFailureKind, last_error: impl Into<String>, )
Marks a connection as terminally failed using an existing ConnectionKey while preserving the attempt count and recording the error.
This is a more efficient variant of mark_connection_failed
when you already have a ConnectionKey, as it avoids constructing a new key from separate
host and plugin name components.
This method updates the connection state to ConnectionStatus::Failed with the specified
failure kind, indicating that a connection attempt has failed and will not be retried. The
attempt counter is preserved from any previous connection attempts, and the provided error
message is stored for diagnostic purposes.
If no previous connection attempts were recorded, the attempt count will be set to 0.
§Parameters
-
key- TheConnectionKeyidentifying the host and plugin combination for which to mark the connection as failed. -
kind- TheConnectionFailureKindclassifying the type of failure (e.g., timeout, authentication failure, DNS error). -
last_error- The error message from the failed connection attempt. Can be any type that converts into aString, such as&str,String, or other string-like types.
§Examples
let state = State::new();
let key = ConnectionKey::new("router1", "ssh");
// Record connection attempts and mark as failed
state.begin_connection_attempt_key(key.clone());
state.begin_connection_attempt_key(key.clone());
state.mark_connection_failed_key(
key.clone(),
ConnectionFailureKind::Timeout,
"connection timed out after 30 seconds"
);
// Verify the connection is marked as failed with error details
let connection_state = state.connection_state_key(&key).unwrap();
assert_eq!(connection_state.status, ConnectionStatus::Failed(ConnectionFailureKind::Timeout));
assert_eq!(connection_state.attempts, 2);
assert_eq!(connection_state.last_error, Some("connection timed out after 30 seconds".to_string()));Sourcepub fn set_task_state(
&self,
host: impl Into<String>,
task_name: impl Into<String>,
state: TaskAttemptState,
)
pub fn set_task_state( &self, host: impl Into<String>, task_name: impl Into<String>, state: TaskAttemptState, )
Sets the task execution state for a specific host and task combination.
This method records the current state of a task execution, including the
task status, number of attempts made, and any error information. The state
is stored using a TaskExecutionKey composed of the host and task name.
If the task state indicates a failure, a warning will be logged with details about the failure kind and any associated error message.
§Parameters
-
host- The hostname for which to set the task state. Can be any type that converts into aString, such as&str,String, or other string-like types. -
task_name- The name of the task being executed. Can be any type that converts into aString, such as&str,String, or other string-like types. -
state- TheTaskAttemptStateto record, containing the task status, attempt count, and optional error information.
§Examples
let state = State::new();
// Record a successful task execution
let task_state = TaskAttemptState::new(TaskStatus::Succeeded)
.with_attempts(1);
state.set_task_state("router1", "show_version", task_state);
// Verify the state was recorded
assert_eq!(
state.task_state("router1", "show_version").map(|s| s.status),
Some(TaskStatus::Succeeded)
);Sourcepub fn set_task_state_key(&self, key: TaskExecutionKey, state: TaskAttemptState)
pub fn set_task_state_key(&self, key: TaskExecutionKey, state: TaskAttemptState)
Sets the task execution state using an existing TaskExecutionKey.
This is a more efficient variant of set_task_state
when you already have a TaskExecutionKey, as it avoids constructing a new key from
separate host and task name components.
If the task state indicates a failure, a warning will be logged with details about the failure kind and any associated error message.
§Parameters
-
key- TheTaskExecutionKeyidentifying the host and task combination for which to set the task state. -
state- TheTaskAttemptStateto record, containing the task status, attempt count, and optional error information.
§Examples
let state = State::new();
let key = TaskExecutionKey::new("router1", "show_version");
// Record a successful task execution
let task_state = TaskAttemptState::new(TaskStatus::Succeeded)
.with_attempts(1);
state.set_task_state_key(key.clone(), task_state);
// Verify the state was recorded
assert_eq!(
state.task_state_key(&key).map(|s| s.status),
Some(TaskStatus::Succeeded)
);Sourcepub fn task_state(
&self,
host: &str,
task_name: &str,
) -> Option<TaskAttemptState>
pub fn task_state( &self, host: &str, task_name: &str, ) -> Option<TaskAttemptState>
Retrieves the current task execution state for a specific host and task combination.
This method looks up the task state using the provided host and task name, returning the current state if it exists. The state includes information about the task status, number of attempts made, and any error from the last failed attempt.
§Parameters
-
host- The hostname for which to retrieve the task state. -
task_name- The name of the task being queried.
§Returns
Returns Some(TaskAttemptState) if a task state has been recorded for
the given host and task combination, or None if no task execution has been
tracked for this combination.
§Examples
let state = State::new();
// No state recorded yet
assert_eq!(state.task_state("router1", "show_version"), None);
// After recording a task execution
let task_state = TaskAttemptState::new(TaskStatus::Running).with_attempts(1);
state.set_task_state("router1", "show_version", task_state.clone());
assert_eq!(state.task_state("router1", "show_version"), Some(task_state));Sourcepub fn task_state_key(&self, key: &TaskExecutionKey) -> Option<TaskAttemptState>
pub fn task_state_key(&self, key: &TaskExecutionKey) -> Option<TaskAttemptState>
Retrieves the current task execution state using an existing TaskExecutionKey.
This is a more efficient variant of task_state when you
already have a TaskExecutionKey, as it avoids constructing a new key from separate
host and task name components.
§Parameters
key- A reference to theTaskExecutionKeyidentifying the host and task combination for which to retrieve the task state.
§Returns
Returns Some(TaskAttemptState) if a task state has been recorded for
the given key, or None if no task execution has been tracked for this
host and task combination.
§Examples
let state = State::new();
let key = TaskExecutionKey::new("router1", "show_version");
// No state recorded yet
assert_eq!(state.task_state_key(&key), None);
// After recording a task execution
let task_state = TaskAttemptState::new(TaskStatus::Running).with_attempts(1);
state.set_task_state_key(key.clone(), task_state.clone());
assert_eq!(state.task_state_key(&key), Some(task_state));