Skip to content

Commit

Permalink
use config_error flag and content event for notify
Browse files Browse the repository at this point in the history
Signed-off-by: Sahil Yeole <[email protected]>
  • Loading branch information
beelchester committed Aug 15, 2024
1 parent 3fa539e commit 2cc5c49
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/cli/server/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,14 @@ impl Server {
if let Some(receiver) = rec {
tokio::select! {
_ = receiver.recv() => {
tracing::info!("Server shutdown signal received");
runtime.shutdown_background();
tracing::info!("Server shutdown complete");
}
_ = handle => {
tracing::info!("Server completed without shutdown signal");
tracing::debug!("Server completed without shutdown signal");
}
}
} else {
handle.await?;
let _ = handle.await?;
}
Ok(())
}
Expand Down
20 changes: 11 additions & 9 deletions src/cli/tc/start.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::sync::Arc;

use anyhow::{Context, Result};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex};
use tokio::sync::broadcast;
use tokio::task;

use super::helpers::log_endpoint_set;
Expand Down Expand Up @@ -57,12 +58,12 @@ async fn start_watch_server(
}

loop {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
match watch_rx.recv() {
Ok(event) => {
if let Ok(event) = event {
if let notify::EventKind::Modify(notify::event::ModifyKind::Data(_)) =
event.kind
if let notify::EventKind::Modify(notify::event::ModifyKind::Data(
notify::event::DataChange::Content,
)) = event.kind
{
tracing::info!("File change detected");
if let Err(err) = tx.send(()) {
Expand All @@ -73,6 +74,7 @@ async fn start_watch_server(
}
Err(e) => tracing::error!("Watch error: {:?}", e),
}
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
}
});

Expand All @@ -81,7 +83,7 @@ async fn start_watch_server(
let file_paths = file_paths.clone();
async move {
let mut rec = Some(&mut rx);
let shown_warning = Arc::new(Mutex::new(false));
let mut config_error = false;
loop {
match config_reader.read_all(&file_paths).await {
Ok(config_module) => {
Expand All @@ -91,14 +93,14 @@ async fn start_watch_server(
if let Err(err) = server.fork_start(rec.as_deref_mut()).await {
tracing::error!("Failed to start server: {}", err);
}
*shown_warning.lock().await = false;
config_error = false;
tracing::info!("Restarting server");
}
Err(err) => {
if !*shown_warning.lock().await {
if !config_error {
tracing::error!("Failed to read config files: {}", err);
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
*shown_warning.lock().await = true;
config_error = true;
}
}
}
Expand Down

0 comments on commit 2cc5c49

Please sign in to comment.