@@ -183,6 +183,31 @@ pub fn wait_for_orchestrator(
183183 Err ( last_error. unwrap_or_else ( || "Timed out waiting for orchestrator" . to_string ( ) ) )
184184}
185185
186+ pub fn request_orchestrator_shutdown ( data_dir : & str ) -> Result < bool , String > {
187+ let base_url = read_orchestrator_state ( data_dir)
188+ . and_then ( |state| state. daemon . map ( |daemon| daemon. base_url ) )
189+ . map ( |url| url. trim ( ) . to_string ( ) )
190+ . filter ( |url| !url. is_empty ( ) ) ;
191+
192+ let Some ( base_url) = base_url else {
193+ return Ok ( false ) ;
194+ } ;
195+
196+ let url = format ! ( "{}/shutdown" , base_url. trim_end_matches( '/' ) ) ;
197+ let agent = ureq:: AgentBuilder :: new ( )
198+ . timeout ( std:: time:: Duration :: from_millis ( 1500 ) )
199+ . build ( ) ;
200+
201+ agent
202+ . post ( & url)
203+ . set ( "Accept" , "application/json" )
204+ . set ( "Content-Type" , "application/json" )
205+ . send_string ( "" )
206+ . map_err ( |e| format ! ( "Failed to request orchestrator shutdown at {url}: {e}" ) ) ?;
207+
208+ Ok ( true )
209+ }
210+
186211pub fn spawn_orchestrator_daemon (
187212 app : & AppHandle ,
188213 options : & OrchestratorSpawnOptions ,
@@ -257,6 +282,69 @@ pub fn spawn_orchestrator_daemon(
257282 . map_err ( |e| format ! ( "Failed to start orchestrator: {e}" ) )
258283}
259284
285+ #[ cfg( test) ]
286+ mod tests {
287+ use super :: request_orchestrator_shutdown;
288+ use std:: fs;
289+ use std:: io:: { Read , Write } ;
290+ use std:: net:: TcpListener ;
291+ use std:: thread;
292+ use uuid:: Uuid ;
293+
294+ #[ test]
295+ fn request_shutdown_returns_false_without_state ( ) {
296+ let dir = std:: env:: temp_dir ( ) . join ( format ! (
297+ "openwork-orchestrator-shutdown-missing-{}" ,
298+ Uuid :: new_v4( )
299+ ) ) ;
300+ fs:: create_dir_all ( & dir) . expect ( "create test dir" ) ;
301+
302+ let stopped = request_orchestrator_shutdown ( & dir. to_string_lossy ( ) ) . expect ( "request" ) ;
303+ assert ! ( !stopped) ;
304+
305+ let _ = fs:: remove_dir_all ( dir) ;
306+ }
307+
308+ #[ test]
309+ fn request_shutdown_posts_to_daemon_endpoint ( ) {
310+ let listener = TcpListener :: bind ( "127.0.0.1:0" ) . expect ( "bind shutdown listener" ) ;
311+ let port = listener. local_addr ( ) . expect ( "listener addr" ) . port ( ) ;
312+
313+ let handle = thread:: spawn ( move || {
314+ let ( mut stream, _) = listener. accept ( ) . expect ( "accept shutdown request" ) ;
315+ let mut buffer = [ 0u8 ; 2048 ] ;
316+ let bytes = stream. read ( & mut buffer) . expect ( "read request" ) ;
317+ let request = String :: from_utf8_lossy ( & buffer[ ..bytes] ) ;
318+ assert ! ( request. starts_with( "POST /shutdown " ) ) ;
319+ stream
320+ . write_all (
321+ b"HTTP/1.1 200 OK\r \n Content-Type: application/json\r \n Content-Length: 11\r \n Connection: close\r \n \r \n {\" ok\" :true}" ,
322+ )
323+ . expect ( "write response" ) ;
324+ } ) ;
325+
326+ let dir = std:: env:: temp_dir ( ) . join ( format ! (
327+ "openwork-orchestrator-shutdown-state-{}" ,
328+ Uuid :: new_v4( )
329+ ) ) ;
330+ fs:: create_dir_all ( & dir) . expect ( "create state dir" ) ;
331+ let state_path = dir. join ( "openwork-orchestrator-state.json" ) ;
332+ fs:: write (
333+ & state_path,
334+ format ! (
335+ "{{\" daemon\" :{{\" pid\" :1,\" port\" :{port},\" baseUrl\" :\" http://127.0.0.1:{port}\" ,\" startedAt\" :1}}}}"
336+ ) ,
337+ )
338+ . expect ( "write state file" ) ;
339+
340+ let stopped = request_orchestrator_shutdown ( & dir. to_string_lossy ( ) ) . expect ( "request" ) ;
341+ assert ! ( stopped) ;
342+
343+ handle. join ( ) . expect ( "server thread" ) ;
344+ let _ = fs:: remove_dir_all ( dir) ;
345+ }
346+ }
347+
260348pub fn orchestrator_status_from_state (
261349 data_dir : & str ,
262350 last_error : Option < String > ,
0 commit comments