feldera_types/
runtime_status.rs1use crate::error::ErrorResponse;
2use actix_web::body::BoxBody;
3use actix_web::http::StatusCode;
4use actix_web::{HttpRequest, HttpResponse, HttpResponseBuilder, Responder, ResponseError};
5use serde::{Deserialize, Serialize};
6use std::fmt;
7use std::fmt::Display;
8use utoipa::ToSchema;
9
10#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, ToSchema)]
15pub enum RuntimeStatus {
16 Unavailable,
26
27 Standby,
29
30 Initializing,
33
34 Bootstrapping,
37
38 Replaying,
41
42 Paused,
44
45 Running,
47
48 Suspended,
50}
51
52#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
53pub enum RuntimeDesiredStatus {
54 Unavailable,
55 Standby,
56 Paused,
57 Running,
58 Suspended,
59}
60
61impl RuntimeDesiredStatus {
62 pub fn may_transition_to(&self, target: Self) -> bool {
63 match (*self, target) {
64 (old, new) if old == new => true,
65 (Self::Standby, Self::Paused | Self::Running) => true,
66 (Self::Paused, Self::Running | Self::Suspended) => true,
67 (Self::Running, Self::Paused | Self::Suspended) => true,
68 _ => false,
69 }
70 }
71
72 pub fn may_transition_to_at_startup(&self, target: Self) -> bool {
73 match (*self, target) {
74 (Self::Suspended, _) => {
75 matches!(target, Self::Paused | Self::Running)
78 }
79 (old, new) if old.may_transition_to(new) => true,
80 _ => false,
81 }
82 }
83}
84
85impl From<String> for RuntimeDesiredStatus {
86 fn from(value: String) -> Self {
87 match value.as_str() {
88 "unavailable" => Self::Unavailable,
89 "standby" => Self::Standby,
90 "paused" => Self::Paused,
91 "running" => Self::Running,
92 "suspended" => Self::Suspended,
93 _ => panic!("Invalid runtime desired status: {value}"),
94 }
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
99pub struct ExtendedRuntimeStatus {
100 pub runtime_status: RuntimeStatus,
102
103 pub runtime_status_details: String,
107
108 pub runtime_desired_status: RuntimeDesiredStatus,
110}
111
112impl Responder for ExtendedRuntimeStatus {
113 type Body = BoxBody;
114
115 fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> {
116 HttpResponseBuilder::new(StatusCode::OK).json(self)
117 }
118}
119
120impl From<ExtendedRuntimeStatus> for HttpResponse<BoxBody> {
121 fn from(value: ExtendedRuntimeStatus) -> Self {
122 HttpResponseBuilder::new(StatusCode::OK).json(value)
123 }
124}
125
126#[derive(Clone, Debug)]
128pub struct ExtendedRuntimeStatusError {
129 pub status_code: StatusCode,
132
133 pub error: ErrorResponse,
135}
136
137impl Display for ExtendedRuntimeStatusError {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 write!(f, "{}: {:?}", self.status_code, self.error)
140 }
141}
142
143impl ResponseError for ExtendedRuntimeStatusError {
144 fn status_code(&self) -> StatusCode {
145 self.status_code
146 }
147
148 fn error_response(&self) -> HttpResponse<BoxBody> {
149 HttpResponseBuilder::new(self.status_code()).json(self.error.clone())
150 }
151}