Skip to content

Commit 96f667c

Browse files
committed
Add a small notice to allow us to tell when we are running in C++ mode
1 parent 2f00d6f commit 96f667c

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

src/cpp/runtime/graph_executor.cpp

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <hgraph/runtime/graph_executor.h>
2+
#include <hgraph/runtime/record_replay.h>
3+
#include <hgraph/types/error_type.h>
24
#include <hgraph/types/graph.h>
35
#include <hgraph/types/node.h>
4-
#include <hgraph/types/error_type.h>
56
#include <hgraph/util/lifecycle.h>
6-
#include <hgraph/runtime/record_replay.h>
77

88
namespace hgraph
99
{
@@ -41,15 +41,14 @@ namespace hgraph
4141
.def_prop_ro("run_mode", &GraphExecutor::run_mode)
4242
.def_prop_ro("graph", &GraphExecutor::graph)
4343
.def("run", &GraphExecutor::run)
44-
.def("__str__", [](const GraphExecutor &self) {
45-
return fmt::format("GraphExecutor@{:p}[mode={}]",
46-
static_cast<const void *>(&self),
47-
static_cast<int>(self.run_mode()));
48-
})
44+
.def("__str__",
45+
[](const GraphExecutor &self) {
46+
return fmt::format("GraphExecutor@{:p}[mode={}]", static_cast<const void *>(&self),
47+
static_cast<int>(self.run_mode()));
48+
})
4949
.def("__repr__", [](const GraphExecutor &self) {
50-
return fmt::format("GraphExecutor@{:p}[mode={}]",
51-
static_cast<const void *>(&self),
52-
static_cast<int>(self.run_mode()));
50+
return fmt::format("GraphExecutor@{:p}[mode={}]", static_cast<const void *>(&self),
51+
static_cast<int>(self.run_mode()));
5352
});
5453

5554
nb::enum_<EvaluationMode>(m, "EvaluationMode")
@@ -81,6 +80,11 @@ namespace hgraph
8180
graph_ptr GraphExecutorImpl::graph() const { return _graph; }
8281

8382
void GraphExecutorImpl::run(const engine_time_t &start_time, const engine_time_t &end_time) {
83+
84+
auto now = std::chrono::system_clock::now();
85+
fmt::print("{} [CPP] Running graph [{}] start time: {} end time: {}\n", fmt::format("{:%Y-%m-%d %H:%M:%S}", now),
86+
(_graph ? *_graph->label() : std::string{"unknown"}), start_time, end_time);
87+
8488
if (end_time <= start_time) {
8589
if (end_time < start_time) {
8690
throw std::invalid_argument("End time cannot be before the start time");
@@ -102,43 +106,31 @@ namespace hgraph
102106
for (const auto &observer : _observers) { evaluationEngine->add_life_cycle_observer(observer); }
103107

104108
try {
105-
// Initialise the graph but do not dispose here; disposal is handled by GraphBuilder.release_instance in Python
106-
initialise_component(*_graph);
107-
// Use RAII; StartStopContext destructor will stop and set Python error if exception occurs
108-
{
109-
auto startStopContext = StartStopContext(*_graph);
110-
while (clock->evaluation_time() < end_time) { _evaluate(*evaluationEngine); }
111-
}
112-
// After StartStopContext destruction, check if a Python error was set during stop
113-
if (PyErr_Occurred()) {
114-
throw nb::python_error();
115-
}
109+
// Initialise the graph but do not dispose here; disposal is handled by GraphBuilder.release_instance in Python
110+
initialise_component(*_graph);
111+
// Use RAII; StartStopContext destructor will stop and set Python error if exception occurs
112+
{
113+
auto startStopContext = StartStopContext(*_graph);
114+
while (clock->evaluation_time() < end_time) { _evaluate(*evaluationEngine); }
115+
}
116+
// After StartStopContext destruction, check if a Python error was set during stop
117+
if (PyErr_Occurred()) { throw nb::python_error(); }
116118
} catch (const NodeException &e) {
117119
// Raise Python hgraph.NodeException constructed from C++ NodeException details
118120
try {
119-
nb::object hgraph_mod = nb::module_::import_("hgraph");
121+
nb::object hgraph_mod = nb::module_::import_("hgraph");
120122
nb::object py_node_exc_cls = hgraph_mod.attr("NodeException");
121-
nb::tuple args = nb::make_tuple(
122-
nb::cast(e.signature_name),
123-
nb::cast(e.label),
124-
nb::cast(e.wiring_path),
125-
nb::cast(e.error_msg),
126-
nb::cast(e.stack_trace),
127-
nb::cast(e.activation_back_trace),
128-
nb::cast(e.additional_context)
129-
);
123+
nb::tuple args =
124+
nb::make_tuple(nb::cast(e.signature_name), nb::cast(e.label), nb::cast(e.wiring_path), nb::cast(e.error_msg),
125+
nb::cast(e.stack_trace), nb::cast(e.activation_back_trace), nb::cast(e.additional_context));
130126
PyErr_SetObject(py_node_exc_cls.ptr(), args.ptr());
131-
} catch (...) {
132-
PyErr_SetString(PyExc_RuntimeError, e.what());
133-
}
127+
} catch (...) { PyErr_SetString(PyExc_RuntimeError, e.what()); }
134128
throw nb::python_error();
135129
} catch (const nb::python_error &e) {
136-
throw; // Preserve Python exception raised above
130+
throw; // Preserve Python exception raised above
137131
} catch (const std::exception &e) {
138132
// Preserve any active Python exception (e.g., hgraph.NodeException)
139-
if (PyErr_Occurred()) {
140-
throw nb::python_error();
141-
}
133+
if (PyErr_Occurred()) { throw nb::python_error(); }
142134
// Provide a clear message for unexpected exceptions
143135
std::string msg = std::string("Graph execution failed: ") + e.what();
144136
throw nb::builtin_exception(nb::exception_type::runtime_error, msg.c_str());
@@ -157,9 +149,7 @@ namespace hgraph
157149
} catch (const NodeException &e) {
158150
// Let NodeException propagate to nanobind exception translator
159151
throw;
160-
} catch (const nb::python_error &e) {
161-
throw;
162-
} catch (const std::exception &e) {
152+
} catch (const nb::python_error &e) { throw; } catch (const std::exception &e) {
163153
if (PyErr_Occurred()) { throw nb::python_error(); }
164154
std::string msg = std::string("Error in notify_before_evaluation: ") + e.what();
165155
throw nb::builtin_exception(nb::exception_type::runtime_error, msg.c_str());
@@ -169,9 +159,7 @@ namespace hgraph
169159
} catch (const NodeException &e) {
170160
// Let NodeException propagate to nanobind exception translator
171161
throw;
172-
} catch (const nb::python_error &e) {
173-
throw;
174-
} catch (const std::exception &e) {
162+
} catch (const nb::python_error &e) { throw; } catch (const std::exception &e) {
175163
if (PyErr_Occurred()) { throw nb::python_error(); }
176164
std::string msg = std::string("Graph evaluation failed: ") + e.what();
177165
throw nb::builtin_exception(nb::exception_type::runtime_error, msg.c_str());

0 commit comments

Comments
 (0)