Skip to main content

TaskAttemptState

Struct TaskAttemptState 

Source
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 current TaskStatus indicating 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 is None if 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

Source

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 initial TaskStatus for 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);
Source

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()));
Source

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 a String, such as &str, String, error types that implement Display, 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

Source§

fn clone(&self) -> TaskAttemptState

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 Debug for TaskAttemptState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for TaskAttemptState

Source§

impl PartialEq for TaskAttemptState

Source§

fn eq(&self, other: &TaskAttemptState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for TaskAttemptState

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.