@@ -3,48 +3,44 @@ use crate::context::context::{Context, ContextResult};
33use crate :: context:: registry:: FunctionStore ;
44use crate :: context:: signal:: Signal ;
55use crate :: error:: RuntimeError ;
6- use std:: cell:: RefCell ;
76use std:: collections:: HashMap ;
87use tucana:: shared:: NodeFunction ;
98
109pub struct Executor < ' a > {
1110 functions : & ' a FunctionStore ,
1211 nodes : HashMap < i64 , NodeFunction > ,
13- context : RefCell < Context > ,
1412}
1513
1614impl < ' a > Executor < ' a > {
17- pub fn new (
18- functions : & ' a FunctionStore ,
19- nodes : HashMap < i64 , NodeFunction > ,
20- context : Context ,
21- ) -> Self {
22- Executor {
23- functions,
24- nodes,
25- context : RefCell :: new ( context) ,
26- }
15+ pub fn new ( functions : & ' a FunctionStore , nodes : HashMap < i64 , NodeFunction > ) -> Self {
16+ Executor { functions, nodes }
2717 }
2818
29- pub fn execute ( & self , starting_node_id : i64 ) -> Signal {
19+ pub fn execute ( & self , starting_node_id : i64 , ctx : & mut Context ) -> Signal {
3020 let mut current_node_id = starting_node_id;
3121
3222 loop {
3323 let node = match self . nodes . get ( & current_node_id) {
3424 None => {
35- return Signal :: Failure ( RuntimeError :: simple_str (
25+ return Signal :: Failure ( RuntimeError :: simple (
3626 "NodeNotFound" ,
37- "The node with the id was not found" ,
27+ format ! (
28+ "The node with the database id: {} was not found" ,
29+ current_node_id
30+ ) ,
3831 ) ) ;
3932 }
4033 Some ( n) => n. clone ( ) ,
4134 } ;
4235
4336 let entry = match self . functions . get ( node. runtime_function_id . as_str ( ) ) {
4437 None => {
45- return Signal :: Failure ( RuntimeError :: simple_str (
38+ return Signal :: Failure ( RuntimeError :: simple (
4639 "FunctionNotFound" ,
47- "The function was not found" ,
40+ format ! (
41+ "The function {} (database id: {}) was not found" ,
42+ node. runtime_function_id, node. database_id
43+ ) ,
4844 ) ) ;
4945 }
5046 Some ( f) => f,
@@ -57,7 +53,7 @@ impl<'a> Executor<'a> {
5753 None => {
5854 return Signal :: Failure ( RuntimeError :: simple_str (
5955 "NodeValueNotFound" ,
60- "Missing parameter value" ,
56+ "Missing parameter value: {} " ,
6157 ) ) ;
6258 }
6359 } ;
@@ -76,7 +72,6 @@ impl<'a> Executor<'a> {
7672 args. push ( Argument :: Eval ( val. clone ( ) ) )
7773 }
7874 tucana:: shared:: node_value:: Value :: ReferenceValue ( reference) => {
79- let mut ctx = self . context . borrow_mut ( ) ;
8075 let value = ctx. get ( reference. node_id ) ;
8176 match value {
8277 ContextResult :: Error ( runtime_error) => {
@@ -108,26 +103,38 @@ impl<'a> Executor<'a> {
108103 if matches ! ( mode, ParameterNode :: Eager )
109104 && let Argument :: Thunk ( id) = * a
110105 {
111- match self . execute ( id) {
112- Signal :: Success ( v) => * a = Argument :: Eval ( v) ,
113- s @ ( Signal :: Failure ( _)
114- | Signal :: Return ( _)
115- | Signal :: Respond ( _)
116- | Signal :: Stop ) => return s,
106+ match self . execute ( id, ctx) {
107+ Signal :: Success ( v) => {
108+ log:: debug!(
109+ "Successfully executed node with database id {}, resulted in value: {:?}" ,
110+ id,
111+ a
112+ ) ;
113+ * a = Argument :: Eval ( v)
114+ }
115+ Signal :: Failure ( err) => {
116+ log:: error!( "Failed to execute node with database id: {}" , id) ;
117+ return Signal :: Failure ( err) ;
118+ }
119+ s @ ( Signal :: Return ( _) | Signal :: Respond ( _) | Signal :: Stop ) => return s,
117120 }
118121 }
119122 }
120123
121- let mut run = |node_id : i64 | self . execute ( node_id) ;
122- let mut ctx = self . context . borrow_mut ( ) ;
123- let result = ( entry. handler ) ( & args, & mut ctx, & mut run) ;
124+ let mut run = |node_id : i64 , ctx : & mut Context | self . execute ( node_id, ctx) ;
125+ let result = ( entry. handler ) ( & args, ctx, & mut run) ;
124126
125127 match result {
126128 Signal :: Success ( value) => {
127129 if let Some ( next_node_id) = node. next_node_id {
128130 current_node_id = next_node_id;
129131 continue ;
130132 } else {
133+ log:: debug!(
134+ "Successfully executed node with database id {}, resulted in value: {:?}" ,
135+ current_node_id,
136+ value
137+ ) ;
131138 return Signal :: Success ( value) ;
132139 }
133140 }
0 commit comments