circles/examples/server_timeout_demonstration.rs
2025-06-19 10:44:40 +03:00

184 lines
6.3 KiB
Rust

// Example: Timeout Demonstration for circle_server_ws
//
// This example demonstrates how circle_server_ws handles Rhai scripts that exceed
// the configured execution timeout (default 30 seconds).
//
// This example will attempt to start its own instance of circle_server_ws.
// Ensure circle_server_ws is compiled (cargo build --bin circle_server_ws).
use circle_client_ws::CircleWsClientBuilder;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use tokio::time::{sleep, Duration};
const EXAMPLE_SERVER_PORT: u16 = 8089; // Using a specific port for this example
const WS_URL: &str = "ws://127.0.0.1:8089/ws";
const CIRCLE_NAME_FOR_EXAMPLE: &str = "timeout_example_circle";
const CIRCLE_SERVER_WS_BIN_NAME: &str = "server_ws";
const SCRIPT_TIMEOUT_SECONDS: u64 = 30; // This is the server-side timeout we expect to hit
// RAII guard for cleaning up child processes
struct ChildProcessGuard {
child: Child,
name: String,
}
impl ChildProcessGuard {
fn new(child: Child, name: String) -> Self {
log::info!("{} process started with PID: {}", name, child.id());
Self { child, name }
}
}
impl Drop for ChildProcessGuard {
fn drop(&mut self) {
log::info!(
"Cleaning up {} process (PID: {})...",
self.name,
self.child.id()
);
match self.child.kill() {
Ok(_) => {
log::info!(
"Successfully sent kill signal to {} (PID: {}).",
self.name,
self.child.id()
);
match self.child.wait() {
Ok(status) => log::info!(
"{} (PID: {}) exited with status: {}",
self.name,
self.child.id(),
status
),
Err(e) => log::warn!(
"Error waiting for {} (PID: {}): {}",
self.name,
self.child.id(),
e
),
}
}
Err(e) => log::error!(
"Failed to kill {} (PID: {}): {}",
self.name,
self.child.id(),
e
),
}
}
}
fn find_target_bin_path(bin_name: &str) -> Result<PathBuf, String> {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map_err(|_| "CARGO_MANIFEST_DIR not set".to_string())?;
let workspace_root = PathBuf::from(manifest_dir)
.parent()
.ok_or("Failed to get workspace root")?
.to_path_buf();
let bin_path = workspace_root.join("target").join("debug").join(bin_name);
if !bin_path.exists() {
return Err(format!(
"Binary '{}' not found at {:?}. Ensure it's built.",
bin_name, bin_path
));
}
Ok(bin_path)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let server_bin_path = find_target_bin_path(CIRCLE_SERVER_WS_BIN_NAME)?;
log::info!("Found server binary at: {:?}", server_bin_path);
log::info!(
"Starting {} for circle '{}' on port {}...",
CIRCLE_SERVER_WS_BIN_NAME,
CIRCLE_NAME_FOR_EXAMPLE,
EXAMPLE_SERVER_PORT
);
let server_process = Command::new(&server_bin_path)
.args([
"--port",
&EXAMPLE_SERVER_PORT.to_string(),
"--circle-name",
CIRCLE_NAME_FOR_EXAMPLE,
])
.stdout(Stdio::piped()) // Pipe stdout to keep terminal clean, or Stdio::inherit() to see server logs
.stderr(Stdio::piped()) // Pipe stderr as well
.spawn()
.map_err(|e| {
format!(
"Failed to start {}: {}. Ensure it is built.",
CIRCLE_SERVER_WS_BIN_NAME, e
)
})?;
let _server_guard =
ChildProcessGuard::new(server_process, CIRCLE_SERVER_WS_BIN_NAME.to_string());
log::info!("Giving the server a moment to start up...");
sleep(Duration::from_secs(3)).await; // Wait for server to initialize
log::info!("Attempting to connect to WebSocket server at: {}", WS_URL);
let mut client = CircleWsClientBuilder::new(WS_URL.to_string()).build();
log::info!("Connecting client...");
if let Err(e) = client.connect().await {
log::error!("Failed to connect to WebSocket server: {}", e);
log::error!("Please check server logs if it failed to start correctly.");
return Err(e.into());
}
log::info!("Client connected successfully.");
// This Rhai script is designed to run for much longer than the typical server timeout.
let long_running_script = "
let mut x = 0;
for i in 0..9999999999 { // Extremely large loop
x = x + i;
}
// This part should not be reached if timeout works correctly.
print(x);
x
"
.to_string();
log::info!(
"Sending long-running script (expected to time out on server after ~{}s)...",
SCRIPT_TIMEOUT_SECONDS
);
match client.play(long_running_script).await {
Ok(play_result) => {
log::warn!(
"Received unexpected success from play request: {:?}",
play_result
);
log::warn!("This might indicate the script finished faster than expected, or the timeout didn't trigger.");
}
Err(e) => {
log::info!("Received expected error from play request: {}", e);
log::info!("This demonstrates the server timing out the script execution.");
// You can further inspect the error details if CircleWsClientError provides them.
// For example, if e.to_string().contains('code: -32002' or 'timed out'.
if e.to_string().contains("timed out") || e.to_string().contains("-32002") {
log::info!("Successfully received timeout error from the server!");
} else {
log::warn!(
"Received an error, but it might not be the expected timeout error: {}",
e
);
}
}
}
log::info!("Disconnecting client...");
client.disconnect().await;
log::info!("Client disconnected.");
log::info!("Timeout demonstration example finished.");
Ok(())
}