Skip to main content

isothrum_runner/error/
timed_out.rs

1//! Which limit a step that ran out of time ran into.
2
3use serde::{Deserialize, Serialize};
4
5/// Which limit a step ran into: a timeout's, also the `error` event's `timeout`.
6///
7/// A step's limit (its `timeout_s`) counts the time it shows no progress: progress is an output
8/// line, on either stream, that isn't blank and isn't one of the last distinct lines it printed
9/// (a stuck download repeating `Downloading 1.121GB` makes none). A step that prints nothing has
10/// its limit from its start. A step that keeps making progress still stops at a ceiling: six
11/// hours from its start, or its limit when that is longer.
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
13#[serde(tag = "kind", rename_all = "snake_case")]
14#[non_exhaustive]
15pub enum TimedOut {
16    /// It showed no progress for its limit.
17    NoProgress {
18        /// The limit, in seconds.
19        after_s: u64,
20        /// The last line it printed (secrets masked, cut to 300 bytes); none when it printed
21        /// nothing.
22        #[serde(default, skip_serializing_if = "Option::is_none")]
23        last_line: Option<String>,
24    },
25    /// It was still making progress at the ceiling.
26    Ceiling {
27        /// The ceiling, in seconds from the step's start.
28        after_s: u64,
29    },
30}
31
32impl TimedOut {
33    /// The limit it ran into, in seconds.
34    #[must_use]
35    pub fn after_s(&self) -> u64 {
36        match self {
37            TimedOut::NoProgress { after_s, .. } | TimedOut::Ceiling { after_s } => *after_s,
38        }
39    }
40
41    /// The same, its last line passed through `mask`.
42    pub(crate) fn masked(self, mask: impl FnOnce(&str) -> String) -> TimedOut {
43        match self {
44            TimedOut::NoProgress { after_s, last_line } => TimedOut::NoProgress {
45                after_s,
46                last_line: last_line.map(|l| mask(&l)),
47            },
48            ceiling @ TimedOut::Ceiling { .. } => ceiling,
49        }
50    }
51}
52
53impl std::fmt::Display for TimedOut {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        match self {
56            TimedOut::NoProgress {
57                after_s,
58                last_line: Some(line),
59            } => {
60                write!(f, "showed no progress for {after_s}s and was stopped; its last line: `{line}`")
61            }
62            TimedOut::NoProgress { after_s, last_line: None } => write!(f, "printed nothing for {after_s}s and was stopped"),
63            TimedOut::Ceiling { after_s } => {
64                write!(f, "was stopped after {after_s}s, the most a step may run, though it kept making progress")
65            }
66        }
67    }
68}