1use crate::{path_segment, Client, RequestProtocol, Result, Value, WorkflowHandle};
4use serde::{Deserialize, Deserializer, Serialize};
5use std::collections::BTreeMap;
6
7#[derive(Clone, Debug, thiserror::Error)]
11#[error("run activity metadata failed ({reason}): {message}")]
12#[non_exhaustive]
13pub struct WorkflowActivityMetadataError {
14 pub reason: String,
15 pub message: String,
16}
17
18fn invalid(reason: &str, message: &str) -> crate::Error {
19 WorkflowActivityMetadataError {
20 reason: reason.into(),
21 message: message.into(),
22 }
23 .into()
24}
25
26#[derive(Clone, Debug, Deserialize, Serialize)]
28#[non_exhaustive]
29pub struct WorkflowActivityList {
30 pub workflow_id: String,
31 pub run_id: String,
32 pub activities: Vec<WorkflowActivity>,
34 #[serde(flatten)]
36 pub raw: BTreeMap<String, Value>,
37}
38
39#[derive(Clone, Debug, Deserialize, Serialize)]
44#[non_exhaustive]
45pub struct WorkflowActivity {
46 pub id: Option<String>,
47 pub idempotency_key: Option<String>,
48 pub sequence: Option<i64>,
49 #[serde(rename = "type")]
50 pub activity_type: Option<String>,
51 #[serde(rename = "class")]
52 pub activity_class: Option<String>,
53 pub execution_mode: Option<String>,
54 pub local_activity: Option<bool>,
55 pub history_authority: Option<String>,
56 pub diagnostic_only: Option<bool>,
57 pub history_event_types: Option<Vec<String>>,
58 pub history_unsupported_reason: Option<String>,
59 pub reused_from_run_id: Option<String>,
60 pub reused_activity_execution_id: Option<String>,
61 pub reused_recorded_at: Option<String>,
62 pub row_status: Option<String>,
63 pub parallel_group_kind: Option<String>,
64 pub parallel_group_id: Option<String>,
65 pub parallel_group_base_sequence: Option<i64>,
66 pub parallel_group_size: Option<i64>,
67 pub parallel_group_index: Option<i64>,
68 pub parallel_group_path: Option<Vec<BTreeMap<String, Value>>>,
69 pub attempt_id: Option<String>,
70 pub worker_attempt_id: Option<String>,
71 pub status: Option<String>,
72 pub attempt_count: Option<i64>,
73 pub retry_policy: Option<Value>,
75 pub connection: Option<String>,
76 pub queue: Option<String>,
77 pub last_heartbeat_at: Option<String>,
78 #[serde(default, deserialize_with = "heartbeat_progress")]
79 pub last_heartbeat_progress: Option<Value>,
80 pub created_at: Option<String>,
81 pub started_at: Option<String>,
82 pub closed_at: Option<String>,
83 pub attempts: Option<Vec<WorkflowActivityAttempt>>,
85 #[serde(flatten)]
87 pub raw: BTreeMap<String, Value>,
88}
89
90#[derive(Clone, Debug, Deserialize, Serialize)]
92#[non_exhaustive]
93pub struct WorkflowActivityAttempt {
94 pub id: Option<String>,
95 pub worker_attempt_id: Option<String>,
96 pub attempt_number: Option<i64>,
97 pub status: Option<String>,
98 pub task_id: Option<String>,
99 pub lease_owner: Option<String>,
100 pub lease_expires_at: Option<String>,
101 pub started_at: Option<String>,
102 pub last_heartbeat_at: Option<String>,
103 #[serde(default, deserialize_with = "heartbeat_progress")]
104 pub last_heartbeat_progress: Option<Value>,
105 pub closed_at: Option<String>,
106 pub can_continue: Option<bool>,
107 pub cancel_requested: Option<bool>,
108 pub stop_reason: Option<String>,
109 pub failure_reason: Option<String>,
110 #[serde(flatten)]
111 pub raw: BTreeMap<String, Value>,
112}
113
114fn heartbeat_progress<'de, D: Deserializer<'de>>(
115 deserializer: D,
116) -> std::result::Result<Option<Value>, D::Error> {
117 match Option::<Value>::deserialize(deserializer)? {
118 Some(value @ (Value::Array(_) | Value::Object(_))) => Ok(Some(value)),
119 None => Ok(None),
120 _ => Err(serde::de::Error::custom(
121 "heartbeat progress must be an object, array or null",
122 )),
123 }
124}
125
126impl Client {
127 pub async fn get_run_activities(
133 &self,
134 workflow_id: &str,
135 run_id: &str,
136 ) -> Result<WorkflowActivityList> {
137 let path = format!(
138 "/workflows/{}/runs/{}/activities",
139 path_segment("workflow_id", workflow_id)?,
140 path_segment("run_id", run_id)?,
141 );
142 let document: Value = self
143 .request_json(
144 reqwest::Method::GET,
145 &path,
146 RequestProtocol::ControlPlane,
147 Option::<&Value>::None,
148 )
149 .await?;
150 let view: WorkflowActivityList = serde_json::from_value(document).map_err(|_| {
151 invalid(
152 "malformed_response",
153 "the response does not match the run activity metadata contract",
154 )
155 })?;
156 if view.workflow_id != workflow_id || view.run_id != run_id {
157 return Err(invalid(
158 "run_identity_mismatch",
159 "the response belongs to another workflow or run",
160 ));
161 }
162 Ok(view)
163 }
164}
165
166impl WorkflowHandle {
167 pub async fn get_activities(&self) -> Result<WorkflowActivityList> {
172 let run_id = self.run_id.as_deref().ok_or_else(|| {
173 invalid(
174 "missing_run_id",
175 "a selected run ID is required for activity inspection",
176 )
177 })?;
178 self.client
179 .get_run_activities(&self.workflow_id, run_id)
180 .await
181 }
182}