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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct TaskEvent {
    pub r#type: Option<String>,
    pub time: Option<i64>,
    pub display_message: Option<String>,
    pub details: HashMap<String, String>,
    // fields below are deprecated
    pub fails_task: Option<bool>,
    pub restart_reason: Option<String>,
    pub setup_error: Option<String>,
    pub driver_error: Option<String>,
    pub driver_message: Option<String>,
    pub exit_code: Option<i32>,
    pub signal: Option<i32>,
    pub message: Option<String>,
    pub kill_reason: Option<String>,
    pub kill_timeout: Option<i64>,
    pub kill_error: Option<String>,
    pub start_delay: Option<i64>,
    pub download_error: Option<String>,
    pub validation_error: Option<String>,
    pub disk_limit: Option<i64>,
    pub disk_size: Option<i64>,
    pub failed_sibling: Option<String>,
    pub vault_error: Option<String>,
    pub task_signal_reason: Option<String>,
    pub task_signal: Option<String>,
    pub generic_source: Option<String>,
}

impl TaskEvent {
    pub fn exit_code(&self) -> Option<i32> {
        if let Some(Ok(exit_code)) = self.details.get("exit_code").map(|s| s.parse::<i32>()) {
            return Some(exit_code);
        }
        None
    }

    pub fn driver_message(&self) -> Option<String> {
        self.details.get("driver_message").map(|s| s.into())
    }

    pub fn restart_reason(&self) -> Option<String> {
        self.details.get("restart_reason").map(|s| s.into())
    }

    pub fn kill_reason(&self) -> Option<String> {
        self.details.get("kill_reason").map(|s| s.into())
    }

    pub fn has_error(&self) -> bool {
        for key in error_keys() {
            if self.details.contains_key(key) {
                return true;
            }
        }

        false
    }

    pub fn error(&self, error_key: &str) -> Option<String> {
        self.details.get(error_key).map(|s| s.into())
    }

    pub fn all_errors(&self) -> Option<Vec<(&str, String)>> {
        let mut errors: Vec<(&str, String)> = Vec::new();

        for key in error_keys() {
            match self.details.get(key) {
                Some(error) => errors.push((key, error.into())),
                None => continue,
            }
        }

        if errors.is_empty() {
            None
        } else {
            Some(errors)
        }
    }
}

pub fn error_keys() -> Vec<&'static str> {
    vec![
        "validation_error",
        "kill_error",
        "setup_error",
        "driver_error",
        "download_error",
        "vault_renewal_error",
    ]
}