Skip to main content

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::attempt::TaskExit;
7use crate::id::types::{Attempt, ChildId, 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    /// Stable child identifier.
29    pub id: ChildId,
30    /// Full child path.
31    pub path: SupervisorPath,
32    /// Child 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 attempt value.
39    pub attempt: Attempt,
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    /// let factory = rust_supervisor::task::factory::service_fn(|_ctx| async {
62    ///     rust_supervisor::task::factory::TaskResult::Succeeded
63    /// });
64    /// let spec = rust_supervisor::spec::child::ChildSpec::worker(
65    ///     rust_supervisor::id::types::ChildId::new("worker"),
66    ///     "worker",
67    ///     rust_supervisor::spec::child::TaskKind::AsyncWorker,
68    ///     std::sync::Arc::new(factory),
69    /// );
70    /// let runtime = rust_supervisor::registry::entry::ChildRuntime::new(
71    ///     spec,
72    ///     rust_supervisor::id::types::SupervisorPath::root().join("worker"),
73    /// );
74    /// assert!(matches!(runtime.status, rust_supervisor::registry::entry::ChildRuntimeStatus::Registered));
75    /// ```
76    pub fn new(spec: ChildSpec, path: SupervisorPath) -> Self {
77        Self {
78            id: spec.id.clone(),
79            path,
80            spec,
81            status: ChildRuntimeStatus::Registered,
82            generation: Generation::initial(),
83            attempt: Attempt::first(),
84            restart_count: 0,
85            last_exit: None,
86        }
87    }
88}