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/bot_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/bot_runner.rs')
-rw-r--r-- | planetwars-matchrunner/src/bot_runner.rs | 12 |
1 files changed, 10 insertions, 2 deletions
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<RequestMessage>, + 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<Self>) -> JoinHandle<()> { + self.join_handle + } } pub fn run_local_bot(player_id: u32, event_bus: Arc<Mutex<EventBus>>, bot: Bot) -> LocalBotHandle { @@ -33,9 +41,9 @@ pub fn run_local_bot(player_id: u32, event_bus: Arc<Mutex<EventBus>>, 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 { |