Skip to content

Commit ff4501f

Browse files
committed
feat: added execute function that differences between pub/sub and request/respond
1 parent b482bec commit ff4501f

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

crates/base/src/store.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use prost::Message;
66
use tucana::shared::{ExecutionFlow, ValidationFlow, Value};
77

88
pub struct AdapterStore {
9-
client: async_nats::Client,
10-
kv: async_nats::jetstream::kv::Store,
9+
pub client: async_nats::Client,
10+
pub kv: async_nats::jetstream::kv::Store,
1111
}
1212

1313
pub enum FlowIdentifyResult {
@@ -56,7 +56,7 @@ impl AdapterStore {
5656
/// - id: The identifier to use for identifying the possible matches. Its just a fine grain identifier that can be used to identify the possible matches. For a REST Flow this will be the regex matcher, for a CRON Flow the trait just return true every time.
5757
///
5858
/// Returns:
59-
/// - FlowIdenfiyResult: The result of the flow identification process. This can be one of the following:
59+
/// - FlowIdentifyResult: The result of the flow identification process. This can be one of the following:
6060
/// - None: No flows matched the identifier.
6161
/// - Single(ValidationFlow): A single flow matched the identifier.
6262
/// - Multiple(Vec<ValidationFlow>): Multiple flows matched the identifier.
@@ -101,6 +101,36 @@ impl AdapterStore {
101101
}
102102
}
103103

104+
pub async fn execute(&self, flow: ExecutionFlow, wait_for_result: bool) -> Option<Value> {
105+
let uuid = uuid::Uuid::new_v4().to_string();
106+
let topic = format!("execution.{}", uuid);
107+
let bytes = flow.encode_to_vec();
108+
109+
if wait_for_result {
110+
match self.client.request(topic, bytes.into()).await {
111+
Ok(message) => match Value::decode(message.payload) {
112+
Ok(value) => Some(value),
113+
Err(err) => {
114+
log::error!("Failed to decode response from NATS server: {:?}", err);
115+
None
116+
}
117+
},
118+
Err(err) => {
119+
log::error!("Failed to send request to NATS server: {:?}", err);
120+
None
121+
}
122+
}
123+
} else {
124+
match self.client.publish(topic, bytes.into()).await {
125+
Ok(_) => None,
126+
Err(err) => {
127+
log::error!("Failed to send request to NATS server: {:?}", err);
128+
None
129+
}
130+
}
131+
}
132+
}
133+
104134
/// validate_and_execute_flow
105135
///
106136
/// This function will validate the flow. If the flow is valid, it will execute (send the flow to the execution and wait for a/multiple result/s) the flow.
@@ -124,25 +154,8 @@ impl AdapterStore {
124154
};
125155
}
126156

127-
let uuid = uuid::Uuid::new_v4().to_string();
128157
let execution_flow: ExecutionFlow = Self::convert_validation_flow(flow, input_value);
129-
let bytes = execution_flow.encode_to_vec();
130-
let topic = format!("execution.{}", uuid);
131-
let result = self.client.request(topic, bytes.into()).await;
132-
133-
match result {
134-
Ok(message) => match Value::decode(message.payload) {
135-
Ok(value) => Some(value),
136-
Err(err) => {
137-
log::error!("Failed to decode response from NATS server: {:?}", err);
138-
None
139-
}
140-
},
141-
Err(err) => {
142-
log::error!("Failed to send request to NATS server: {:?}", err);
143-
None
144-
}
145-
}
158+
self.execute(execution_flow, true).await
146159
}
147160

148161
fn convert_validation_flow(flow: ValidationFlow, input_value: Option<Value>) -> ExecutionFlow {

0 commit comments

Comments
 (0)