stop and remove bot containers after match is finished

This commit is contained in:
Ilion Beyst 2022-05-21 14:20:38 +02:00
parent 39940aaabd
commit c873f3a1cb

View file

@ -85,16 +85,35 @@ async fn spawn_docker_process(
.await?; .await?;
Ok(ContainerProcess { Ok(ContainerProcess {
docker,
container_id,
stdin: input, stdin: input,
output, output,
}) })
} }
struct ContainerProcess { struct ContainerProcess {
docker: Docker,
container_id: String,
stdin: Pin<Box<dyn AsyncWrite + Send>>, stdin: Pin<Box<dyn AsyncWrite + Send>>,
output: Pin<Box<dyn Stream<Item = Result<LogOutput, bollard::errors::Error>> + Send>>, output: Pin<Box<dyn Stream<Item = Result<LogOutput, bollard::errors::Error>> + 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( fn create_docker_bot(
process: ContainerProcess, process: ContainerProcess,
player_id: u32, player_id: u32,
@ -151,6 +170,11 @@ impl DockerBotRunner {
.unwrap() .unwrap()
.resolve_request(request_id, request_response); .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<Bytes> { pub async fn communicate(&mut self, input: &[u8]) -> io::Result<Bytes> {