From daf44fe1c296bdcab224a7d02c4bc90d1ad18f28 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 28 Sep 2023 12:21:11 +0700 Subject: [PATCH] More changes to keep threads alive over reconnect --- src/common/mdthread.rs | 50 +++++++++++++++++++++++------------------- src/common/neocam.rs | 1 + 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/common/mdthread.rs b/src/common/mdthread.rs index f087a2a..b83700e 100644 --- a/src/common/mdthread.rs +++ b/src/common/mdthread.rs @@ -8,12 +8,12 @@ use tokio::{ oneshot::Sender as OneshotSender, watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender}, }, - time::Instant, + time::{sleep, Duration, Instant}, }; use tokio_util::sync::CancellationToken; use super::NeoInstance; -use crate::Result; +use crate::{AnyResult, Result}; use neolink_core::bc_protocol::MotionStatus; pub(crate) enum MdState { @@ -64,29 +64,35 @@ impl NeoCamMdThread { } Ok(()) } => v, - v = md_instance.run_passive_task(|cam| { - let watcher = watcher.clone(); - Box::pin( - async move { - let mut md = cam.listen_on_motion().await?; - loop { - let event = md.next_motion().await?; - match event { - MotionStatus::Start(at) => { - watcher.send_replace( - MdState::Start(at.into()) - ); + v = async { + loop { + let r: AnyResult<()> = md_instance.run_passive_task(|cam| { + let watcher = watcher.clone(); + Box::pin( + async move { + let mut md = cam.listen_on_motion().await?; + loop { + let event = md.next_motion().await?; + match event { + MotionStatus::Start(at) => { + watcher.send_replace( + MdState::Start(at.into()) + ); + } + MotionStatus::Stop(at) => { + watcher.send_replace( + MdState::Stop(at.into()) + ); + } + MotionStatus::NoChange(_) => {}, } - MotionStatus::Stop(at) => { - watcher.send_replace( - MdState::Stop(at.into()) - ); - } - MotionStatus::NoChange(_) => {}, } } - } - )}) => v + )}).await; + log::debug!("Error in MD task Restarting: {:?}", r); + sleep(Duration::from_secs(1)).await; + } + } => v } } } diff --git a/src/common/neocam.rs b/src/common/neocam.rs index 7e3b1f3..e615267 100644 --- a/src/common/neocam.rs +++ b/src/common/neocam.rs @@ -4,6 +4,7 @@ //! Shared stream BC delivery //! Common restart code //! Clonable interface to share amongst threadsanyhow::anyhow; +use anyhow::Context; use futures::{stream::StreamExt, TryFutureExt}; use std::sync::Weak; use tokio::{