1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use std::collections::BTreeMap;
use jiff::Timestamp;
use serde::Serialize;
use serde::de::DeserializeOwned;
use super::store::{Entry, EntryStatus, Kind, RunRecord};
use super::{RunId, Status};
/// A run's status, steps, and output or error, as [`workflow::find`](super::find) reads them.
///
/// `RunInfo` serializes, so a route may return it for a progress page.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RunInfo {
id: RunId,
workflow: String,
status: Status,
#[serde(serialize_with = "as_json")]
output: Option<String>,
error: Option<String>,
wake_at: Option<Timestamp>,
created_at: Timestamp,
finished_at: Option<Timestamp>,
attributes: BTreeMap<String, String>,
purged: bool,
steps: Vec<StepInfo>,
}
impl RunInfo {
pub(super) fn new(run: RunRecord, entries: Vec<Entry>) -> Self {
Self {
id: run.id,
workflow: run.workflow,
status: run.status,
output: run.output,
error: run.error,
wake_at: run.wake_at,
created_at: run.created_at,
finished_at: run.finished_at,
purged: run.purge && run.status.has_ended(),
attributes: run.attributes,
steps: entries.into_iter().map(StepInfo::new).collect(),
}
}
/// The run's ID.
#[must_use]
pub const fn id(&self) -> RunId {
self.id
}
/// The workflow's registered name.
#[must_use]
pub fn workflow(&self) -> &str {
&self.workflow
}
/// How far the run has come.
#[must_use]
pub const fn status(&self) -> Status {
self.status
}
/// The run's steps, sleeps, and waits, in the order the run first reached them.
#[must_use]
pub fn steps(&self) -> &[StepInfo] {
&self.steps
}
/// What the workflow returned, or `None` if the run hasn't completed.
///
/// # Errors
///
/// Returns an error if the output doesn't decode as `T`.
pub fn output<T: DeserializeOwned>(&self) -> crate::Result<Option<T>> {
self.output
.as_deref()
.map(|output| serde_json::from_str(output))
.transpose()
.map_err(Into::into)
}
/// The error the run failed with, or the reason it was cancelled.
#[must_use]
pub fn error(&self) -> Option<&str> {
self.error.as_deref()
}
/// The run's labels, set when it started and with [`Run::set_attribute`](super::Run::set_attribute).
#[must_use]
pub const fn attributes(&self) -> &BTreeMap<String, String> {
&self.attributes
}
/// Whether the store deleted the run's data when the run finished.
///
/// A run started with [`purge_when_finished`](super::PendingStart::purge_when_finished) loses
/// its output, its error, and its steps' outputs and errors when it finishes.
#[must_use]
pub const fn purged(&self) -> bool {
self.purged
}
/// When the run next has something to do, or `None` if it has ended or is waiting only for a
/// resume.
#[must_use]
pub const fn wake_at(&self) -> Option<Timestamp> {
self.wake_at
}
/// When the run started.
#[must_use]
pub const fn created_at(&self) -> Timestamp {
self.created_at
}
/// When the run completed, failed, or was cancelled, or `None` if it hasn't ended.
#[must_use]
pub const fn finished_at(&self) -> Option<Timestamp> {
self.finished_at
}
/// The tokens the run is waiting for, with a wait or an inbox.
pub fn waiting_for(&self) -> impl Iterator<Item = &str> {
self.steps
.iter()
.filter(|step| step.status == EntryStatus::Pending)
.filter_map(|step| match step.kind {
Kind::Wait => Some(step.name.as_str()),
Kind::Inbox => step.name.rsplit_once(" #").map(|(token, _)| token),
_ => None,
})
}
}
/// Serialize the output's JSON text as the JSON it holds.
fn as_json<S: serde::Serializer>(output: &Option<String>, serializer: S) -> Result<S::Ok, S::Error> {
let value = output
.as_deref()
.map(serde_json::from_str::<serde_json::Value>)
.transpose()
.map_err(serde::ser::Error::custom)?;
value.serialize(serializer)
}
/// One step, sleep, or wait of a [`RunInfo`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StepInfo {
kind: Kind,
name: String,
status: EntryStatus,
tries: u32,
error: Option<String>,
wake_at: Option<Timestamp>,
started_at: Timestamp,
finished_at: Option<Timestamp>,
timed_out: bool,
}
impl StepInfo {
fn new(entry: Entry) -> Self {
Self {
timed_out: matches!(entry.kind, Kind::Wait | Kind::Inbox)
&& entry.status == EntryStatus::Completed
&& entry.output.is_none(),
kind: entry.kind,
name: entry.name,
status: entry.status,
tries: entry.tries,
error: entry.error,
wake_at: entry.wake_at,
started_at: entry.started_at,
finished_at: entry.finished_at,
}
}
/// Whether the entry is a step, a sleep, or a wait.
#[must_use]
pub const fn kind(&self) -> Kind {
self.kind
}
/// The step's or sleep's name, or the wait's token.
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
/// How far the entry has come.
#[must_use]
pub const fn status(&self) -> EntryStatus {
self.status
}
/// How many times the step has been tried.
#[must_use]
pub const fn tries(&self) -> u32 {
self.tries
}
/// The error from the step's last failed try, if any.
#[must_use]
pub fn error(&self) -> Option<&str> {
self.error.as_deref()
}
/// When the sleep ends, the wait times out, or the step is tried again, if any of those is due.
#[must_use]
pub const fn wake_at(&self) -> Option<Timestamp> {
self.wake_at
}
/// When the run first reached the entry.
#[must_use]
pub const fn started_at(&self) -> Timestamp {
self.started_at
}
/// When the entry completed or failed, or `None` if it hasn't finished.
#[must_use]
pub const fn finished_at(&self) -> Option<Timestamp> {
self.finished_at
}
/// Whether the entry is a wait that timed out before a resume gave it a value.
#[must_use]
pub const fn timed_out(&self) -> bool {
self.timed_out
}
}