diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-09-23 21:34:57 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-09-23 21:34:57 +0200 |
commit | a1d81ac774c0ae52f155cd764fd74fd1ba928a5f (patch) | |
tree | f0d4c7fda3f09fcc824795100b045d456baeecec /planetwars-matchrunner/src/docker_runner.rs | |
parent | 8f3621813e44df2dace49a6400cfe870bc3778ea (diff) | |
download | planetwars.dev-a1d81ac774c0ae52f155cd764fd74fd1ba928a5f.tar.xz planetwars.dev-a1d81ac774c0ae52f155cd764fd74fd1ba928a5f.zip |
ensure bots cleanly stop before a match completes
Diffstat (limited to 'planetwars-matchrunner/src/docker_runner.rs')
-rw-r--r-- | planetwars-matchrunner/src/docker_runner.rs | 19 |
1 files changed, 13 insertions, 6 deletions
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<dyn PlayerHandle> { 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<Mutex<EventBus>>, 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<RequestMessage>, + 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<Self>) -> JoinHandle<()> { + self.join_handle + } } pub struct DockerBotRunner { |