Skip to main content

rust_supervisor/role/context/
worker.rs

1//! Worker role context wrapper.
2//!
3//! The wrapper keeps worker code focused on readiness, heartbeat, and
4//! cancellation observation without exposing the full runtime task context.
5
6use crate::id::types::{ChildId, SupervisorPath};
7use crate::task::context::TaskContext;
8
9/// Narrow context passed to a worker role.
10#[derive(Debug, Clone)]
11pub struct WorkerContext {
12    /// Runtime task context hidden behind the worker-specific API.
13    inner: TaskContext,
14}
15
16impl WorkerContext {
17    /// Creates a worker context from a runtime task context.
18    ///
19    /// # Arguments
20    ///
21    /// - `inner`: Runtime context for one worker child_start_count.
22    ///
23    /// # Returns
24    ///
25    /// Returns a worker-specific context wrapper.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// let (task_context, _heartbeat) = rust_supervisor::task::context::TaskContext::new(
31    ///     rust_supervisor::id::types::ChildId::new("worker"),
32    ///     rust_supervisor::id::types::SupervisorPath::root().join("worker"),
33    ///     rust_supervisor::id::types::Generation::initial(),
34    ///     rust_supervisor::id::types::ChildStartCount::first(),
35    /// );
36    /// let worker_context = rust_supervisor::role::context::worker::WorkerContext::new(
37    ///     task_context,
38    /// );
39    /// assert!(!worker_context.is_cancelled());
40    /// ```
41    pub fn new(inner: TaskContext) -> Self {
42        Self { inner }
43    }
44
45    /// Reports that the worker is ready.
46    ///
47    /// # Arguments
48    ///
49    /// This function has no arguments.
50    ///
51    /// # Returns
52    ///
53    /// This function does not return a value.
54    ///
55    /// # Examples
56    ///
57    /// ```
58    /// let (task_context, _heartbeat) = rust_supervisor::task::context::TaskContext::new(
59    ///     rust_supervisor::id::types::ChildId::new("worker"),
60    ///     rust_supervisor::id::types::SupervisorPath::root().join("worker"),
61    ///     rust_supervisor::id::types::Generation::initial(),
62    ///     rust_supervisor::id::types::ChildStartCount::first(),
63    /// );
64    /// let worker_context = rust_supervisor::role::context::worker::WorkerContext::new(
65    ///     task_context,
66    /// );
67    /// worker_context.ready();
68    /// ```
69    pub fn ready(&self) {
70        self.inner.mark_ready();
71    }
72
73    /// Emits a worker heartbeat.
74    ///
75    /// # Arguments
76    ///
77    /// This function has no arguments.
78    ///
79    /// # Returns
80    ///
81    /// This function does not return a value.
82    pub fn heartbeat(&self) {
83        self.inner.heartbeat();
84    }
85
86    /// Returns whether cancellation has been requested.
87    ///
88    /// # Arguments
89    ///
90    /// This function has no arguments.
91    ///
92    /// # Returns
93    ///
94    /// Returns `true` when runtime cancellation has been requested.
95    pub fn is_cancelled(&self) -> bool {
96        self.inner.is_cancelled()
97    }
98
99    /// Waits until cancellation has been requested.
100    ///
101    /// # Arguments
102    ///
103    /// This function has no arguments.
104    ///
105    /// # Returns
106    ///
107    /// Returns after the runtime cancellation token has been cancelled.
108    pub async fn wait_cancelled(&self) {
109        self.inner.cancellation_token().cancelled().await;
110    }
111
112    /// Returns the stable worker child identifier.
113    ///
114    /// # Arguments
115    ///
116    /// This function has no arguments.
117    ///
118    /// # Returns
119    ///
120    /// Returns the child identifier owned by the runtime context.
121    pub fn child_id(&self) -> &ChildId {
122        &self.inner.child_id
123    }
124
125    /// Returns the worker path in the supervisor tree.
126    ///
127    /// # Arguments
128    ///
129    /// This function has no arguments.
130    ///
131    /// # Returns
132    ///
133    /// Returns the supervisor tree path owned by the runtime context.
134    pub fn path(&self) -> &SupervisorPath {
135        &self.inner.path
136    }
137}