rust_supervisor/registry/entry.rs
1//! Registry entry types.
2//!
3//! This module owns the runtime record for a registered child and keeps that
4//! record independent from task execution.
5
6use crate::child_runner::run_exit::TaskExit;
7use crate::id::types::{ChildId, ChildStartCount, Generation, SupervisorPath};
8use crate::spec::child::ChildSpec;
9
10/// Runtime status for a registered child.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum ChildRuntimeStatus {
13 /// The child exists in the registry but has not started.
14 Registered,
15 /// The child is starting.
16 Starting,
17 /// The child task is running.
18 Running,
19 /// The child task reported readiness.
20 Ready,
21 /// The child exited.
22 Exited,
23}
24
25/// Runtime state owned by the registry for one child.
26#[derive(Debug, Clone)]
27pub struct ChildRuntime {
28 /// Child task identifier.
29 pub id: ChildId,
30 /// Full child task path.
31 pub path: SupervisorPath,
32 /// Child task declaration copied from the supervisor specification.
33 pub spec: ChildSpec,
34 /// Current runtime status.
35 pub status: ChildRuntimeStatus,
36 /// Current generation value.
37 pub generation: Generation,
38 /// Current child_start_count value.
39 pub child_start_count: ChildStartCount,
40 /// Number of restarts that have occurred.
41 pub restart_count: u64,
42 /// Last known task exit.
43 pub last_exit: Option<TaskExit>,
44}
45
46impl ChildRuntime {
47 /// Creates a registry runtime record for a child.
48 ///
49 /// # Arguments
50 ///
51 /// - `spec`: Child declaration.
52 /// - `path`: Full child path in the supervisor tree.
53 ///
54 /// # Returns
55 ///
56 /// Returns a [`ChildRuntime`] in registered status.
57 ///
58 /// # Examples
59 ///
60 /// ```
61 /// # fn example() -> Result<(), rust_supervisor::error::types::SupervisorError> {
62 /// let factory = rust_supervisor::task::factory::service_fn(|_ctx| async {
63 /// rust_supervisor::task::factory::TaskResult::Succeeded
64 /// });
65 /// let spec = rust_supervisor::spec::child::ChildSpec::worker(
66 /// rust_supervisor::id::types::ChildId::new("worker"),
67 /// "worker",
68 /// rust_supervisor::spec::child::TaskKind::AsyncWorker,
69 /// std::sync::Arc::new(factory),
70 /// )?;
71 /// let runtime = rust_supervisor::registry::entry::ChildRuntime::new(
72 /// spec,
73 /// rust_supervisor::id::types::SupervisorPath::root().join("worker"),
74 /// );
75 /// assert!(matches!(runtime.status, rust_supervisor::registry::entry::ChildRuntimeStatus::Registered));
76 /// # Ok(())
77 /// # }
78 /// ```
79 pub fn new(spec: ChildSpec, path: SupervisorPath) -> Self {
80 Self {
81 id: spec.id.clone(),
82 path,
83 spec,
84 status: ChildRuntimeStatus::Registered,
85 generation: Generation::initial(),
86 child_start_count: ChildStartCount::first(),
87 restart_count: 0,
88 last_exit: None,
89 }
90 }
91}