From 361658510f3e2eb9aefbe66232b9b1f1a1ebb80f Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Sun, 29 Oct 2017 11:52:54 -0700 Subject: Fix shard shutdown via Context This fixes the shard shutdown via the Context. This works by setting a "shutdown" field on the Shard struct as being true, indicating that the Shard has shutdown. The Shard Runner detects this and requests a shutdown from the Shard Manager. The ideal solution here would be to add a "Shutdown" variant to serenity::gateway::ConnectionStage, but that would be a breaking change, so we instead need to opt for adding a new struct to gateway::Shard. The Shard Manager has also been updated to only attempt the shutdown of a shard if it doesn't already know for certain that it shut itself down, which avoids an error logged saying that there was an error sending a shutdown message to its Shard Runner. When all shards have been shutdown (for most bots, this will only be one), the Shard Manager will end and the Client will stop its operations, returning thread control to the user. --- src/client/bridge/gateway/shard_manager.rs | 14 +++++++++----- src/client/bridge/gateway/shard_runner.rs | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) (limited to 'src/client') diff --git a/src/client/bridge/gateway/shard_manager.rs b/src/client/bridge/gateway/shard_manager.rs index 98ada1e..dfd4a16 100644 --- a/src/client/bridge/gateway/shard_manager.rs +++ b/src/client/bridge/gateway/shard_manager.rs @@ -174,13 +174,17 @@ impl ShardManager { } fn shutdown(&mut self, shard_id: ShardId) { - info!("Shutting down shard {}", shard_id); - if let Some(runner) = self.runners.lock().get(&shard_id) { - let msg = ShardManagerMessage::Shutdown(shard_id); + let is_shutdown = runner.shard.lock().is_shutdown(); + + if !is_shutdown { + info!("Shutting down shard {}", shard_id); + + let msg = ShardManagerMessage::Shutdown(shard_id); - if let Err(why) = runner.runner_tx.send(msg) { - warn!("Failed to cleanly shutdown shard {}: {:?}", shard_id, why); + if let Err(why) = runner.runner_tx.send(msg) { + warn!("Failed to cleanly shutdown shard {}: {:?}", shard_id, why); + } } } diff --git a/src/client/bridge/gateway/shard_runner.rs b/src/client/bridge/gateway/shard_runner.rs index 7ee18c3..aa0e064 100644 --- a/src/client/bridge/gateway/shard_runner.rs +++ b/src/client/bridge/gateway/shard_runner.rs @@ -140,8 +140,16 @@ impl ShardRunner { }} } - if !successful && !self.shard.lock().stage().is_connecting() { - return self.request_restart(); + { + let shard = self.shard.lock(); + + if !successful && !shard.stage().is_connecting() { + return self.request_restart(); + } + + if shard.is_shutdown() { + return self.request_shutdown(); + } } } } @@ -219,4 +227,11 @@ impl ShardRunner { Ok(()) } + + fn request_shutdown(&self) -> Result<()> { + debug!("[ShardRunner {:?}] Requesting shutdown", self.shard_info); + let _ = self.manager_tx.send(ShardManagerMessage::ShutdownAll); + + Ok(()) + } } -- cgit v1.2.3 From e219a6a9d6a890b008fc390a909ae504a0c1a329 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Wed, 1 Nov 2017 07:48:24 -0700 Subject: Use consistent token names in examples The names of environment variable tokens in the examples differed, so this makes them all use the same name. --- src/client/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/client') diff --git a/src/client/mod.rs b/src/client/mod.rs index 0a3c49f..6711287 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -685,7 +685,7 @@ impl Client { /// use serenity::Client; /// use std::env; /// - /// let token = env::var("DISCORD_BOT_TOKEN").unwrap(); + /// let token = env::var("DISCORD_TOKEN").unwrap(); /// let mut client = Client::new(&token, Handler); /// /// let _ = client.start_shard_range([4, 7], 10); -- cgit v1.2.3 From 2c59f87c1408ef633b8749f6688dc42129054a36 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Wed, 1 Nov 2017 13:10:16 -0700 Subject: Fix doctests for a variety of feature targets --- src/client/context.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/client') diff --git a/src/client/context.rs b/src/client/context.rs index 2288f28..d0a6fc7 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -263,6 +263,8 @@ impl Context { /// playing: /// /// ```rust,no_run + /// # #[cfg(feature = "model")] + /// # fn main() { /// # use serenity::prelude::*; /// # use serenity::model::*; /// # @@ -282,6 +284,10 @@ impl Context { /// } /// /// let mut client = Client::new("token", Handler); client.start().unwrap(); + /// # } + /// # + /// # #[cfg(not(feature = "model"))] + /// # fn main() { } /// ``` /// /// [`Online`]: ../model/enum.OnlineStatus.html#variant.Online -- cgit v1.2.3