Skip to main content

SupervisorRuntime

Struct SupervisorRuntime 

Source
pub struct SupervisorRuntime { /* private fields */ }
Expand description

An async runtime for supervising a group of child tasks.

SupervisorRuntime manages a JoinSet of async tasks and applies a RestartStrategy to determine which children to restart when a failure occurs. It handles the concurrency details of spawning, joining, and tracking child tasks.

§Examples

use gust_runtime::prelude::*;

let runtime = SupervisorRuntime::with_strategy(RestartStrategy::OneForAll);
let handle = runtime.spawn_named("worker-1", async {
    // ... child machine logic ...
    Ok(())
});

// Wait for the next child to complete
if let Some(result) = runtime.join_next().await {
    match result {
        Ok(()) => println!("child completed successfully"),
        Err(e) => println!("child failed: {e}"),
    }
}

Implementations§

Source§

impl SupervisorRuntime

Source

pub fn new() -> Self

Creates a new SupervisorRuntime with the default RestartStrategy::OneForOne strategy.

Source

pub fn with_strategy(strategy: RestartStrategy) -> Self

Creates a new SupervisorRuntime with the specified restart strategy.

Source

pub fn spawn_named<F>(&self, id: impl Into<String>, fut: F) -> ChildHandle
where F: Future<Output = Result<(), String>> + Send + 'static,

Spawns a named child task and returns a ChildHandle.

The future fut is spawned onto the Tokio runtime. If the internal task set lock is immediately available the spawn happens synchronously; otherwise a helper task is used to avoid blocking.

The id is stored in the returned ChildHandle for identification purposes.

Source

pub async fn join_next(&self) -> Option<Result<(), String>>

Waits for the next child task to complete and returns its result.

Returns Some(Ok(())) when a child succeeds, Some(Err(..)) when a child fails, or None when all children have completed and no pending spawns remain.

This method will yield cooperatively if there are pending spawns that have not yet been added to the task set.

Source

pub fn strategy(&self) -> RestartStrategy

Returns the RestartStrategy configured for this runtime.

Source

pub fn restart_scope( &self, failed_child_index: usize, child_count: usize, ) -> Range<usize>

Computes the range of child indices that should be restarted when the child at failed_child_index fails.

The returned range depends on the configured RestartStrategy:

  • OneForOne: only the failed child (index..index+1)
  • OneForAll: all children (0..child_count)
  • RestForOne: the failed child and all that follow (index..child_count)

Trait Implementations§

Source§

impl Clone for SupervisorRuntime

Source§

fn clone(&self) -> SupervisorRuntime

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SupervisorRuntime

Source§

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

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

impl Default for SupervisorRuntime

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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.