Skip to main content

temporalio_workflow/workflow_context/
view.rs

1use std::time::{Duration, SystemTime};
2
3use temporalio_common_wasm::{
4    Memo, Priority, RetryPolicy, WorkflowExecution,
5    data_converters::{PayloadConverter, SerializationContextData},
6    protos::coresdk::{
7        common::NamespacedWorkflowExecution, workflow_activation::InitializeWorkflow,
8    },
9    search_attributes::SearchAttributes,
10};
11
12/// Read-only view of workflow context for use in init and query handlers.
13///
14/// This provides access to workflow information but cannot issue commands.
15#[derive(Clone, Debug)]
16#[non_exhaustive]
17pub struct WorkflowContextView {
18    raw: InitializeWorkflow,
19    namespace: String,
20    task_queue: String,
21    run_id: String,
22    payload_converter: PayloadConverter,
23}
24
25impl WorkflowContextView {
26    /// Create a new view from workflow initialization data.
27    pub(crate) fn new(
28        namespace: String,
29        task_queue: String,
30        run_id: String,
31        raw: InitializeWorkflow,
32        payload_converter: PayloadConverter,
33    ) -> Self {
34        Self {
35            raw,
36            namespace,
37            task_queue,
38            run_id,
39            payload_converter,
40        }
41    }
42
43    pub(super) fn into_parts(self) -> (String, String, String, InitializeWorkflow) {
44        (self.namespace, self.task_queue, self.run_id, self.raw)
45    }
46
47    /// Returns the workflow's unique identifier.
48    pub fn workflow_id(&self) -> &str {
49        &self.raw.workflow_id
50    }
51
52    /// Returns the run ID of this workflow execution.
53    pub fn run_id(&self) -> &str {
54        &self.run_id
55    }
56
57    /// Returns the workflow type name.
58    pub fn workflow_type(&self) -> &str {
59        &self.raw.workflow_type
60    }
61
62    /// Returns the task queue this workflow is executing on.
63    pub fn task_queue(&self) -> &str {
64        &self.task_queue
65    }
66
67    /// Returns the namespace this workflow is executing in.
68    pub fn namespace(&self) -> &str {
69        &self.namespace
70    }
71
72    /// Returns the current attempt number, starting from one.
73    pub fn attempt(&self) -> u32 {
74        self.raw.attempt as u32
75    }
76
77    /// Returns the run ID of the first execution in the chain.
78    pub fn first_execution_run_id(&self) -> &str {
79        &self.raw.first_execution_run_id
80    }
81
82    /// Returns the run ID of the previous execution when this is a continuation.
83    pub fn continued_from_run_id(&self) -> Option<&str> {
84        (!self.raw.continued_from_execution_run_id.is_empty())
85            .then_some(self.raw.continued_from_execution_run_id.as_str())
86    }
87
88    /// Returns when the workflow execution started.
89    pub fn start_time(&self) -> Option<SystemTime> {
90        self.raw.start_time.and_then(|time| time.try_into().ok())
91    }
92
93    /// Returns the total workflow execution timeout, including retries and continue-as-new.
94    pub fn execution_timeout(&self) -> Option<Duration> {
95        self.raw
96            .workflow_execution_timeout
97            .and_then(|timeout| timeout.try_into().ok())
98    }
99
100    /// Returns the timeout of a single workflow run.
101    pub fn run_timeout(&self) -> Option<Duration> {
102        self.raw
103            .workflow_run_timeout
104            .and_then(|timeout| timeout.try_into().ok())
105    }
106
107    /// Returns the timeout of a single workflow task.
108    pub fn task_timeout(&self) -> Option<Duration> {
109        self.raw
110            .workflow_task_timeout
111            .and_then(|timeout| timeout.try_into().ok())
112    }
113
114    /// Returns information about the parent workflow when this is a child workflow.
115    pub fn parent(&self) -> Option<NamespacedWorkflowInfo> {
116        self.raw
117            .parent_workflow_info
118            .clone()
119            .map(NamespacedWorkflowInfo::from_raw)
120    }
121
122    /// Returns information about the root workflow in the execution chain.
123    pub fn root(&self) -> Option<WorkflowExecution> {
124        self.raw.root_workflow.clone().map(Into::into)
125    }
126
127    /// Returns the workflow's retry policy.
128    pub fn retry_policy(&self) -> Option<RetryPolicy> {
129        self.raw.retry_policy.clone().map(Into::into)
130    }
131
132    /// Returns the cron schedule when this workflow runs on one.
133    pub fn cron_schedule(&self) -> Option<&str> {
134        (!self.raw.cron_schedule.is_empty()).then_some(self.raw.cron_schedule.as_str())
135    }
136
137    /// Returns priority and fairness configuration for this workflow execution.
138    pub fn priority(&self) -> Priority {
139        self.raw.priority.clone().unwrap_or_default().into()
140    }
141
142    /// Returns user-defined memo values.
143    pub fn memo(&self) -> Memo {
144        Memo::from_raw(
145            self.raw.memo.clone(),
146            self.payload_converter.clone(),
147            SerializationContextData::Workflow,
148        )
149    }
150
151    /// Returns initial search attributes as a typed collection.
152    pub fn search_attributes(&self) -> Option<SearchAttributes> {
153        self.raw
154            .search_attributes
155            .as_ref()
156            .map(SearchAttributes::from_proto)
157    }
158
159    /// Accesses the underlying workflow initialization protobuf.
160    pub fn raw(&self) -> &InitializeWorkflow {
161        &self.raw
162    }
163
164    /// Consumes this view and returns the underlying workflow initialization protobuf.
165    pub fn into_raw(self) -> InitializeWorkflow {
166        self.raw
167    }
168}
169
170/// Information about a parent workflow.
171#[derive(Clone, Debug)]
172#[non_exhaustive]
173pub struct NamespacedWorkflowInfo {
174    raw: NamespacedWorkflowExecution,
175}
176
177impl NamespacedWorkflowInfo {
178    fn from_raw(raw: NamespacedWorkflowExecution) -> Self {
179        Self { raw }
180    }
181
182    /// Returns the parent workflow's unique identifier.
183    pub fn workflow_id(&self) -> &str {
184        &self.raw.workflow_id
185    }
186
187    /// Returns the parent workflow's run ID.
188    pub fn run_id(&self) -> &str {
189        &self.raw.run_id
190    }
191
192    /// Returns the parent workflow's namespace.
193    pub fn namespace(&self) -> &str {
194        &self.raw.namespace
195    }
196
197    /// Accesses the underlying parent workflow protobuf.
198    pub fn raw(&self) -> &NamespacedWorkflowExecution {
199        &self.raw
200    }
201
202    /// Consumes this wrapper and returns the underlying parent workflow protobuf.
203    pub fn into_raw(self) -> NamespacedWorkflowExecution {
204        self.raw
205    }
206}