pub struct TaskAttemptState {
pub status: TaskStatus,
pub attempts: usize,
pub last_error: Option<String>,
}Expand description
Tracks the state of task execution attempts for a specific host and task combination.
This structure maintains comprehensive information about task execution attempts, including
the current task status, the number of attempts that have been made, and any error message
from the most recent failed attempt. It is used by the State structure to track task
execution history and current state for each host/task pair.
§Fields
-
status- The currentTaskStatusindicating whether the task is pending, running, succeeded, pending retry, failed, or skipped. This field provides high-level information about the task execution state. -
attempts- The total number of task execution attempts that have been made for this host/task combination. This counter tracks how many times the task has been attempted and is preserved across status changes, allowing you to track how many attempts were needed before the task succeeded or ultimately failed. -
last_error- An optional error message from the most recent failed task execution attempt. This field isNoneif no error has occurred or if the task is currently running or has succeeded. When present, it contains diagnostic information about why the last task execution attempt failed, which can be useful for troubleshooting and logging.
§Examples
// Create a new task state indicating a successful execution
let state = TaskAttemptState::new(TaskStatus::Succeeded)
.with_attempts(1);
assert_eq!(state.status, TaskStatus::Succeeded);
assert_eq!(state.attempts, 1);
assert_eq!(state.last_error, None);
// Create a task state with a failure and error message
let failed_state = TaskAttemptState::new(
TaskStatus::Failed(TaskFailureKind::ParseFailed)
)
.with_attempts(2)
.with_last_error("failed to parse command output");
assert_eq!(failed_state.attempts, 2);
assert_eq!(
failed_state.last_error,
Some("failed to parse command output".to_string())
);Fields§
§status: TaskStatus§attempts: usize§last_error: Option<String>Implementations§
Source§impl TaskAttemptState
impl TaskAttemptState
Sourcepub fn new(status: TaskStatus) -> Self
pub fn new(status: TaskStatus) -> Self
Creates a new TaskAttemptState with the specified task status.
This constructor initializes a task attempt state with the given status, setting the attempt counter to 0 and leaving the error field empty. This is typically used when first recording a task execution or when transitioning to a new task state.
§Parameters
status- The initialTaskStatusfor this task attempt state. This indicates whether the task is pending, running, succeeded, pending retry, failed, or skipped.
§Returns
Returns a new TaskAttemptState instance with the specified status,
zero attempts, and no error message.
§Examples
// Create a state for a new task execution
let state = TaskAttemptState::new(TaskStatus::Running);
assert_eq!(state.status, TaskStatus::Running);
assert_eq!(state.attempts, 0);
assert_eq!(state.last_error, None);Sourcepub fn with_attempts(self, attempts: usize) -> Self
pub fn with_attempts(self, attempts: usize) -> Self
Sets the number of task execution attempts for this task state.
This builder method allows you to specify how many task execution attempts have been made for a particular host/task combination. The attempt count is typically used to track retry behavior and can be helpful for implementing retry strategies or determining when to give up on a task execution.
This method consumes self and returns a new instance with the updated attempt
count, following the builder pattern for convenient method chaining.
§Parameters
attempts- The number of task execution attempts to record. This should represent the total number of attempts made, not just the increment. A value of 0 indicates no attempts have been made yet, while higher values indicate multiple retry attempts.
§Returns
Returns self with the attempts field updated to the specified value, allowing
for method chaining with other builder methods like with_last_error.
§Examples
// Create a task state with 2 attempts
let state = TaskAttemptState::new(TaskStatus::Running)
.with_attempts(2);
assert_eq!(state.attempts, 2);// Chain multiple builder methods together
let state = TaskAttemptState::new(
TaskStatus::Failed(TaskFailureKind::ParseFailed)
)
.with_attempts(3)
.with_last_error("failed to parse command output");
assert_eq!(state.attempts, 3);
assert_eq!(state.last_error, Some("failed to parse command output".to_string()));Sourcepub fn with_last_error(self, last_error: impl Into<String>) -> Self
pub fn with_last_error(self, last_error: impl Into<String>) -> Self
Sets the error message from the most recent failed task execution attempt.
This builder method allows you to record diagnostic information about why a task execution attempt failed. The error message is typically set when marking a task as retry pending or failed, and can be used for logging, debugging, or displaying error information to users.
This method consumes self and returns a new instance with the error message
set, following the builder pattern for convenient method chaining.
§Parameters
last_error- The error message to record. Can be any type that converts into aString, such as&str,String, error types that implementDisplay, or other string-like types. The error message should provide meaningful diagnostic information about what went wrong during the task execution attempt.
§Returns
Returns self with the last_error field set to Some(error_message), allowing
for method chaining with other builder methods like with_attempts.
§Examples
// Create a task state with an error message
let state = TaskAttemptState::new(
TaskStatus::Failed(TaskFailureKind::ParseFailed)
)
.with_last_error("failed to parse show version output");
assert_eq!(
state.last_error,
Some("failed to parse show version output".to_string())
);// Chain multiple builder methods together
let state = TaskAttemptState::new(TaskStatus::RetryPending)
.with_attempts(1)
.with_last_error("command execution timed out");
assert_eq!(state.attempts, 1);
assert_eq!(
state.last_error,
Some("command execution timed out".to_string())
);// Use with String type
let error_msg = String::from("validation failed: invalid interface name");
let state = TaskAttemptState::new(
TaskStatus::Failed(TaskFailureKind::ValidationFailed)
)
.with_last_error(error_msg);
assert_eq!(state.last_error, Some("validation failed: invalid interface name".to_string()));Trait Implementations§
Source§impl Clone for TaskAttemptState
impl Clone for TaskAttemptState
Source§fn clone(&self) -> TaskAttemptState
fn clone(&self) -> TaskAttemptState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TaskAttemptState
impl Debug for TaskAttemptState
impl Eq for TaskAttemptState
Source§impl PartialEq for TaskAttemptState
impl PartialEq for TaskAttemptState
Source§fn eq(&self, other: &TaskAttemptState) -> bool
fn eq(&self, other: &TaskAttemptState) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for TaskAttemptState
Auto Trait Implementations§
impl Freeze for TaskAttemptState
impl RefUnwindSafe for TaskAttemptState
impl Send for TaskAttemptState
impl Sync for TaskAttemptState
impl Unpin for TaskAttemptState
impl UnsafeUnpin for TaskAttemptState
impl UnwindSafe for TaskAttemptState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.