@@ -4,6 +4,11 @@ use utils::error::Result;
44use pyroscope:: pyroscope_backends:: rbspy:: { Rbspy , RbspyConfig } ;
55use pyroscope:: PyroscopeAgent ;
66
7+ use ctrlc;
8+ use std:: sync:: mpsc:: channel;
9+
10+ use duct:: cmd;
11+
712/// adhoc command
813pub fn adhoc ( ) -> Result < ( ) > {
914 println ! ( "adhoc command" ) ;
@@ -12,12 +17,48 @@ pub fn adhoc() -> Result<()> {
1217
1318/// exec command
1419pub 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
2059pub 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