|
13 | 13 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
14 | 14 | // specific language governing permissions and limitations under the License. |
15 | 15 |
|
16 | | -use polaris_rust::core::model::error::PolarisError; |
| 16 | +use std::{collections::HashMap, sync::Arc, time::Duration}; |
| 17 | + |
| 18 | +use polaris_rust::{ |
| 19 | + config::{ |
| 20 | + api::{new_config_file_api_by_context, ConfigFileAPI}, |
| 21 | + req::{ |
| 22 | + CreateConfigFileRequest, PublishConfigFileRequest, UpdateConfigFileRequest, |
| 23 | + UpsertAndPublishConfigFileRequest, WatchConfigFileRequest, |
| 24 | + }, |
| 25 | + }, |
| 26 | + core::{ |
| 27 | + context::SDKContext, |
| 28 | + model::{ |
| 29 | + config::{ConfigFile, ConfigFileRelease}, |
| 30 | + error::PolarisError, |
| 31 | + }, |
| 32 | + }, |
| 33 | +}; |
| 34 | +use tracing::level_filters::LevelFilter; |
17 | 35 |
|
18 | 36 | #[tokio::main] |
19 | 37 | async fn main() -> Result<(), PolarisError> { |
| 38 | + tracing_subscriber::fmt() |
| 39 | + // all spans/events with a level higher than TRACE (e.g, info, warn, etc.) |
| 40 | + // will be written to stdout. |
| 41 | + .with_thread_names(true) |
| 42 | + .with_file(true) |
| 43 | + .with_level(true) |
| 44 | + .with_line_number(true) |
| 45 | + .with_thread_ids(true) |
| 46 | + .with_max_level(LevelFilter::INFO) |
| 47 | + // sets this to be the default, global collector for this application. |
| 48 | + .init(); |
| 49 | + |
| 50 | + let start_time = std::time::Instant::now(); |
| 51 | + |
| 52 | + let sdk_context_ret = SDKContext::default(); |
| 53 | + if sdk_context_ret.is_err() { |
| 54 | + tracing::error!( |
| 55 | + "create sdk context fail: {}", |
| 56 | + sdk_context_ret.err().unwrap() |
| 57 | + ); |
| 58 | + return Err(PolarisError::new( |
| 59 | + polaris_rust::core::model::error::ErrorCode::UnknownServerError, |
| 60 | + "".to_string(), |
| 61 | + )); |
| 62 | + } |
| 63 | + let arc_ctx = Arc::new(sdk_context_ret.unwrap()); |
| 64 | + |
| 65 | + let config_file_api_ret = new_config_file_api_by_context(arc_ctx.clone()); |
| 66 | + if config_file_api_ret.is_err() { |
| 67 | + tracing::error!( |
| 68 | + "create config_file api fail: {}", |
| 69 | + config_file_api_ret.err().unwrap() |
| 70 | + ); |
| 71 | + return Err(PolarisError::new( |
| 72 | + polaris_rust::core::model::error::ErrorCode::UnknownServerError, |
| 73 | + "".to_string(), |
| 74 | + )); |
| 75 | + } |
| 76 | + |
| 77 | + tracing::info!( |
| 78 | + "create config_file api client cost: {:?}", |
| 79 | + start_time.elapsed() |
| 80 | + ); |
| 81 | + |
| 82 | + let config_file_api = config_file_api_ret.unwrap(); |
| 83 | + |
| 84 | + let mut labels = HashMap::<String, String>::new(); |
| 85 | + |
| 86 | + labels.insert("rust".to_string(), "rust".to_string()); |
| 87 | + |
| 88 | + // 创建文件 |
| 89 | + let ret = config_file_api |
| 90 | + .create_config_file(CreateConfigFileRequest { |
| 91 | + flow_id: uuid::Uuid::new_v4().to_string(), |
| 92 | + timeout: Duration::from_secs(1), |
| 93 | + file: ConfigFile { |
| 94 | + namespace: "rust".to_string(), |
| 95 | + group: "rust".to_string(), |
| 96 | + name: "rust.toml".to_string(), |
| 97 | + content: "test".to_string(), |
| 98 | + labels: labels.clone(), |
| 99 | + ..Default::default() |
| 100 | + }, |
| 101 | + }) |
| 102 | + .await; |
| 103 | + |
| 104 | + if ret.is_err() { |
| 105 | + tracing::error!("create config_file fail: {}", ret.err().unwrap()); |
| 106 | + return Err(PolarisError::new( |
| 107 | + polaris_rust::core::model::error::ErrorCode::UnknownServerError, |
| 108 | + "".to_string(), |
| 109 | + )); |
| 110 | + } |
| 111 | + |
| 112 | + // 更新文件 |
| 113 | + let ret = config_file_api |
| 114 | + .update_config_file(UpdateConfigFileRequest { |
| 115 | + flow_id: uuid::Uuid::new_v4().to_string(), |
| 116 | + timeout: Duration::from_secs(1), |
| 117 | + file: ConfigFile { |
| 118 | + namespace: "rust".to_string(), |
| 119 | + group: "rust".to_string(), |
| 120 | + name: "rust.toml".to_string(), |
| 121 | + content: "test".to_string(), |
| 122 | + labels: labels.clone(), |
| 123 | + ..Default::default() |
| 124 | + }, |
| 125 | + }) |
| 126 | + .await; |
| 127 | + |
| 128 | + if ret.is_err() { |
| 129 | + tracing::error!("update config_file fail: {}", ret.err().unwrap()); |
| 130 | + return Err(PolarisError::new( |
| 131 | + polaris_rust::core::model::error::ErrorCode::UnknownServerError, |
| 132 | + "".to_string(), |
| 133 | + )); |
| 134 | + } |
| 135 | + |
| 136 | + // 发布文件 |
| 137 | + let ret = config_file_api |
| 138 | + .publish_config_file(PublishConfigFileRequest { |
| 139 | + flow_id: uuid::Uuid::new_v4().to_string(), |
| 140 | + timeout: Duration::from_secs(1), |
| 141 | + config_file: ConfigFileRelease { |
| 142 | + namespace: "rust".to_string(), |
| 143 | + group: "rust".to_string(), |
| 144 | + file_name: "rust.toml".to_string(), |
| 145 | + release_name: "rust".to_string(), |
| 146 | + md5: "".to_string(), |
| 147 | + }, |
| 148 | + }) |
| 149 | + .await; |
| 150 | + |
| 151 | + if ret.is_err() { |
| 152 | + tracing::error!("publish config_file fail: {}", ret.err().unwrap()); |
| 153 | + return Err(PolarisError::new( |
| 154 | + polaris_rust::core::model::error::ErrorCode::UnknownServerError, |
| 155 | + "".to_string(), |
| 156 | + )); |
| 157 | + } |
| 158 | + |
| 159 | + // 文件变更订阅 |
| 160 | + let _ = config_file_api |
| 161 | + .watch_config_file(WatchConfigFileRequest { |
| 162 | + namespace: "rust".to_string(), |
| 163 | + group: "rust".to_string(), |
| 164 | + file: "rust.toml".to_string(), |
| 165 | + call_back: Arc::new(|event| { |
| 166 | + tracing::info!("event: {:?}", event); |
| 167 | + }), |
| 168 | + }) |
| 169 | + .await; |
| 170 | + |
| 171 | + // 变更 10 次配置文件并发布 |
| 172 | + for i in 0..10 { |
| 173 | + let ret = config_file_api |
| 174 | + .upsert_publish_config_file(UpsertAndPublishConfigFileRequest { |
| 175 | + flow_id: uuid::Uuid::new_v4().to_string(), |
| 176 | + timeout: Duration::from_secs(1), |
| 177 | + release_name: format!("rust-{}", i), |
| 178 | + md5: "".to_string(), |
| 179 | + config_file: ConfigFile { |
| 180 | + namespace: "rust".to_string(), |
| 181 | + group: "rust".to_string(), |
| 182 | + name: "rust.toml".to_string(), |
| 183 | + content: format!("test-{}", i), |
| 184 | + labels: labels.clone(), |
| 185 | + ..Default::default() |
| 186 | + }, |
| 187 | + }) |
| 188 | + .await; |
| 189 | + |
| 190 | + if ret.is_err() { |
| 191 | + tracing::error!( |
| 192 | + "upsert and publish config_file fail: {}", |
| 193 | + ret.err().unwrap() |
| 194 | + ); |
| 195 | + return Err(PolarisError::new( |
| 196 | + polaris_rust::core::model::error::ErrorCode::UnknownServerError, |
| 197 | + "".to_string(), |
| 198 | + )); |
| 199 | + } |
| 200 | + |
| 201 | + std::thread::sleep(Duration::from_secs(10)); |
| 202 | + } |
| 203 | + |
20 | 204 | Ok(()) |
21 | 205 | } |
0 commit comments