stream_long_running/
stream_long_running.rs1use sandbox_rs::{SandboxBuilder, StreamChunk};
7use std::time::Duration;
8use tempfile::tempdir;
9
10fn main() -> sandbox_rs::Result<()> {
11 let tmp = tempdir().expect("Failed to create temp dir");
12
13 let mut sandbox = SandboxBuilder::new("long-running-example")
14 .memory_limit_str("512M")?
15 .cpu_limit_percent(100)
16 .timeout(Duration::from_secs(30))
17 .root(tmp.path())
18 .build()?;
19
20 println!("Running bash script with streaming output...\n");
21
22 let (result, stream) = sandbox.run_with_stream(
24 "/bin/bash",
25 &[
26 "-c",
27 "for i in {1..5}; do echo \"Line $i from stdout\"; echo \"Error line $i\" >&2; done",
28 ],
29 )?;
30
31 println!("Streaming output:");
32
33 let mut stdout_count = 0;
34 let mut stderr_count = 0;
35
36 for chunk in stream.into_iter() {
37 match chunk {
38 StreamChunk::Stdout(line) => {
39 stdout_count += 1;
40 println!(" [OUT #{}] {}", stdout_count, line);
41 }
42 StreamChunk::Stderr(line) => {
43 stderr_count += 1;
44 eprintln!(" [ERR #{}] {}", stderr_count, line);
45 }
46 StreamChunk::Exit {
47 exit_code,
48 signal: _,
49 } => {
50 println!("Process finished with exit code: {}", exit_code);
51 }
52 }
53 }
54
55 println!("\nSummary:");
56 println!(" Stdout lines: {}", stdout_count);
57 println!(" Stderr lines: {}", stderr_count);
58 println!(" Wall time: {} ms", result.wall_time_ms);
59
60 Ok(())
61}