Skip to content

Commit f4e372b

Browse files
Merge pull request #107 from code0-tech/105-shutdown-on-sigterm
Shutdown on Sigterm
2 parents 280b1c9 + 8e95c00 commit f4e372b

12 files changed

Lines changed: 663 additions & 237 deletions

File tree

taurus/src/context/argument.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ pub trait TryFromArgument: Sized {
2424
fn try_from_argument(a: &Argument) -> Result<Self, Signal>;
2525
}
2626

27-
fn type_err(msg: &str) -> Signal {
27+
fn type_err(msg: &str, a: &Argument) -> Signal {
2828
Signal::Failure(RuntimeError::simple(
2929
"InvalidArgumentRuntimeError",
30-
msg.to_string(),
30+
format!("{} but it was the arugment: {:?}", msg, a),
3131
))
3232
}
3333

3434
impl TryFromArgument for Value {
3535
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
3636
match a {
3737
Argument::Eval(v) => Ok(v.clone()),
38-
_ => Err(type_err("Expected evaluated value but got lazy thunk")),
38+
_ => Err(type_err("Expected evaluated value but got lazy thunk", a)),
3939
}
4040
}
4141
}
@@ -46,7 +46,7 @@ impl TryFromArgument for f64 {
4646
Argument::Eval(Value {
4747
kind: Some(Kind::NumberValue(n)),
4848
}) => Ok(*n),
49-
_ => Err(type_err("Expected number")),
49+
_ => Err(type_err("Expected number", a)),
5050
}
5151
}
5252
}
@@ -57,7 +57,7 @@ impl TryFromArgument for bool {
5757
Argument::Eval(Value {
5858
kind: Some(Kind::BoolValue(b)),
5959
}) => Ok(*b),
60-
_ => Err(type_err("Expected boolean")),
60+
_ => Err(type_err("Expected boolean", a)),
6161
}
6262
}
6363
}
@@ -68,7 +68,7 @@ impl TryFromArgument for String {
6868
Argument::Eval(Value {
6969
kind: Some(Kind::StringValue(s)),
7070
}) => Ok(s.clone()),
71-
_ => Err(type_err("Expected string")),
71+
_ => Err(type_err("Expected string", a)),
7272
}
7373
}
7474
}
@@ -79,7 +79,7 @@ impl TryFromArgument for Struct {
7979
Argument::Eval(Value {
8080
kind: Some(Kind::StructValue(s)),
8181
}) => Ok(s.clone()),
82-
_ => Err(type_err("Expected struct")),
82+
_ => Err(type_err("Expected struct", a)),
8383
}
8484
}
8585
}
@@ -90,9 +90,9 @@ impl TryFromArgument for ListValue {
9090
Argument::Eval(Value {
9191
kind: Some(Kind::ListValue(list)),
9292
}) => Ok(list.clone()),
93-
_ => Err(Signal::Failure(RuntimeError::simple_str(
93+
_ => Err(Signal::Failure(RuntimeError::simple(
9494
"InvalidArgumentRuntimeError",
95-
"Expected array (ListValue)",
95+
format!("Expected array (ListValue) but it was: {:?}", a),
9696
))),
9797
}
9898
}

taurus/src/context/executor.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,44 @@ use crate::context::context::{Context, ContextResult};
33
use crate::context::registry::FunctionStore;
44
use crate::context::signal::Signal;
55
use crate::error::RuntimeError;
6-
use std::cell::RefCell;
76
use std::collections::HashMap;
87
use tucana::shared::NodeFunction;
98

109
pub struct Executor<'a> {
1110
functions: &'a FunctionStore,
1211
nodes: HashMap<i64, NodeFunction>,
13-
context: RefCell<Context>,
1412
}
1513

1614
impl<'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
}

taurus/src/context/macros.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ macro_rules! args {
2020
$ty as $crate::context::argument::TryFromArgument
2121
>::try_from_argument(& $args_ident[__i]) {
2222
Ok(v) => v,
23-
Err(sig) => return sig,
23+
Err(sig) => {
24+
log::debug!(
25+
"Failed to parse argument '{}' (index {}, type {})",
26+
stringify!($name),
27+
__i,
28+
::core::any::type_name::<$ty>(),
29+
);
30+
return sig;
31+
}
2432
};
2533
__i += 1;
2634
)+

taurus/src/context/registry.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ use std::collections::HashMap;
77
/// - For eager params, the executor will already convert them to Argument::Eval(Value).
88
/// - For lazy params, the executor will pass Argument::Thunk(node_id).
99
/// - If a handler wants to execute a lazy arg, it calls run(node_id).
10-
pub type HandlerFn =
11-
fn(args: &[Argument], ctx: &mut Context, run: &mut dyn FnMut(i64) -> Signal) -> Signal;
10+
pub type HandlerFn = fn(
11+
args: &[Argument],
12+
ctx: &mut Context,
13+
run: &mut dyn FnMut(i64, &mut Context) -> Signal,
14+
) -> Signal;
1215

1316
pub struct HandlerFunctionEntry {
1417
pub handler: HandlerFn,

0 commit comments

Comments
 (0)