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/bot_runner.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'planetwars-matchrunner/src/bot_runner.rs') diff --git a/planetwars-matchrunner/src/bot_runner.rs b/planetwars-matchrunner/src/bot_runner.rs index d40a133..8597e26 100644 --- a/planetwars-matchrunner/src/bot_runner.rs +++ b/planetwars-matchrunner/src/bot_runner.rs @@ -6,14 +6,18 @@ use std::sync::Mutex; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, Lines}; use tokio::process; use tokio::sync::mpsc; +use tokio::task::JoinHandle; use tokio::time::timeout; use super::match_context::EventBus; use super::match_context::PlayerHandle; use super::match_context::RequestError; use super::match_context::RequestMessage; +// TODO: this is exactly the same as the docker bot handle. +// should this abstraction be removed? pub struct LocalBotHandle { tx: mpsc::UnboundedSender, + join_handle: JoinHandle<()>, } impl PlayerHandle for LocalBotHandle { @@ -22,6 +26,10 @@ impl PlayerHandle for LocalBotHandle { .send(r) .expect("failed to send message to local bot"); } + + fn into_join_handle(self: Box) -> JoinHandle<()> { + self.join_handle + } } pub fn run_local_bot(player_id: u32, event_bus: Arc>, bot: Bot) -> LocalBotHandle { @@ -33,9 +41,9 @@ pub fn run_local_bot(player_id: u32, event_bus: Arc>, bot: Bot) player_id, bot, }; - tokio::spawn(runner.run()); + let join_handle = tokio::spawn(runner.run()); - LocalBotHandle { tx } + LocalBotHandle { tx, join_handle } } pub struct LocalBotRunner { -- cgit v1.2.3