pub struct Supervisor { /* private fields */ }Expand description
A supervisor is a process which supervises other processes, which we refer to as child processes. Supervisors are used to build a hierarchical process structure called a supervision tree. Supervision trees provide fault-tolerance and encapsulate how our applications start and shutdown.
Implementations§
Source§impl Supervisor
impl Supervisor
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new instance of Supervisor with no children.
Sourcepub fn with_children<T: IntoIterator<Item = ChildSpec>>(children: T) -> Self
pub fn with_children<T: IntoIterator<Item = ChildSpec>>(children: T) -> Self
Constructs a new instance of Supervisor with the given children.
Examples found in repository?
30 async fn start(&self) -> Result<Pid, ExitReason> {
31 // Spawn two instances of `MyServer` with their own unique ids.
32 let children = [
33 MyServer::new().child_spec().id("server1"),
34 MyServer::new().child_spec().id("server2"),
35 ];
36
37 // Restart only the terminated child.
38 Supervisor::with_children(children)
39 .strategy(SupervisionStrategy::OneForOne)
40 .start_link(SupervisorOptions::new())
41 .await
42 }More examples
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }Sourcepub fn add_child(self, child: ChildSpec) -> Self
pub fn add_child(self, child: ChildSpec) -> Self
Adds a child to this Supervisor.
Sourcepub fn child_spec(self, options: SupervisorOptions) -> ChildSpec
pub fn child_spec(self, options: SupervisorOptions) -> ChildSpec
Builds a child specification for this Supervisor process.
Sourcepub const fn strategy(self, strategy: SupervisionStrategy) -> Self
pub const fn strategy(self, strategy: SupervisionStrategy) -> Self
Sets the supervision strategy for the Supervisor.
Examples found in repository?
30 async fn start(&self) -> Result<Pid, ExitReason> {
31 // Spawn two instances of `MyServer` with their own unique ids.
32 let children = [
33 MyServer::new().child_spec().id("server1"),
34 MyServer::new().child_spec().id("server2"),
35 ];
36
37 // Restart only the terminated child.
38 Supervisor::with_children(children)
39 .strategy(SupervisionStrategy::OneForOne)
40 .start_link(SupervisorOptions::new())
41 .await
42 }More examples
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }Sourcepub const fn auto_shutdown(self, auto_shutdown: AutoShutdown) -> Self
pub const fn auto_shutdown(self, auto_shutdown: AutoShutdown) -> Self
Sets the behavior to use when a significant process exits.
Sourcepub const fn max_restarts(self, max_restarts: usize) -> Self
pub const fn max_restarts(self, max_restarts: usize) -> Self
Sets the maximum number of restarts allowed in a time frame.
Defaults to 3.
Sourcepub const fn max_duration(self, max_duration: Duration) -> Self
pub const fn max_duration(self, max_duration: Duration) -> Self
Sets the time frame in which max_restarts applies.
Defaults to 5s.
Sourcepub async fn start(self, options: SupervisorOptions) -> Result<Pid, ExitReason>
pub async fn start(self, options: SupervisorOptions) -> Result<Pid, ExitReason>
Creates a supervisor process not apart of a supervision tree.
This will not return until all of the child processes have been started.
Sourcepub async fn start_link(
self,
options: SupervisorOptions,
) -> Result<Pid, ExitReason>
pub async fn start_link( self, options: SupervisorOptions, ) -> Result<Pid, ExitReason>
Creates a supervisor process as part of a supervision tree.
For example, this function ensures that the supervisor is linked to the calling process (its supervisor).
This will not return until all of the child processes have been started.
Examples found in repository?
30 async fn start(&self) -> Result<Pid, ExitReason> {
31 // Spawn two instances of `MyServer` with their own unique ids.
32 let children = [
33 MyServer::new().child_spec().id("server1"),
34 MyServer::new().child_spec().id("server2"),
35 ];
36
37 // Restart only the terminated child.
38 Supervisor::with_children(children)
39 .strategy(SupervisionStrategy::OneForOne)
40 .start_link(SupervisorOptions::new())
41 .await
42 }More examples
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }Sourcepub async fn count_children<T: Into<Dest>>(
supervisor: T,
) -> Result<SupervisorCounts, SupervisorError>
pub async fn count_children<T: Into<Dest>>( supervisor: T, ) -> Result<SupervisorCounts, SupervisorError>
Returns SupervisorCounts containing the counts for each of the different child specifications.
Sourcepub async fn start_child<T: Into<Dest>>(
supervisor: T,
child: ChildSpec,
) -> Result<Option<Pid>, SupervisorError>
pub async fn start_child<T: Into<Dest>>( supervisor: T, child: ChildSpec, ) -> Result<Option<Pid>, SupervisorError>
Adds the child specification to the Supervisor and starts that child.
Sourcepub async fn terminate_child<T: Into<Dest>, I: Into<String>>(
supervisor: T,
child_id: I,
) -> Result<(), SupervisorError>
pub async fn terminate_child<T: Into<Dest>, I: Into<String>>( supervisor: T, child_id: I, ) -> Result<(), SupervisorError>
Terminates the given child identified by child_id.
The process is terminated, if there’s one. The child specification is kept unless the child is temporary.
A non-temporary child process may later be restarted by the Supervisor.
The child process can also be restarted explicitly by calling restart_child. Use delete_child to remove the child specification.
Sourcepub async fn restart_child<T: Into<Dest>, I: Into<String>>(
supervisor: T,
child_id: I,
) -> Result<Option<Pid>, SupervisorError>
pub async fn restart_child<T: Into<Dest>, I: Into<String>>( supervisor: T, child_id: I, ) -> Result<Option<Pid>, SupervisorError>
Restarts a child identified by child_id.
The child specification must exist and the corresponding child process must not be running.
Note that for temporary children, the child specification is automatically deleted when the child terminates, and thus it is not possible to restart such children.
Sourcepub async fn delete_child<T: Into<Dest>, I: Into<String>>(
supervisor: T,
child_id: I,
) -> Result<(), SupervisorError>
pub async fn delete_child<T: Into<Dest>, I: Into<String>>( supervisor: T, child_id: I, ) -> Result<(), SupervisorError>
Deletes the child specification identified by child_id.
The corrosponding child process must not be running, use terminate_child to terminate it if it’s running.
Sourcepub async fn which_children<T: Into<Dest>>(
supervisor: T,
) -> Result<Vec<SupervisorChildInfo>, SupervisorError>
pub async fn which_children<T: Into<Dest>>( supervisor: T, ) -> Result<Vec<SupervisorChildInfo>, SupervisorError>
Returns a list with information about all children of the given Supervisor.
Trait Implementations§
Source§impl Clone for Supervisor
impl Clone for Supervisor
Source§fn clone(&self) -> Supervisor
fn clone(&self) -> Supervisor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for Supervisor
impl Default for Supervisor
Source§impl GenServer for Supervisor
impl GenServer for Supervisor
Source§async fn init(&mut self) -> Result<(), ExitReason>
async fn init(&mut self) -> Result<(), ExitReason>
start_link or start will block until it returns.Source§async fn terminate(&mut self, _reason: ExitReason)
async fn terminate(&mut self, _reason: ExitReason)
Source§async fn handle_cast(
&mut self,
message: Self::Message,
) -> Result<(), ExitReason>
async fn handle_cast( &mut self, message: Self::Message, ) -> Result<(), ExitReason>
cast messages.Source§async fn handle_call(
&mut self,
message: Self::Message,
_from: From,
) -> Result<Option<Self::Message>, ExitReason>
async fn handle_call( &mut self, message: Self::Message, _from: From, ) -> Result<Option<Self::Message>, ExitReason>
call messages. call will block until a reply is received
(unless the call times out or nodes are disconnected). Read moreSource§async fn handle_info(
&mut self,
info: Message<Self::Message>,
) -> Result<(), ExitReason>
async fn handle_info( &mut self, info: Message<Self::Message>, ) -> Result<(), ExitReason>
Source§fn start(
self,
options: GenServerOptions,
) -> impl Future<Output = Result<Pid, ExitReason>> + Send
fn start( self, options: GenServerOptions, ) -> impl Future<Output = Result<Pid, ExitReason>> + Send
Source§fn start_link(
self,
options: GenServerOptions,
) -> impl Future<Output = Result<Pid, ExitReason>> + Send
fn start_link( self, options: GenServerOptions, ) -> impl Future<Output = Result<Pid, ExitReason>> + Send
Source§fn stop<T: Into<Dest>>(
server: T,
reason: ExitReason,
timeout: Option<Duration>,
) -> impl Future<Output = Result<(), ExitReason>>
fn stop<T: Into<Dest>>( server: T, reason: ExitReason, timeout: Option<Duration>, ) -> impl Future<Output = Result<(), ExitReason>>
reason. Read moreSource§fn cast<T: Into<Dests>>(servers: T, message: Self::Message)
fn cast<T: Into<Dests>>(servers: T, message: Self::Message)
servers without waiting for a response. Read more