stop player fibers on shutdown

This commit is contained in:
Nikita Vilunov 2023-08-24 14:10:31 +02:00
parent 5c07c8368d
commit b9724cd995
2 changed files with 16 additions and 1 deletions

View File

@ -261,6 +261,19 @@ impl PlayerRegistry {
let player_handle = self.get_or_create_player(id).await; let player_handle = self.get_or_create_player(id).await;
player_handle.subscribe().await player_handle.subscribe().await
} }
pub async fn shutdown_all(&mut self) -> Result<()> {
let mut inner = self.0.write().unwrap();
let mut players = HashMap::new();
std::mem::swap(&mut players, &mut inner.players);
for (i, (k, j)) in inner.players.drain() {
drop(k);
j.await?;
log::debug!("Player stopped #{i:?}")
}
log::debug!("All players stopped");
Ok(())
}
} }
/// The player registry state representation. /// The player registry state representation.
@ -337,6 +350,7 @@ impl Player {
PlayerCommand::Cmd(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await, PlayerCommand::Cmd(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await,
} }
} }
log::debug!("Shutting down player actor #{:?}", self.player_id);
self self
} }

View File

@ -54,7 +54,7 @@ async fn main() -> Result<()> {
let mut metrics = MetricsRegistry::new(); let mut metrics = MetricsRegistry::new();
let storage = Storage::open(storage_config).await?; let storage = Storage::open(storage_config).await?;
let rooms = RoomRegistry::new(&mut metrics, storage.clone())?; let rooms = RoomRegistry::new(&mut metrics, storage.clone())?;
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics)?; let mut players = PlayerRegistry::empty(rooms.clone(), &mut metrics)?;
let telemetry_terminator = let telemetry_terminator =
util::telemetry::launch(telemetry_config, metrics.clone(), rooms.clone()).await?; util::telemetry::launch(telemetry_config, metrics.clone(), rooms.clone()).await?;
let irc = projections::irc::launch(irc_config, players.clone(), rooms.clone(), metrics.clone(), storage.clone()).await?; let irc = projections::irc::launch(irc_config, players.clone(), rooms.clone(), metrics.clone(), storage.clone()).await?;
@ -67,6 +67,7 @@ async fn main() -> Result<()> {
xmpp.terminate().await?; xmpp.terminate().await?;
irc.terminate().await?; irc.terminate().await?;
telemetry_terminator.terminate().await?; telemetry_terminator.terminate().await?;
players.shutdown_all().await?;
drop(players); drop(players);
drop(rooms); drop(rooms);
storage.close().await?; storage.close().await?;