Skip to main content

airup_sdk/
system.rs

1use crate::{Error, files::Service};
2use serde::{Deserialize, Serialize};
3
4/// Result of querying a service.
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct QueryService {
7    pub status: Status,
8    pub status_since: Option<i64>,
9    pub pid: Option<i64>,
10    pub memory_usage: Option<u64>,
11    pub task_class: Option<String>,
12    pub last_error: Option<Error>,
13    pub definition: Service,
14}
15impl QueryService {
16    pub fn default_of(definition: Service) -> Self {
17        Self {
18            status: Status::Stopped,
19            status_since: None,
20            pid: None,
21            memory_usage: None,
22            task_class: None,
23            last_error: None,
24            definition,
25        }
26    }
27}
28
29/// Result of querying information about the whole system.
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct QuerySystem {
32    /// Status of the system.
33    pub status: Status,
34
35    /// Timestamp generated when the system started to boot.
36    pub boot_timestamp: i64,
37
38    /// Timestamp generated when the system completed booting.
39    pub booted_since: Option<i64>,
40
41    /// Indicates whether the system is booting.
42    pub is_booting: bool,
43
44    /// List of entered milestones in the system.
45    pub milestones: Vec<EnteredMilestone>,
46
47    /// Instance name of the server.
48    pub instance_name: String,
49
50    /// List of cached services in the system.
51    pub services: Vec<String>,
52}
53
54/// Representation of the status of a service.
55#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Serialize, Deserialize)]
56#[serde(rename_all = "kebab-case")]
57pub enum Status {
58    /// The service is active.
59    Active,
60
61    /// The service has stopped.
62    #[default]
63    Stopped,
64}
65
66/// Item of an log record
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct LogRecord {
69    /// Timestamp generated when the log record is emitted.
70    pub timestamp: i64,
71
72    /// Module of the log record.
73    pub module: String,
74
75    /// Message of the log record.
76    pub message: String,
77}
78
79/// Information of an entered milestone.
80#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct EnteredMilestone {
82    /// Name of the milestone.
83    pub name: String,
84
85    /// Timestamp generated when we started to enter the milestone.
86    pub begin_timestamp: i64,
87
88    /// Timestamp generated when we completed entering the milestone.
89    pub finish_timestamp: i64,
90}
91
92/// Representation of an Airup event.
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct Event {
95    /// Identifier of the event.
96    pub id: String,
97
98    /// Payload data of the event.
99    pub payload: String,
100}
101impl Event {
102    /// Creates a new [`Event`] instance with given ID and paylaod.
103    pub fn new(id: String, payload: String) -> Self {
104        Self { id, payload }
105    }
106}
107
108/// An extension trait to provide `system.*` API invocation.
109pub trait ConnectionExt<'a>: crate::Connection {
110    /// Sideloads a service.
111    fn sideload_service(&'a mut self, name: &'a str, service: &'a Service) -> Self::Invoke<'a, ()> {
112        self.invoke("system.sideload_service", (name, service))
113    }
114
115    /// Starts the specified service.
116    fn start_service(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
117        self.invoke("system.start_service", name)
118    }
119
120    /// Stops the specified service.
121    fn stop_service(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
122        self.invoke("system.stop_service", name)
123    }
124
125    /// Forces the specified service to stop.
126    fn kill_service(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
127        self.invoke("system.kill_service", name)
128    }
129
130    /// Reloads the specified service.
131    fn reload_service(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
132        self.invoke("system.reload_service", name)
133    }
134
135    /// Caches the specified service.
136    fn cache_service(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
137        self.invoke("system.cache_service", name)
138    }
139
140    /// Uncaches the specified service.
141    fn uncache_service(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
142        self.invoke("system.uncache_service", name)
143    }
144
145    /// Queries the specified service.
146    fn query_service(&'a mut self, name: &'a str) -> Self::Invoke<'a, QueryService> {
147        self.invoke("system.query_service", name)
148    }
149
150    /// Interrupts current task running in specific service's supervisor.
151    fn interrupt_service_task(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
152        self.invoke("system.interrupt_service_task", name)
153    }
154
155    /// Queries information about the whole system.
156    fn query_system(&'a mut self) -> Self::Invoke<'a, QuerySystem> {
157        self.invoke("system.query_system", ())
158    }
159
160    /// Lists all services.
161    fn list_services(&'a mut self) -> Self::Invoke<'a, Vec<String>> {
162        self.invoke("system.list_services", ())
163    }
164
165    /// Refreshes cached system information in the `airupd` daemon.
166    fn refresh(&'a mut self) -> Self::Invoke<'a, Vec<(String, Error)>> {
167        self.invoke("system.refresh", ())
168    }
169
170    /// Deletes cached system information in the `airupd` daemon.
171    fn gc(&'a mut self) -> Self::Invoke<'a, ()> {
172        self.invoke("system.gc", ())
173    }
174
175    /// Enters the specific milestone.
176    fn enter_milestone(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
177        self.invoke("system.enter_milestone", name)
178    }
179
180    /// Sets the server's instance name.
181    fn set_instance_name(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
182        self.invoke("system.set_instance_name", name)
183    }
184
185    /// Triggers the specific event.
186    fn trigger_event(&'a mut self, event: &'a Event) -> Self::Invoke<'a, ()> {
187        self.invoke("system.trigger_event", event)
188    }
189
190    /// Unloads an extension.
191    fn unregister_extension(&'a mut self, name: &'a str) -> Self::Invoke<'a, ()> {
192        self.invoke("system.unregister_extension", name)
193    }
194}
195impl<T> ConnectionExt<'_> for T where T: crate::Connection {}