summaryrefslogtreecommitdiff
path: root/src/protocol.rs
blob: 9f79a7c7d8a3fb579f7d2e2aef266d3f035f6021 (plain)
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
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
#[serde(rename_all = "snake_case")]
pub enum ClientProto {
    Started,
    ArtifactCreate,
    NewTask(RequestedJob),
    NewTaskPlease { allowed_pushers: Option<Vec<String>>, host_info: HostInfo },
    Metric { name: String, value: String },
    Command(CommandInfo),
    TaskStatus(TaskInfo),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "command_info")]
#[serde(rename_all = "snake_case")]
pub enum CommandInfo {
    Started { command: Vec<String>, cwd: Option<String>, id: u32 },
    Finished { exit_code: Option<i32>, id: u32 },
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "task_info")]
#[serde(rename_all = "snake_case")]
pub enum TaskInfo {
    Finished { status: String },
    Interrupted { status: String, description: Option<String> },
}

impl ClientProto {
    pub fn metric(name: impl Into<String>, value: impl Into<String>) -> Self {
        ClientProto::Metric { name: name.into(), value: value.into() }
    }

    pub fn command(state: CommandInfo) -> Self {
        ClientProto::Command(state)
    }

    pub fn new_task_please(allowed_pushers: Option<Vec<String>>, host_info: HostInfo) -> Self {
        ClientProto::NewTaskPlease { allowed_pushers, host_info }
    }

    pub fn task_status(state: TaskInfo) -> Self {
        ClientProto::TaskStatus(state)
    }

    pub fn new_task(task: RequestedJob) -> Self {
        ClientProto::NewTask(task)
    }
}

impl CommandInfo {
    pub fn started(command: impl Into<Vec<String>>, cwd: Option<&str>, id: u32) -> Self {
        CommandInfo::Started { command: command.into(), cwd: cwd.map(ToOwned::to_owned), id }
    }

    pub fn finished(exit_code: Option<i32>, id: u32) -> Self {
        CommandInfo::Finished { exit_code, id }
    }
}

impl TaskInfo {
    pub fn finished(status: impl Into<String>) -> Self {
        TaskInfo::Finished { status: status.into() }
    }

    pub fn interrupted(status: impl Into<String>, description: impl Into<Option<String>>) -> Self {
        TaskInfo::Interrupted { status: status.into(), description: description.into() }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HostInfo {
    pub hostname: String,
    pub cpu_info: CpuInfo,
    pub memory_info: MemoryInfo,
    pub env_info: EnvInfo,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CpuInfo {
    pub model_name: String,
    pub microcode: String,
    pub cores: u32,
    pub vendor_id: String,
    pub family: String,
    pub model: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MemoryInfo {
    pub total: String,
    pub available: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EnvInfo {
    pub arch: String,
    pub family: String,
    pub os: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestedJob {
    pub commit: String,
    pub remote_url: String,
    pub build_token: String,
}