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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
async fn start(&self) -> Result<Pid, ExitReason> {
// Spawn a registry that will take care of registering 'MySpace'.
let children = [
Registry::new("space-registry")
.with_start(|key| {
let RegistryKey::String(id) = key else {
panic!()
};
MySpace::new(id).start_link(GenServerOptions::new())
})
.with_shutdown(Shutdown::Infinity)
.child_spec(RegistryOptions::new())
.id("space-registry"),
ChildSpec::new("test-registry")
.start(move || async { Ok(Process::spawn(test_registry())) }),
];
// Restart only the terminated child.
Supervisor::with_children(children)
.strategy(SupervisionStrategy::OneForOne)
.start_link(SupervisorOptions::new())
.await
}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 31 32 33 34 35 36 37 38 39 40 41 42
async fn start(&self) -> Result<Pid, ExitReason> {
// Spawn two instances of `MyServer` with their own unique ids.
let children = [
MyServer::new().child_spec().id("server1"),
MyServer::new().child_spec().id("server2"),
];
// Restart only the terminated child.
Supervisor::with_children(children)
.strategy(SupervisionStrategy::OneForOne)
.start_link(SupervisorOptions::new())
.await
}More examples
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
async fn start(&self) -> Result<Pid, ExitReason> {
// Spawn a registry that will take care of registering 'MySpace'.
let children = [
Registry::new("space-registry")
.with_start(|key| {
let RegistryKey::String(id) = key else {
panic!()
};
MySpace::new(id).start_link(GenServerOptions::new())
})
.with_shutdown(Shutdown::Infinity)
.child_spec(RegistryOptions::new())
.id("space-registry"),
ChildSpec::new("test-registry")
.start(move || async { Ok(Process::spawn(test_registry())) }),
];
// Restart only the terminated child.
Supervisor::with_children(children)
.strategy(SupervisionStrategy::OneForOne)
.start_link(SupervisorOptions::new())
.await
}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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
async fn start(&self) -> Result<Pid, ExitReason> {
// Spawn a registry that will take care of registering 'MySpace'.
let children = [
Registry::new("space-registry")
.with_start(|key| {
let RegistryKey::String(id) = key else {
panic!()
};
MySpace::new(id).start_link(GenServerOptions::new())
})
.with_shutdown(Shutdown::Infinity)
.child_spec(RegistryOptions::new())
.id("space-registry"),
ChildSpec::new("test-registry")
.start(move || async { Ok(Process::spawn(test_registry())) }),
];
// Restart only the terminated child.
Supervisor::with_children(children)
.strategy(SupervisionStrategy::OneForOne)
.start_link(SupervisorOptions::new())
.await
}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.