zenoh_flow/runtime/dataflow/instance/runners/
mod.rs1pub mod connector;
16
17use crate::traits::Node;
18use crate::zfresult::Error;
19use crate::Result as ZFResult;
20use async_std::task::JoinHandle;
21use futures::future::{AbortHandle, Abortable, Aborted};
22use std::sync::Arc;
23use std::time::Instant;
24
25#[derive(Clone, Debug, PartialEq, Eq)]
29pub enum RunnerKind {
30 Source,
31 Operator,
32 Sink,
33 Connector,
34}
35
36pub enum RunAction {
38 RestartRun(Option<Error>),
39 Stop,
40}
41
42pub(crate) struct Runner {
46 pub(crate) node: Arc<dyn Node>,
47 pub(crate) run_loop_handle: Option<JoinHandle<Result<Error, Aborted>>>,
48 pub(crate) run_loop_abort_handle: Option<AbortHandle>,
49}
50
51impl Runner {
52 pub(crate) fn new(node: Arc<dyn Node>) -> Self {
53 Self {
54 node,
55 run_loop_handle: None,
56 run_loop_abort_handle: None,
57 }
58 }
59
60 pub(crate) fn start(&mut self) {
64 if self.is_running() {
65 log::warn!("Called `start` while node is ALREADY running. Returning.");
66 return;
67 }
68
69 let node = self.node.clone();
70 let run_loop = async move {
71 let mut instant: Instant;
72 loop {
73 instant = Instant::now();
74 log::trace!("Iteration start: {:?}", instant);
75 if let Err(e) = node.iteration().await {
76 log::error!("Iteration error: {:?}", e);
77 return e;
78 }
79
80 log::trace!("iteration took: {}ms", instant.elapsed().as_millis());
81
82 async_std::task::yield_now().await;
83 }
84 };
85
86 let (abort_handle, abort_registration) = AbortHandle::new_pair();
87 let handle = async_std::task::spawn(Abortable::new(run_loop, abort_registration));
88
89 self.run_loop_handle = Some(handle);
90 self.run_loop_abort_handle = Some(abort_handle);
91 }
92
93 pub(crate) async fn stop(&mut self) -> ZFResult<()> {
101 if !self.is_running() {
102 log::warn!("Called `stop` while node is NOT running. Returning.");
103 return Ok(()); }
105
106 if let Some(abort_handle) = self.run_loop_abort_handle.take() {
107 abort_handle.abort();
108 if let Some(handle) = self.run_loop_handle.take() {
109 log::trace!("Handler finished with {:?}", handle.await);
110 }
111 }
112
113 Ok(())
114 }
115
116 pub(crate) fn is_running(&self) -> bool {
121 self.run_loop_handle.is_some()
122 }
123}