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
impl SupervisorRuntime
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new SupervisorRuntime with the default
RestartStrategy::OneForOne strategy.
Sourcepub fn with_strategy(strategy: RestartStrategy) -> Self
pub fn with_strategy(strategy: RestartStrategy) -> Self
Creates a new SupervisorRuntime with the specified restart strategy.
Sourcepub fn spawn_named<F>(&self, id: impl Into<String>, fut: F) -> ChildHandle
pub fn spawn_named<F>(&self, id: impl Into<String>, fut: F) -> ChildHandle
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.
Sourcepub async fn join_next(&self) -> Option<Result<(), String>>
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.
Sourcepub fn strategy(&self) -> RestartStrategy
pub fn strategy(&self) -> RestartStrategy
Returns the RestartStrategy configured for this runtime.
Sourcepub fn restart_scope(
&self,
failed_child_index: usize,
child_count: usize,
) -> Range<usize>
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
impl Clone for SupervisorRuntime
Source§fn clone(&self) -> SupervisorRuntime
fn clone(&self) -> SupervisorRuntime
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more