From a1d81ac774c0ae52f155cd764fd74fd1ba928a5f Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Fri, 23 Sep 2022 21:34:57 +0200 Subject: ensure bots cleanly stop before a match completes --- planetwars-matchrunner/src/docker_runner.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'planetwars-matchrunner/src/docker_runner.rs') diff --git a/planetwars-matchrunner/src/docker_runner.rs b/planetwars-matchrunner/src/docker_runner.rs index 6de9bb1..939d734 100644 --- a/planetwars-matchrunner/src/docker_runner.rs +++ b/planetwars-matchrunner/src/docker_runner.rs @@ -9,6 +9,7 @@ use bytes::Bytes; use futures::{Stream, StreamExt}; use tokio::io::{AsyncWrite, AsyncWriteExt}; use tokio::sync::mpsc; +use tokio::task::JoinHandle; use tokio::time::timeout; use crate::match_context::{EventBus, PlayerHandle, RequestError, RequestMessage}; @@ -42,8 +43,7 @@ impl BotSpec for DockerBotSpec { match_logger: MatchLogger, ) -> Box { let process = spawn_docker_process(self).await.unwrap(); - let (handle, runner) = create_docker_bot(process, player_id, event_bus, match_logger); - tokio::spawn(runner.run()); + let handle = run_docker_bot(process, player_id, event_bus, match_logger); return Box::new(handle); } } @@ -155,14 +155,13 @@ impl ContainerProcess { } } -fn create_docker_bot( +fn run_docker_bot( process: ContainerProcess, player_id: u32, event_bus: Arc>, match_logger: MatchLogger, -) -> (DockerBotHandle, DockerBotRunner) { +) -> DockerBotHandle { let (tx, rx) = mpsc::unbounded_channel(); - let bot_handle = DockerBotHandle { tx }; let bot_runner = DockerBotRunner { process, player_id, @@ -170,11 +169,15 @@ fn create_docker_bot( match_logger, rx, }; - (bot_handle, bot_runner) + + let join_handle = tokio::spawn(bot_runner.run()); + + DockerBotHandle { tx, join_handle } } pub struct DockerBotHandle { tx: mpsc::UnboundedSender, + join_handle: JoinHandle<()>, } impl PlayerHandle for DockerBotHandle { @@ -183,6 +186,10 @@ impl PlayerHandle for DockerBotHandle { .send(r) .expect("failed to send message to local bot"); } + + fn into_join_handle(self: Box) -> JoinHandle<()> { + self.join_handle + } } pub struct DockerBotRunner { -- cgit v1.2.3