pub struct ChildSpec { /* private fields */ }Expand description
The child specification describes how the supervisor starts, shuts down, and restarts child processes.
Implementations§
Source§impl ChildSpec
impl ChildSpec
Sourcepub fn new<T: Into<String>>(id: T) -> Self
pub fn new<T: Into<String>>(id: T) -> Self
Constructs a new instance of ChildSpec with the given id.
Examples found in repository?
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 id<T: Into<String>>(self, id: T) -> Self
pub fn id<T: Into<String>>(self, id: T) -> Self
Sets a new id for this ChildSpec.
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 start<T, F>(self, start: T) -> Self
pub fn start<T, F>(self, start: T) -> Self
The method invoked to start the child process.
Must return a future that resolves to Result<Pid, ExitReason>.
Examples found in repository?
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 restart(self, restart: Restart) -> Self
pub const fn restart(self, restart: Restart) -> Self
Defines when a terminated child process should be restarted.
Defaults to Restart::Permanent.
Sourcepub fn shutdown<T: Into<Shutdown>>(self, shutdown: T) -> Self
pub fn shutdown<T: Into<Shutdown>>(self, shutdown: T) -> Self
Defines how a process should be terminated, specifically how long to wait before forcefully killing it.
Defaults to 5s if the type is worker or infinity if the type is supervisor.
Sourcepub const fn child_type(self, child_type: ChildType) -> Self
pub const fn child_type(self, child_type: ChildType) -> Self
Specifies the type of child process.
Sourcepub const fn significant(self, significant: bool) -> Self
pub const fn significant(self, significant: bool) -> Self
A boolean indicating if the child process should be considered significant with regard to automatic shutdown.
Only transient and temporary child processes can be marked as significant.
Defaults to false.