@@ -3,8 +3,9 @@ use std::sync::Arc;
3
3
4
4
use crate :: epsilon:: server:: instances:: common:: instance_type:: InstanceType ;
5
5
use crate :: epsilon:: server:: instances:: common:: state:: EpsilonState ;
6
- use crate :: EResult ;
7
6
7
+ use crate :: epsilon:: epsilon_error:: EpsilonError ;
8
+ use anyhow:: format_err;
8
9
use async_minecraft_ping:: { ConnectionConfig , StatusResponse } ;
9
10
use kube:: CustomResource ;
10
11
use schemars:: JsonSchema ;
@@ -45,14 +46,24 @@ pub struct EpsilonInstanceStatus {
45
46
}
46
47
47
48
impl EpsilonInstance {
48
- pub async fn to_json ( & self ) -> InstanceJson {
49
- InstanceJson {
50
- name : self . metadata . name . as_ref ( ) . unwrap ( ) . clone ( ) ,
49
+ pub async fn to_json ( & self ) -> Result < InstanceJson , EpsilonError > {
50
+ Ok ( InstanceJson {
51
+ name : self . get_name ( ) ,
51
52
template : self . spec . template . clone ( ) ,
52
- state : self . get_state ( ) . clone ( ) ,
53
- slots : self . status . as_ref ( ) . unwrap ( ) . slots ,
53
+ state : * self . get_state ( ) ,
54
+
55
+ slots : self
56
+ . status
57
+ . as_ref ( )
58
+ . ok_or ( EpsilonError :: RetrieveStatusError ) ?
59
+ . slots ,
60
+
54
61
online_count : self . get_online_count ( ) . await . unwrap_or ( 0 ) ,
55
- }
62
+ } )
63
+ }
64
+
65
+ pub fn get_name ( & self ) -> String {
66
+ self . metadata . name . as_ref ( ) . unwrap ( ) . to_owned ( )
56
67
}
57
68
58
69
pub fn get_state ( & self ) -> & EpsilonState {
@@ -62,38 +73,51 @@ impl EpsilonInstance {
62
73
}
63
74
}
64
75
65
- pub async fn get_info ( & self ) -> EResult < StatusResponse > {
66
- let status = self . status . as_ref ( ) . unwrap ( ) ;
67
- let address = status. ip . as_ref ( ) . unwrap ( ) ;
68
- let config = ConnectionConfig :: build ( address) ;
76
+ pub async fn get_info ( & self ) -> Result < StatusResponse , EpsilonError > {
77
+ let status = self
78
+ . status
79
+ . as_ref ( )
80
+ . ok_or ( EpsilonError :: RetrieveStatusError ) ?;
69
81
70
- let duration = Duration :: from_millis ( 100 ) ;
82
+ let address = status
83
+ . ip
84
+ . as_ref ( )
85
+ . ok_or ( EpsilonError :: RetrieveStatusError ) ?;
71
86
72
- let connect = timeout ( duration, async move { config. connect ( ) . await } ) . await ??;
87
+ let config = ConnectionConfig :: build ( address) ;
88
+ let duration = Duration :: from_millis ( 150 ) ;
73
89
74
- let status = timeout ( duration, async move { connect. status ( ) . await } ) . await ??;
90
+ let status_result: Result < StatusResponse , EpsilonError > = timeout ( duration, async move {
91
+ Ok ( config. connect ( ) . await ?. status ( ) . await ?. status )
92
+ } )
93
+ . await ?;
75
94
76
- Ok ( status . status )
95
+ status_result
77
96
}
78
97
79
- pub async fn get_online_count ( & self ) -> EResult < i32 > {
98
+ pub async fn get_online_count ( & self ) -> Result < i32 , EpsilonError > {
80
99
Ok ( self . get_info ( ) . await ?. players . online as i32 )
81
100
}
82
101
83
- pub async fn get_available_slots ( & self ) -> EResult < i32 > {
84
- Ok ( & self . status . as_ref ( ) . unwrap ( ) . slots - self . get_online_count ( ) . await ?)
102
+ pub async fn get_available_slots ( & self ) -> Result < i32 , EpsilonError > {
103
+ Ok ( & self
104
+ . status
105
+ . as_ref ( )
106
+ . ok_or ( EpsilonError :: RetrieveStatusError ) ?
107
+ . slots
108
+ - self . get_online_count ( ) . await ?)
85
109
}
86
110
}
87
111
88
112
#[ async_trait]
89
113
pub trait VectorOfInstance {
90
- async fn get_available_slots ( & self ) -> EResult < i32 > ;
91
- async fn get_online_count ( & self ) -> EResult < i32 > ;
114
+ async fn get_available_slots ( & self ) -> Result < i32 , EpsilonError > ;
115
+ async fn get_online_count ( & self ) -> Result < i32 , EpsilonError > ;
92
116
}
93
117
94
118
#[ async_trait]
95
119
impl VectorOfInstance for Vec < Arc < EpsilonInstance > > {
96
- async fn get_available_slots ( & self ) -> EResult < i32 > {
120
+ async fn get_available_slots ( & self ) -> Result < i32 , EpsilonError > {
97
121
let mut available_slots = 0 ;
98
122
99
123
for instance in self {
@@ -103,7 +127,7 @@ impl VectorOfInstance for Vec<Arc<EpsilonInstance>> {
103
127
Ok ( available_slots)
104
128
}
105
129
106
- async fn get_online_count ( & self ) -> EResult < i32 > {
130
+ async fn get_online_count ( & self ) -> Result < i32 , EpsilonError > {
107
131
let mut number = 0 ;
108
132
109
133
for instance in self {
0 commit comments