Skip to content

Commit

Permalink
handle ctrl-c signals
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Sep 10, 2024
1 parent 7cc5157 commit 07b9a6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ hyper = { version = "0.14.26", features = ["client", "http2", "server", "tcp"] }
rcgen = "0.10"
rustls = { version = "0.21", default-features = false }
rustls-pemfile = "1.0.2"
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net"] }
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net", "signal"] }

[target.'cfg(windows)'.dependencies]
winapi = "0.3"
Expand Down
23 changes: 21 additions & 2 deletions mm2src/mm2_main/src/mm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#[cfg(not(target_arch = "wasm32"))] use common::block_on;
use common::crash_reports::init_crash_reports;
use common::double_panic_crash;
use common::executor::SpawnFuture;
use common::log;
use common::log::LogLevel;
use common::password_policy::password_policy;
use mm2_core::mm_ctx::MmCtxBuilder;
Expand All @@ -54,7 +56,6 @@ use lp_swap::PAYMENT_LOCKTIME;
use std::sync::atomic::Ordering;

use gstuff::slurp;

use serde::ser::Serialize;
use serde_json::{self as json, Value as Json};

Expand All @@ -64,7 +65,6 @@ use std::process::exit;
use std::ptr::null;
use std::str;

mod lp_native_dex;
pub use self::lp_native_dex::init_hw;
pub use self::lp_native_dex::lp_init;
use coins::update_coins_config;
Expand All @@ -75,6 +75,7 @@ use mm2_err_handle::prelude::*;
pub mod heartbeat_event;
pub mod lp_dispatcher;
pub mod lp_message_service;
mod lp_native_dex;
pub mod lp_network;
pub mod lp_ordermatch;
pub mod lp_stats;
Expand Down Expand Up @@ -160,10 +161,28 @@ pub async fn lp_main(
.with_datetime(datetime.clone())
.into_mm_arc();
ctx_cb(try_s!(ctx.ffi_handle()));

#[cfg(not(target_arch = "wasm32"))]
spawn_ctrl_c_handler(ctx.clone());

try_s!(lp_init(ctx, version, datetime).await);
Ok(())
}

/// Handles CTRL-C signals and shutdowns the KDF runtime gracefully.
#[cfg(not(target_arch = "wasm32"))]
fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) {
ctx.spawner().spawn(async move {
tokio::signal::ctrl_c()
.await
.expect("Couldn't listen for the CTRL-C signal.");

log::info!("Wrapping things up and shutting down...");

ctx.stop().await.expect("Couldn't stop the KDF runtime.");
});
}

fn help() {
const HELP_MSG: &str = r#"Command-line options.
The first command-line argument is special and designates the mode.
Expand Down

0 comments on commit 07b9a6d

Please sign in to comment.