@@ -13,71 +13,33 @@ use code0_flow::flow_config::mode::Mode::DYNAMIC;
1313use futures_lite:: StreamExt ;
1414use log:: error;
1515use prost:: Message ;
16- use std:: collections:: HashMap ;
1716use std:: sync:: Arc ;
1817use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
1918use std:: time:: { Duration , Instant } ;
20- use taurus_core:: context:: context:: Context ;
21- use taurus_core:: context:: executor:: Executor ;
22- use taurus_core:: context:: registry:: FunctionStore ;
23- use taurus_core:: context:: signal:: Signal ;
24- use taurus_core:: runtime:: error:: RuntimeError ;
19+ use taurus_core:: runtime:: engine:: { EmitType , ExecutionEngine , RespondEmitter } ;
20+ use taurus_core:: types:: signal:: Signal ;
2521use tokio:: signal;
2622use tokio:: sync:: mpsc;
2723use tokio:: time:: sleep;
2824use tonic_health:: pb:: health_server:: HealthServer ;
2925use tucana:: shared:: value:: Kind ;
30- use tucana:: shared:: {
31- ExecutionFlow , NodeFunction , RuntimeFeature , RuntimeUsage , Translation , Value ,
32- } ;
26+ use tucana:: shared:: { ExecutionFlow , RuntimeFeature , RuntimeUsage , Translation , Value } ;
3327
3428fn handle_message (
3529 flow : ExecutionFlow ,
36- store : & FunctionStore ,
30+ engine : & ExecutionEngine ,
3731 nats_remote : & RemoteNatsClient ,
38- respond_emitter : Option < & dyn Fn ( Value ) > ,
32+ respond_emitter : Option < & dyn RespondEmitter > ,
3933) -> ( Signal , RuntimeUsage ) {
4034 let start = Instant :: now ( ) ;
41- let mut context = match flow. input_value {
42- Some ( v) => {
43- log:: debug!( "Input Value for flow: {:?}" , v) ;
44- Context :: new ( v)
45- }
46- None => Context :: default ( ) ,
47- } ;
48-
49- if flow. node_functions . is_empty ( ) {
50- let duration_millis = start. elapsed ( ) . as_millis ( ) as i64 ;
51- return (
52- Signal :: Failure ( RuntimeError :: simple_str (
53- "InvalidFlow" ,
54- "This flow has no nodes to execute!" ,
55- ) ) ,
56- RuntimeUsage {
57- flow_id : flow. flow_id ,
58- duration : duration_millis,
59- } ,
60- ) ;
61- }
62-
63- let node_functions: HashMap < i64 , NodeFunction > = flow
64- . node_functions
65- . into_iter ( )
66- . map ( |node| ( node. database_id , node) )
67- . collect ( ) ;
68-
69- let mut executor = Executor :: new ( store, node_functions) . with_remote_runtime ( nats_remote) ;
70- if let Some ( emitter) = respond_emitter {
71- executor = executor. with_respond_emitter ( emitter) ;
72- }
73-
74- let signal = executor. execute ( flow. starting_node_id , & mut context, true ) ;
35+ let flow_id = flow. flow_id ;
36+ let ( signal, _reason) = engine. execute_flow ( flow, Some ( nats_remote) , respond_emitter, true ) ;
7537 let duration_millis = start. elapsed ( ) . as_millis ( ) as i64 ;
7638
7739 (
7840 signal,
7941 RuntimeUsage {
80- flow_id : flow . flow_id ,
42+ flow_id,
8143 duration : duration_millis,
8244 } ,
8345 )
@@ -92,7 +54,7 @@ async fn main() {
9254 load_env_file ( ) ;
9355
9456 let config = Config :: new ( ) ;
95- let store = FunctionStore :: default ( ) ;
57+ let engine = ExecutionEngine :: new ( ) ;
9658 let mut runtime_status_service: Option < TaurusRuntimeStatusService > = None ;
9759 let mut runtime_usage_service: Option < TaurusRuntimeUsageService > = None ;
9860
@@ -240,18 +202,23 @@ async fn main() {
240202 let emit_tx = respond_tx. clone ( ) ;
241203 let respond_count = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
242204 let respond_count_for_emitter = respond_count. clone ( ) ;
243- let respond_emitter = move |value : Value | {
244- respond_count_for_emitter. fetch_add ( 1 , Ordering :: Relaxed ) ;
245- if let Err ( err) = emit_tx. send ( value) {
246- log:: debug!(
247- "Dropped respond signal value because publisher is unavailable: {:?}" ,
248- err
249- ) ;
205+ let respond_emitter = move |emit_type : EmitType , value : Value | match emit_type {
206+ EmitType :: OngoingExec => {
207+ respond_count_for_emitter. fetch_add ( 1 , Ordering :: Relaxed ) ;
208+ if let Err ( err) = emit_tx. send ( value) {
209+ log:: debug!(
210+ "Dropped respond signal value because publisher is unavailable: {:?}" ,
211+ err
212+ ) ;
213+ }
250214 }
215+ EmitType :: StartingExec => log:: debug!( "Flow execution started" ) ,
216+ EmitType :: FinishedExec => log:: debug!( "Flow execution finished" ) ,
217+ EmitType :: FailedExec => log:: debug!( "Flow execution failed" ) ,
251218 } ;
252219
253220 let ( signal, runtime_usage) =
254- handle_message ( flow, & store , & nats_client, Some ( & respond_emitter) ) ;
221+ handle_message ( flow, & engine , & nats_client, Some ( & respond_emitter) ) ;
255222 drop ( respond_emitter) ;
256223 drop ( respond_tx) ;
257224
0 commit comments