|
1 | 1 | use std::path::Path; |
2 | 2 |
|
3 | | -use async_nats::Client; |
4 | | -use async_trait::async_trait; |
5 | 3 | use clap::{Parser, arg, command}; |
6 | | -use log::{error, info}; |
7 | | -use prost::Message; |
| 4 | +use log::error; |
| 5 | +use log::info; |
8 | 6 | use serde::Deserialize; |
9 | 7 | use taurus_core::runtime::engine::ExecutionEngine; |
10 | | -use taurus_core::runtime::remote::{RemoteExecution, RemoteRuntime}; |
11 | | -use taurus_core::types::errors::runtime_error::RuntimeError; |
12 | | -use tucana::aquila::ExecutionResult; |
| 8 | +use taurus_core::types::signal::Signal; |
| 9 | +use taurus_provider::providers::emitter::nats_emitter::NATSRespondEmitter; |
| 10 | +use taurus_provider::providers::remote::nats_remote_runtime::NATSRemoteRuntime; |
| 11 | +use tucana::shared::ValidationFlow; |
13 | 12 | use tucana::shared::helper::value::from_json_value; |
14 | 13 | use tucana::shared::helper::value::to_json_value; |
15 | | -use tucana::shared::{ValidationFlow, Value}; |
16 | 14 |
|
17 | 15 | #[derive(Clone, Deserialize)] |
18 | 16 | pub struct Input { |
@@ -115,78 +113,6 @@ impl Cases { |
115 | 113 | get_test_cases(path) |
116 | 114 | } |
117 | 115 | } |
118 | | -pub struct RemoteNatsClient { |
119 | | - client: Client, |
120 | | -} |
121 | | - |
122 | | -impl RemoteNatsClient { |
123 | | - pub fn new(client: Client) -> Self { |
124 | | - RemoteNatsClient { client } |
125 | | - } |
126 | | -} |
127 | | - |
128 | | -#[async_trait] |
129 | | -impl RemoteRuntime for RemoteNatsClient { |
130 | | - async fn execute_remote(&self, execution: RemoteExecution) -> Result<Value, RuntimeError> { |
131 | | - let topic = format!( |
132 | | - "action.{}.{}", |
133 | | - execution.target_service, execution.request.execution_identifier |
134 | | - ); |
135 | | - let payload = execution.request.encode_to_vec(); |
136 | | - let res = self.client.request(topic.clone(), payload.into()).await; |
137 | | - log::info!("Publishing to topic: {}", topic); |
138 | | - let message = match res { |
139 | | - Ok(r) => r, |
140 | | - Err(err) => { |
141 | | - log::error!( |
142 | | - "RemoteRuntimeExeption: failed to handle NATS message: {}", |
143 | | - err |
144 | | - ); |
145 | | - return Err(RuntimeError::new( |
146 | | - "T-RMT-000001", |
147 | | - "RemoteRuntimeExeption", |
148 | | - "Failed to receive any response messages from a remote runtime.", |
149 | | - )); |
150 | | - } |
151 | | - }; |
152 | | - |
153 | | - let decode_result = ExecutionResult::decode(message.payload); |
154 | | - let execution_result = match decode_result { |
155 | | - Ok(r) => r, |
156 | | - Err(err) => { |
157 | | - log::error!( |
158 | | - "RemoteRuntimeExeption: failed to decode NATS message: {}", |
159 | | - err |
160 | | - ); |
161 | | - return Err(RuntimeError::new( |
162 | | - "T-RMT-000002", |
163 | | - "RemoteRuntimeExeption", |
164 | | - "Failed to read Remote Response", |
165 | | - )); |
166 | | - } |
167 | | - }; |
168 | | - |
169 | | - match execution_result.result { |
170 | | - Some(result) => match result { |
171 | | - tucana::aquila::execution_result::Result::Success(value) => Ok(value), |
172 | | - tucana::aquila::execution_result::Result::Error(err) => { |
173 | | - let code = err.code.to_string(); |
174 | | - let description = match err.description { |
175 | | - Some(string) => string, |
176 | | - None => "Unknown Error".to_string(), |
177 | | - }; |
178 | | - let error = RuntimeError::new(code, "RemoteExecutionError", description); |
179 | | - Err(error) |
180 | | - } |
181 | | - }, |
182 | | - None => Err(RuntimeError::new( |
183 | | - "T-RMT-000003", |
184 | | - "RemoteRuntimeExeption", |
185 | | - "Result of Remote Response was empty.", |
186 | | - )), |
187 | | - } |
188 | | - } |
189 | | -} |
190 | 116 |
|
191 | 117 | #[derive(clap::Parser, Debug)] |
192 | 118 | #[command(author, version, about)] |
@@ -233,35 +159,37 @@ async fn main() { |
233 | 159 | panic!("Failed to connect to NATS server: {}", err); |
234 | 160 | } |
235 | 161 | }; |
236 | | - let remote = RemoteNatsClient::new(client); |
| 162 | + let remote = NATSRemoteRuntime::new(client.clone()); |
| 163 | + let emitter = NATSRespondEmitter::new(client); |
237 | 164 | let engine = ExecutionEngine::new(); |
238 | 165 | let (result, _) = engine.execute_graph( |
239 | 166 | case.flow.starting_node_id, |
240 | 167 | case.flow.node_functions.clone(), |
241 | 168 | flow_input, |
242 | 169 | Some(&remote), |
243 | | - None, |
244 | | - true, |
| 170 | + Some(&emitter), |
| 171 | + false, |
245 | 172 | ); |
| 173 | + emitter.shutdown().await; |
246 | 174 |
|
247 | 175 | match result { |
248 | | - taurus_core::types::signal::Signal::Success(value) => { |
| 176 | + Signal::Success(value) => { |
249 | 177 | let json = to_json_value(value); |
250 | 178 | let pretty = serde_json::to_string_pretty(&json).unwrap(); |
251 | 179 | println!("{}", pretty); |
252 | 180 | } |
253 | | - taurus_core::types::signal::Signal::Return(value) => { |
| 181 | + Signal::Return(value) => { |
254 | 182 | let json = to_json_value(value); |
255 | 183 | let pretty = serde_json::to_string_pretty(&json).unwrap(); |
256 | 184 | println!("{}", pretty); |
257 | 185 | } |
258 | | - taurus_core::types::signal::Signal::Respond(value) => { |
| 186 | + Signal::Respond(value) => { |
259 | 187 | let json = to_json_value(value); |
260 | 188 | let pretty = serde_json::to_string_pretty(&json).unwrap(); |
261 | 189 | println!("{}", pretty); |
262 | 190 | } |
263 | | - taurus_core::types::signal::Signal::Stop => println!("Received Stop signal"), |
264 | | - taurus_core::types::signal::Signal::Failure(runtime_error) => { |
| 191 | + Signal::Stop => println!("Received Stop signal"), |
| 192 | + Signal::Failure(runtime_error) => { |
265 | 193 | println!("RuntimeError: {:?}", runtime_error); |
266 | 194 | } |
267 | 195 | } |
|
0 commit comments