|
| 1 | +use std::time::{SystemTime, UNIX_EPOCH}; |
| 2 | + |
| 3 | +use tonic::transport::Channel; |
| 4 | +use tucana::{ |
| 5 | + aquila::{ |
| 6 | + RuntimeStatusUpdateRequest, runtime_status_service_client::RuntimeStatusServiceClient, |
| 7 | + runtime_status_update_request::Status, |
| 8 | + }, |
| 9 | + shared::{AdapterConfiguration, AdapterRuntimeStatus, RuntimeFeature}, |
| 10 | +}; |
| 11 | + |
| 12 | +struct DracoRuntimeStatusService { |
| 13 | + channel: Channel, |
| 14 | + identifier: String, |
| 15 | + features: Vec<RuntimeFeature>, |
| 16 | + configs: Vec<AdapterConfiguration>, |
| 17 | +} |
| 18 | + |
| 19 | +impl DracoRuntimeStatusService { |
| 20 | + fn new( |
| 21 | + channel: Channel, |
| 22 | + identifier: String, |
| 23 | + features: Vec<RuntimeFeature>, |
| 24 | + configs: Vec<AdapterConfiguration>, |
| 25 | + ) -> Self { |
| 26 | + DracoRuntimeStatusService { |
| 27 | + channel, |
| 28 | + identifier, |
| 29 | + features, |
| 30 | + configs, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + async fn add_config(&mut self, feat: RuntimeFeature) { |
| 35 | + self.features.push(feat); |
| 36 | + } |
| 37 | + |
| 38 | + async fn update_runtime_status_by_status( |
| 39 | + &self, |
| 40 | + status: tucana::shared::adapter_runtime_status::Status, |
| 41 | + ) { |
| 42 | + log::info!("Updating the current Runtime Status!"); |
| 43 | + let mut client = RuntimeStatusServiceClient::new(self.channel.clone()); |
| 44 | + |
| 45 | + let now = SystemTime::now(); |
| 46 | + let timestamp = match now.duration_since(UNIX_EPOCH) { |
| 47 | + Ok(time) => time.as_secs(), |
| 48 | + Err(err) => { |
| 49 | + log::error!("cannot get current system time: {:?}", err); |
| 50 | + 0 |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + let request = RuntimeStatusUpdateRequest { |
| 55 | + status: Some(Status::AdapterRuntimeStatus(AdapterRuntimeStatus { |
| 56 | + status: status.into(), |
| 57 | + timestamp: timestamp as i64, |
| 58 | + identifier: self.identifier.clone(), |
| 59 | + features: self.features.clone(), |
| 60 | + configurations: self.configs.clone(), |
| 61 | + })), |
| 62 | + }; |
| 63 | + |
| 64 | + match client.update(request).await { |
| 65 | + Ok(response) => { |
| 66 | + log::info!( |
| 67 | + "Was the update of the RuntimeStatus accepted by Sagittarius? {}", |
| 68 | + response.into_inner().success |
| 69 | + ); |
| 70 | + } |
| 71 | + Err(err) => { |
| 72 | + log::error!("Failed to update RuntimeStatus: {:?}", err); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + async fn update_runtime_status(&self, status: AdapterRuntimeStatus) { |
| 77 | + log::info!("Updating the current Runtime Status!"); |
| 78 | + let mut client = RuntimeStatusServiceClient::new(self.channel.clone()); |
| 79 | + |
| 80 | + let request = RuntimeStatusUpdateRequest { |
| 81 | + status: Some(Status::AdapterRuntimeStatus(status)), |
| 82 | + }; |
| 83 | + |
| 84 | + match client.update(request).await { |
| 85 | + Ok(response) => { |
| 86 | + log::info!( |
| 87 | + "Was the update of the RuntimeStatus accepted by Sagittarius? {}", |
| 88 | + response.into_inner().success |
| 89 | + ); |
| 90 | + } |
| 91 | + Err(err) => { |
| 92 | + log::error!("Failed to update RuntimeStatus: {:?}", err); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments