Skip to main content

dapr_durabletask/task/
activity_context.rs

1use crate::api::PropagatedHistory;
2
3/// Context provided to activity functions during execution.
4pub struct ActivityContext {
5    pub(crate) orchestration_id: String,
6    pub(crate) task_id: i32,
7    pub(crate) task_execution_id: String,
8    pub(crate) propagated_history: Option<PropagatedHistory>,
9}
10
11impl ActivityContext {
12    pub fn new(orchestration_id: String, task_id: i32, task_execution_id: String) -> Self {
13        Self {
14            orchestration_id,
15            task_id,
16            task_execution_id,
17            propagated_history: None,
18        }
19    }
20
21    /// Construct an activity context with an attached propagated history
22    /// (delivered by the worker via `ActivityRequest.propagated_history`).
23    pub fn with_propagated_history(mut self, history: Option<PropagatedHistory>) -> Self {
24        self.propagated_history = history;
25        self
26    }
27
28    pub fn orchestration_id(&self) -> &str {
29        &self.orchestration_id
30    }
31
32    pub fn task_id(&self) -> i32 {
33        self.task_id
34    }
35
36    /// A unique identifier for this specific activity execution.
37    ///
38    /// Unlike [`task_id`](Self::task_id), which is deterministic and reused across retries,
39    /// `task_execution_id` is unique per attempt and can be used for
40    /// idempotency keys or deduplication.
41    pub fn task_execution_id(&self) -> &str {
42        &self.task_execution_id
43    }
44
45    /// Returns history forwarded from the calling workflow, if the workflow
46    /// scheduled this activity with a non-`None` history propagation scope.
47    pub fn propagated_history(&self) -> Option<&PropagatedHistory> {
48        self.propagated_history.as_ref()
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_activity_context() {
58        let ctx = ActivityContext::new("inst-1".to_string(), 42, "exec-abc".to_string());
59        assert_eq!(ctx.orchestration_id(), "inst-1");
60        assert_eq!(ctx.task_id(), 42);
61        assert_eq!(ctx.task_execution_id(), "exec-abc");
62    }
63}