@@ -2533,37 +2533,32 @@ pub enum CommanderError {
25332533}
25342534
25352535#[ derive( Debug ) ]
2536- pub struct TerminateContainer ( Option < Command > ) ;
2536+ pub struct TerminateContainer ( Option < ( String , Command ) > ) ;
25372537
25382538impl TerminateContainer {
2539- pub fn new ( command : Command ) -> Self {
2540- Self ( Some ( command) )
2539+ pub fn new ( name : String , command : Command ) -> Self {
2540+ Self ( Some ( ( name , command) ) )
25412541 }
25422542
25432543 pub fn none ( ) -> Self {
25442544 Self ( None )
25452545 }
25462546
25472547 async fn terminate_now ( & mut self ) -> Result < ( ) , TerminateContainerError > {
2548- if let Some ( mut kill_child) = self . take_command ( ) {
2549- let o = kill_child. output ( ) . await ?;
2550- Self :: report_failure ( o) ;
2548+ use terminate_container_error:: * ;
2549+
2550+ if let Some ( ( name, mut kill_child) ) = self . 0 . take ( ) {
2551+ let o = kill_child
2552+ . output ( )
2553+ . await
2554+ . context ( TerminateContainerSnafu { name : & name } ) ?;
2555+ Self :: report_failure ( name, o) ;
25512556 }
25522557
25532558 Ok ( ( ) )
25542559 }
25552560
2556- fn take_command ( & mut self ) -> Option < Command > {
2557- self . 0 . take ( ) . map ( |mut kill_child| {
2558- kill_child
2559- . stdin ( Stdio :: null ( ) )
2560- . stdout ( Stdio :: null ( ) )
2561- . stderr ( Stdio :: null ( ) ) ;
2562- kill_child
2563- } )
2564- }
2565-
2566- fn report_failure ( s : std:: process:: Output ) {
2561+ fn report_failure ( name : String , s : std:: process:: Output ) {
25672562 // We generally don't care if the command itself succeeds or
25682563 // not; the container may already be dead! However, let's log
25692564 // it in an attempt to debug cases where there are more
@@ -2575,28 +2570,28 @@ impl TerminateContainer {
25752570 let stdout = String :: from_utf8_lossy ( & s. stdout ) ;
25762571 let stderr = String :: from_utf8_lossy ( & s. stderr ) ;
25772572
2578- error ! ( ?code, %stdout, %stderr, "Killing the container failed" ) ;
2573+ error ! ( ?code, %stdout, %stderr, %name , "Killing the container failed" ) ;
25792574 }
25802575 }
25812576}
25822577
25832578impl Drop for TerminateContainer {
25842579 fn drop ( & mut self ) {
2585- if let Some ( mut kill_child) = self . take_command ( ) {
2580+ if let Some ( ( name , mut kill_child) ) = self . 0 . take ( ) {
25862581 match kill_child. as_std_mut ( ) . output ( ) {
2587- Ok ( o) => Self :: report_failure ( o) ,
2588- Err ( e) => error ! ( "Unable to kill the container while dropping: {e}" ) ,
2582+ Ok ( o) => Self :: report_failure ( name , o) ,
2583+ Err ( e) => error ! ( "Unable to kill container {name} while dropping: {e}" ) ,
25892584 }
25902585 }
25912586 }
25922587}
25932588
25942589#[ derive( Debug , Snafu ) ]
25952590#[ snafu( module) ]
2596- pub enum TerminateContainerError {
2597- # [ snafu ( display ( "Unable to kill the child process" ) ) ]
2598- # [ snafu ( context ( false ) ) ]
2599- Execute { source : std:: io:: Error } ,
2591+ # [ snafu ( display ( "Unable to kill the Docker container {name}" ) ) ]
2592+ pub struct TerminateContainerError {
2593+ name : String ,
2594+ source : std:: io:: Error ,
26002595}
26012596
26022597pub trait Backend {
@@ -2710,8 +2705,8 @@ impl Backend for DockerBackend {
27102705 . arg ( "/playground" ) ;
27112706
27122707 let mut kill = Command :: new ( "docker" ) ;
2713- kill. arg ( "kill" ) . args ( [ "--signal" , "KILL" ] ) . arg ( name) ;
2714- let kill = TerminateContainer :: new ( kill) ;
2708+ kill. arg ( "kill" ) . args ( [ "--signal" , "KILL" ] ) . arg ( & name) ;
2709+ let kill = TerminateContainer :: new ( name , kill) ;
27152710
27162711 ( command, kill)
27172712 }
0 commit comments