diff options
| author | Nathan Perry <avaglir@gmail.com> | 2018-04-04 01:19:04 -0400 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2018-04-04 01:27:23 -0400 |
| commit | 7952b4b11423f7fb582e71ec982b18e6a780562e (patch) | |
| tree | f425cc764b8f74ca6be58b8fed9a32cee63553d2 | |
| parent | b7b4b264fa4f3b1deedc872e89d62895208d4583 (diff) | |
add support for SIGINT/SIGTERM with ctrlc crate
| -rw-r--r-- | Cargo.lock | 24 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/main.rs | 54 |
3 files changed, 53 insertions, 26 deletions
@@ -128,6 +128,16 @@ dependencies = [ ] [[package]] +name = "ctrlc" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "derive-error-chain" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -451,6 +461,17 @@ dependencies = [ ] [[package]] +name = "nix" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "num" version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -903,6 +924,7 @@ version = "0.1.0" dependencies = [ "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ctrlc 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv_codegen 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1075,6 +1097,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0aa3473e85a3161b59845d6096b289bb577874cafeaf75ea1b1beaa6572c7fc" "checksum core-foundation 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "25bfd746d203017f7d5cbd31ee5d8e17f94b6521c7af77ece6c9e4b2d4b16c67" "checksum core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "065a5d7ffdcbc8fa145d6f0746f3555025b9097a9e9cda59f7467abae670c78d" +"checksum ctrlc 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "653abc99aa905f693d89df4797fadc08085baee379db92be9f2496cefe8a6f2c" "checksum derive-error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "92183014af72c63aea490e66526c712bf1066ac50f66c9f34824f02483ec1d98" "checksum diesel 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "925325c57038f2f14c0413bdf6a92ca72acff644959d0a1a9ebf8d19be7e9c01" "checksum diesel_derives 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "28e2b2605ac6a3b9a586383f5f8b2b5f1108f07a421ade965b266289d2805e79" @@ -1112,6 +1135,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum miniz-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "609ce024854aeb19a0ef7567d348aaa5a746b32fb72e336df7fcc16869d7e2b4" "checksum multipart 0.13.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92f54eb45230c3aa20864ccf0c277eeaeadcf5e437e91731db498dbf7fbe0ec6" "checksum native-tls 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f74dbadc8b43df7864539cedb7bc91345e532fdd913cfdc23ad94f4d2d40fbc0" +"checksum nix 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a2c5afeb0198ec7be8569d666644b574345aad2e95a53baf3a532da3e0f3fb32" "checksum num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e" "checksum num-integer 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f8d26da319fb45674985c78f1d1caf99aa4941f785d384a2ae36d0740bc3e2fe" "checksum num-iter 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "4b226df12c5a59b63569dd57fafb926d91b385dfce33d8074a412411b689d593" @@ -18,6 +18,7 @@ chrono = "0.4" fern = { version = "0.5", features = ["colored"] } diesel = { version = "1.0.0", features = ["postgres"], optional = true } cfg-if = "0.1" +ctrlc = { version = "3.0", features = ["termination"] } [dependencies.serenity] # version = "~0.5" diff --git a/src/main.rs b/src/main.rs index 8d5baa2..2a1b76a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,28 @@ -#[macro_use] extern crate serenity; -#[macro_use] extern crate log; -#[macro_use] extern crate error_chain; -#[macro_use] extern crate lazy_static; #[macro_use] extern crate cfg_if; -#[macro_use] extern crate dotenv_codegen; - +extern crate chrono; +extern crate ctrlc; extern crate dotenv; +#[macro_use] extern crate dotenv_codegen; +#[macro_use] extern crate error_chain; extern crate fern; +#[macro_use] extern crate lazy_static; +#[macro_use] extern crate log; +#[macro_use] extern crate serenity; extern crate typemap; extern crate url; -extern crate chrono; +use commands::register_commands; +use dotenv::dotenv; +use errors::*; +use serenity::framework::standard::help_commands; +use serenity::framework::StandardFramework; +use serenity::model::gateway::Ready; +use serenity::model::id::{GuildId, UserId}; +use serenity::prelude::*; +use std::env; +use std::thread; +use std::time::{Duration, Instant}; +pub use util::*; cfg_if! { if #[cfg(feature = "diesel")] { #[macro_use] extern crate diesel; @@ -21,20 +33,6 @@ cfg_if! { mod commands; mod util; -use std::env; -use std::thread; -use std::time::{Duration, Instant}; - -use serenity::prelude::*; -use serenity::framework::StandardFramework; -use serenity::framework::standard::help_commands; -use serenity::model::gateway::Ready; -use serenity::model::id::{UserId, GuildId}; - -use dotenv::dotenv; - -use commands::register_commands; - mod errors { error_chain! { foreign_links { @@ -46,9 +44,6 @@ mod errors { } } -use errors::*; -pub use util::*; - lazy_static! { static ref TARGET_GUILD: u64 = dotenv!("TARGET_GUILD").parse().expect("unable to parse TARGET_GUILD as u64"); static ref TARGET_GUILD_ID: GuildId = GuildId(*TARGET_GUILD); @@ -107,8 +102,14 @@ fn run() -> Result<()> { }); framework = register_commands(framework); - client.with_framework(framework); + + let shard_manager = client.shard_manager.clone(); + ctrlc::set_handler(move || { + info!("shutting down"); + shard_manager.lock().shutdown_all(); + }).expect("unable to create SIGINT/SIGTERM handlers"); + client.start()?; Ok(()) @@ -183,7 +184,8 @@ fn main() { ::std::process::exit(1); }, _ => { - warn!("somehow `run` completed without an error. should probably take a look at this."); + // NOTE: we MUST have gotten here through SIGINT/SIGTERM handlers + ::std::process::exit(0); } } |
