agner_sup/
mixed.rs

1//! Mixed Supervisor
2//! =====
3
4mod child_id;
5mod child_spec;
6mod restart_intensity;
7mod restart_strategy;
8mod sup_spec;
9mod supervisor;
10
11use agner_actors::{ActorID, Exit, System};
12use agner_utils::result_err_flatten::ResultErrFlattenIn;
13pub use child_id::ChildID;
14pub use child_spec::{BoxedMixedChildSpec, ChildType, FlatMixedChildSpec, MixedChildSpec};
15pub use restart_intensity::RestartIntensity;
16pub use restart_strategy::{AllForOne, OneForOne, RestForOne, RestartStrategy};
17pub use sup_spec::SupSpec;
18
19pub mod plumbing {
20    pub use super::restart_intensity::{DurationToInstant, ElapsedSince, RestartStats};
21    pub use super::restart_strategy::{Action, Decider};
22}
23
24pub use supervisor::run;
25use tokio::sync::oneshot;
26
27use self::supervisor::SupervisorError;
28
29pub async fn start_child<ID, CS>(
30    system: &System,
31    sup: ActorID,
32    child_spec: CS,
33) -> Result<ActorID, SupervisorError>
34where
35    ID: ChildID,
36    CS: Into<BoxedMixedChildSpec<ID>>,
37{
38    let (tx, rx) = oneshot::channel();
39    let message = supervisor::Message::StartChild(child_spec.into(), tx);
40    system.send(sup, message).await;
41    rx.await.err_flatten_in()
42}
43
44pub async fn terminate_child<ID>(
45    system: &System,
46    sup: ActorID,
47    child_id: ID,
48) -> Result<Exit, SupervisorError>
49where
50    ID: ChildID,
51{
52    let (tx, rx) = oneshot::channel();
53    let message = supervisor::Message::TerminateChild(child_id, tx);
54    system.send(sup, message).await;
55    rx.await.err_flatten_in()
56}
57
58pub async fn which_children<ID>(
59    system: &System,
60    sup: ActorID,
61) -> Result<Vec<(ID, ActorID)>, SupervisorError>
62where
63    ID: ChildID,
64{
65    let (tx, rx) = oneshot::channel();
66    let message = supervisor::Message::WhichChildren(tx);
67    system.send(sup, message).await;
68    rx.await.map_err(Into::into)
69}