Skip to content

Commit 6c77888

Browse files
committed
wip: CLI
1 parent b93c41c commit 6c77888

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

pyroscope_cli/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Cargo.lock
33
**/*.rs.bk
44
.cache
55
.DS_Store
6+
scripts

pyroscope_cli/core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ edition = "2021"
99
utils = { path = "../utils" }
1010
pyroscope = { path = "../../" }
1111
log = "0.4.14"
12+
ctrlc = "3.2.1"
13+
duct = "0.13.5"

pyroscope_cli/core/src/commands.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use utils::error::Result;
44
use pyroscope::pyroscope_backends::rbspy::{Rbspy, RbspyConfig};
55
use pyroscope::PyroscopeAgent;
66

7+
use ctrlc;
8+
use std::sync::mpsc::channel;
9+
10+
use duct::cmd;
11+
712
/// adhoc command
813
pub fn adhoc() -> Result<()> {
914
println!("adhoc command");
@@ -12,12 +17,48 @@ pub fn adhoc() -> Result<()> {
1217

1318
/// exec command
1419
pub fn exec() -> Result<()> {
15-
println!("exec command");
20+
let (tx, rx) = channel();
21+
22+
let handle = cmd!("ruby", "./scripts/ruby.rb").stdout_capture().start()?;
23+
let pids = handle.pids();
24+
25+
let pid = *pids.get(0).unwrap() as i32;
26+
27+
let config = RbspyConfig::new(pid, 100, true, None, true);
28+
29+
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "rbspy.basic")
30+
.backend(Rbspy::new(config))
31+
.build()
32+
.unwrap();
33+
34+
agent.start().unwrap();
35+
36+
//handle.wait()?;
37+
38+
ctrlc::set_handler(move || {
39+
tx.send(()).unwrap();
40+
})
41+
.expect("Error setting Ctrl-C handler");
42+
43+
println!("Press Ctrl-C to exit.");
44+
45+
rx.recv().unwrap();
46+
47+
println!("Exiting.");
48+
49+
agent.stop().unwrap();
50+
51+
drop(agent);
52+
53+
handle.kill()?;
54+
1655
Ok(())
1756
}
1857

1958
/// connect command
2059
pub fn connect() -> Result<()> {
60+
let (tx, rx) = channel();
61+
2162
println!("connect command");
2263
let pid: i32 = AppConfig::get("pid")?;
2364

@@ -30,12 +71,21 @@ pub fn connect() -> Result<()> {
3071

3172
agent.start().unwrap();
3273

33-
std::thread::sleep(std::time::Duration::from_secs(100));
74+
ctrlc::set_handler(move || {
75+
tx.send(()).unwrap();
76+
})
77+
.expect("Error setting Ctrl-C handler");
3478

35-
println!("agent started");
79+
println!("Press Ctrl-C to exit.");
80+
81+
rx.recv().unwrap();
82+
83+
println!("Exiting.");
3684

3785
agent.stop().unwrap();
3886

87+
drop(agent);
88+
3989
Ok(())
4090
}
4191

0 commit comments

Comments
 (0)