From c873f3a1cb9d59caecfe3f78d04fd0b67e1d8161 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 21 May 2022 14:20:38 +0200 Subject: [PATCH] stop and remove bot containers after match is finished --- planetwars-matchrunner/src/docker_runner.rs | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/planetwars-matchrunner/src/docker_runner.rs b/planetwars-matchrunner/src/docker_runner.rs index d563d60..6e73e08 100644 --- a/planetwars-matchrunner/src/docker_runner.rs +++ b/planetwars-matchrunner/src/docker_runner.rs @@ -85,16 +85,35 @@ async fn spawn_docker_process( .await?; Ok(ContainerProcess { + docker, + container_id, stdin: input, output, }) } struct ContainerProcess { + docker: Docker, + container_id: String, stdin: Pin>, output: Pin> + Send>>, } +impl ContainerProcess { + // &mut is required here to make terminate().await Sync + async fn terminate(&mut self) -> Result<(), bollard::errors::Error> { + self.docker + .remove_container( + &self.container_id, + Some(bollard::container::RemoveContainerOptions { + force: true, + ..Default::default() + }), + ) + .await + } +} + fn create_docker_bot( process: ContainerProcess, player_id: u32, @@ -151,6 +170,11 @@ impl DockerBotRunner { .unwrap() .resolve_request(request_id, request_response); } + + self.process + .terminate() + .await + .expect("could not terminate process"); } pub async fn communicate(&mut self, input: &[u8]) -> io::Result {