use log::info; use hero_dispatcher::{DispatcherBuilder, DispatcherError, ScriptType}; use std::time::{Duration, Instant}; #[tokio::main] async fn main() -> Result<(), Box> { env_logger::builder() .filter_level(log::LevelFilter::Info) .init(); // Build the client using the new builder pattern let client = DispatcherBuilder::new() .caller_id("timeout-example-runner") .redis_url("redis://127.0.0.1/") .build()?; info!("Dispatcher created."); let script_content = r#" // This script will never be executed by a worker because the recipient does not exist. let x = 10; let y = x + 32; y "#; // The worker_id points to a worker queue that doesn't have a worker. let non_existent_recipient = "non_existent_worker_for_timeout_test"; let very_short_timeout = Duration::from_secs(2); info!( "Submitting script to non-existent recipient '{}' with a timeout of {:?}...", non_existent_recipient, very_short_timeout ); let start_time = Instant::now(); // Use the new JobBuilder let result = client .new_job() .script_type(ScriptType::HeroScript) .script(script_content) .timeout(very_short_timeout) .await_response() .await; match result { Ok(details) => { log::error!( "Timeout Example FAILED: Expected a timeout, but got Ok: {:?}", details ); Err("Expected timeout, but task completed successfully.".into()) } Err(e) => { let elapsed = start_time.elapsed(); info!("Timeout Example: Received error as expected: {}", e); info!("Elapsed time: {:?}", elapsed); match e { DispatcherError::Timeout(task_id) => { info!("Timeout Example PASSED: Correctly received DispatcherError::Timeout for task_id: {}", task_id); // Ensure the elapsed time is close to the timeout duration // Allow for some buffer for processing assert!( elapsed >= very_short_timeout && elapsed < very_short_timeout + Duration::from_secs(1), "Elapsed time {:?} should be close to timeout {:?}", elapsed, very_short_timeout ); info!( "Elapsed time {:?} is consistent with timeout duration {:?}.", elapsed, very_short_timeout ); Ok(()) } other_error => { log::error!( "Timeout Example FAILED: Expected DispatcherError::Timeout, but got other error: {:?}", other_error ); Err(format!( "Expected DispatcherError::Timeout, got other error: {:?}", other_error ) .into()) } } } } }