From e5238eebd2bc9cfbb508d7b69b3b84708bf184f7 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 27 Oct 2025 15:23:28 +0800 Subject: Add some debug logs --- .../vcs_actions/src/connection/action_service.rs | 66 ++++++++++++++++++---- 1 file changed, 56 insertions(+), 10 deletions(-) (limited to 'crates/vcs_actions/src/connection') diff --git a/crates/vcs_actions/src/connection/action_service.rs b/crates/vcs_actions/src/connection/action_service.rs index c302fd4..f76921e 100644 --- a/crates/vcs_actions/src/connection/action_service.rs +++ b/crates/vcs_actions/src/connection/action_service.rs @@ -1,7 +1,13 @@ -use std::{net::SocketAddr, path::PathBuf, sync::Arc}; +use std::{ + net::SocketAddr, + path::PathBuf, + sync::Arc, + time::{Duration, Instant}, +}; use action_system::{action::ActionContext, action_pool::ActionPool}; use cfg_file::config::ConfigFile; +use log::{error, info, warn}; use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; use tokio::{ net::{TcpListener, TcpStream}, @@ -67,11 +73,32 @@ fn build_server_future( let mut active_connections = 0; let mut shutdown_requested = false; - // Spawn task to handle Ctrl+C + // Spawn task to handle Ctrl+C with rapid exit detection let shutdown_tx_clone = shutdown_tx.clone(); spawn(async move { - if let Ok(()) = signal::ctrl_c().await { + let mut ctrl_c_count = 0; + let mut last_ctrl_c_time = Instant::now(); + + while let Ok(()) = signal::ctrl_c().await { + let now = Instant::now(); + + // Reset counter if more than 5 seconds have passed + if now.duration_since(last_ctrl_c_time) > Duration::from_secs(5) { + ctrl_c_count = 0; + } + + ctrl_c_count += 1; + last_ctrl_c_time = now; + let _ = shutdown_tx_clone.send(()).await; + + // If 3 Ctrl+C within 5 seconds, exit immediately + if ctrl_c_count >= 3 { + info!("Shutdown. (3/3)"); + std::process::exit(0); + } else { + info!("Ctrl + C to force shutdown. ({} / 3)", ctrl_c_count); + } } }); @@ -82,14 +109,16 @@ fn build_server_future( accept_result = listener.accept(), if !shutdown_requested => { match accept_result { Ok((stream, _addr)) => { - active_connections += 1; + info!("New connection accepted."); let _ = tx.send(1).await; let vault_clone = vault.clone(); let action_pool_clone = action_pool.clone(); let tx_clone = tx.clone(); + spawn(async move { process_connection(stream, vault_clone, action_pool_clone).await; + info!("A connection closed."); let _ = tx_clone.send(-1).await; }); } @@ -114,7 +143,10 @@ fn build_server_future( shutdown_requested = true; // If no active connections, break immediately if active_connections == 0 { + info!("No active connections. Shutting down."); break; + } else { + warn!("Cannot shutdown while active connections exist! ({} active)", active_connections); } } } @@ -131,8 +163,12 @@ async fn process_connection(stream: TcpStream, vault: Arc, action_pool: A let mut instance = ConnectionInstance::from(stream); // Read action name and action arguments - let Ok(msg) = instance.read_msgpack::().await else { - return; + let msg = match instance.read_msgpack::().await { + Ok(msg) => msg, + Err(e) => { + error!("Failed to read action message: {}", e); + return; + } }; // Build context @@ -141,11 +177,21 @@ async fn process_connection(stream: TcpStream, vault: Arc, action_pool: A // Insert vault into context let ctx = ctx.insert_arc(vault); + info!( + "Process action `{}` with argument `{}`", + msg.action_name, msg.action_args_json + ); + // Process action - let Ok(_result_json) = action_pool + let result = action_pool .process_json(&msg.action_name, ctx, msg.action_args_json) - .await - else { - return; + .await; + + match result { + Ok(_result_json) => {} + Err(e) => { + warn!("Failed to process action `{}`: {}", msg.action_name, e); + return; + } }; } -- cgit From 60219b20754dda7f560deb5e9e442d46e4636507 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 27 Oct 2025 17:50:35 +0800 Subject: update: Add server lock behavior to vault lifecycle Lock the vault before starting the server and unlock it during shutdown. --- crates/vcs_actions/src/connection/action_service.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'crates/vcs_actions/src/connection') diff --git a/crates/vcs_actions/src/connection/action_service.rs b/crates/vcs_actions/src/connection/action_service.rs index f76921e..a657408 100644 --- a/crates/vcs_actions/src/connection/action_service.rs +++ b/crates/vcs_actions/src/connection/action_service.rs @@ -31,6 +31,12 @@ pub async fn server_entry(vault_path: impl Into) -> Result<(), TcpTarge // Initialize the vault let vault: Arc = init_vault(vault_cfg, vault_path.into()).await?; + // Lock the vault + vault.lock().map_err(|e| { + error!("{}", e); + TcpTargetError::Locked(e.to_string()) + })?; + // Create ActionPool let action_pool: Arc = Arc::new(server_action_pool()); @@ -38,6 +44,9 @@ pub async fn server_entry(vault_path: impl Into) -> Result<(), TcpTarge let (_shutdown_rx, future) = build_server_future(vault.clone(), action_pool.clone(), listener); future.await?; // Start and block until shutdown + // Unlock the vault + vault.unlock()?; + Ok(()) } -- cgit From b772e12c22b87e1473d1002ef9b82d954d2239a7 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 27 Oct 2025 17:51:07 +0800 Subject: update: Add connection logs to show active count Include current connection count in connection open/close log messages for better monitoring of server load --- crates/vcs_actions/src/connection/action_service.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/vcs_actions/src/connection') diff --git a/crates/vcs_actions/src/connection/action_service.rs b/crates/vcs_actions/src/connection/action_service.rs index a657408..f787cae 100644 --- a/crates/vcs_actions/src/connection/action_service.rs +++ b/crates/vcs_actions/src/connection/action_service.rs @@ -118,7 +118,7 @@ fn build_server_future( accept_result = listener.accept(), if !shutdown_requested => { match accept_result { Ok((stream, _addr)) => { - info!("New connection accepted."); + info!("New connection. (now {})", active_connections); let _ = tx.send(1).await; let vault_clone = vault.clone(); @@ -127,7 +127,7 @@ fn build_server_future( spawn(async move { process_connection(stream, vault_clone, action_pool_clone).await; - info!("A connection closed."); + info!("A connection closed. (now {})", active_connections); let _ = tx_clone.send(-1).await; }); } -- cgit