-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
154 lines (138 loc) · 4.8 KB
/
Copy pathmod.rs
File metadata and controls
154 lines (138 loc) · 4.8 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::time::sleep;
use tonic::transport::{Channel, Endpoint};
use tucana::{
aquila::{
RuntimeStatusUpdateRequest, runtime_status_service_client::RuntimeStatusServiceClient,
runtime_status_update_request::Status,
},
shared::{AdapterConfiguration, AdapterRuntimeStatus, RuntimeFeature},
};
pub struct DracoRuntimeStatusService {
channel: Channel,
identifier: String,
features: Vec<RuntimeFeature>,
configs: Vec<AdapterConfiguration>,
}
const MAX_BACKOFF: u64 = 2000 * 60;
const MAX_RETRIES: i8 = 10;
// Will create a channel and retry if its not possible
pub async fn create_channel_with_retry(channel_name: &str, url: String) -> Channel {
let mut backoff = 100;
let mut retries = 0;
loop {
let channel = match Endpoint::from_shared(url.clone()) {
Ok(c) => {
log::debug!("Creating a new endpoint for the: {} Service", channel_name);
c.connect_timeout(std::time::Duration::from_secs(2))
.timeout(std::time::Duration::from_secs(10))
}
Err(err) => {
panic!(
"Cannot create Endpoint for Service: `{}`. Reason: {:?}",
channel_name, err
);
}
};
match channel.connect().await {
Ok(ch) => {
return ch;
}
Err(err) => {
log::warn!(
"Retry connect to `{}` using url: `{}` failed: {:?}, retrying in {}ms",
channel_name,
url,
err,
backoff
);
sleep(std::time::Duration::from_millis(backoff)).await;
backoff = (backoff * 2).min(MAX_BACKOFF);
retries += 1;
if retries >= MAX_RETRIES {
panic!("Reached max retries to url {}", url)
}
}
}
}
}
impl DracoRuntimeStatusService {
pub async fn from_url(
aquila_url: String,
identifier: String,
features: Vec<RuntimeFeature>,
configs: Vec<AdapterConfiguration>,
) -> Self {
let channel = create_channel_with_retry("Aquila", aquila_url).await;
Self::new(channel, identifier, features, configs)
}
pub fn new(
channel: Channel,
identifier: String,
features: Vec<RuntimeFeature>,
configs: Vec<AdapterConfiguration>,
) -> Self {
DracoRuntimeStatusService {
channel,
identifier,
features,
configs,
}
}
async fn add_config(&mut self, feat: RuntimeFeature) {
self.features.push(feat);
}
pub async fn update_runtime_status_by_status(
&self,
status: tucana::shared::adapter_runtime_status::Status,
) {
log::info!("Updating the current Runtime Status!");
let mut client = RuntimeStatusServiceClient::new(self.channel.clone());
let now = SystemTime::now();
let timestamp = match now.duration_since(UNIX_EPOCH) {
Ok(time) => time.as_secs(),
Err(err) => {
log::error!("cannot get current system time: {:?}", err);
0
}
};
let request = RuntimeStatusUpdateRequest {
status: Some(Status::AdapterRuntimeStatus(AdapterRuntimeStatus {
status: status.into(),
timestamp: timestamp as i64,
identifier: self.identifier.clone(),
features: self.features.clone(),
configurations: self.configs.clone(),
})),
};
match client.update(request).await {
Ok(response) => {
log::info!(
"Was the update of the RuntimeStatus accepted by Sagittarius? {}",
response.into_inner().success
);
}
Err(err) => {
log::error!("Failed to update RuntimeStatus: {:?}", err);
}
}
}
async fn update_runtime_status(&self, status: AdapterRuntimeStatus) {
log::info!("Updating the current Runtime Status!");
let mut client = RuntimeStatusServiceClient::new(self.channel.clone());
let request = RuntimeStatusUpdateRequest {
status: Some(Status::AdapterRuntimeStatus(status)),
};
match client.update(request).await {
Ok(response) => {
log::info!(
"Was the update of the RuntimeStatus accepted by Sagittarius? {}",
response.into_inner().success
);
}
Err(err) => {
log::error!("Failed to update RuntimeStatus: {:?}", err);
}
}
}
}