From c7020da81c0fe5bdc86dff72ba3161700ced59f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C5=9Fu?= Date: Wed, 22 Mar 2023 16:43:00 +0200 Subject: [PATCH] Use nym task crate for network statistics signal handle (#3209) * Use nym task crate for network statistics signal handle * Update changelog --- CHANGELOG.md | 4 ++++ Cargo.lock | 1 + service-providers/network-statistics/Cargo.toml | 1 + .../network-statistics/src/api/mod.rs | 15 ++++++--------- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbc5cfaefb..9cba9f5970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Post 1.0.0 release, the changelog format is based on [Keep a Changelog](https:// ## [Unreleased] +- nym-network-statistics properly handles signals ([#3209]) + +[#3209]: https://github.com/nymtech/nym/issues/3209 + ## [v1.1.13] (2023-03-15) - NE - instead of throwing a "Mixnode/Gateway not found" error for blacklisted nodes due to bad performance, show their history but tag them as "Having poor performance" ([#2979]) diff --git a/Cargo.lock b/Cargo.lock index 58a817f3dd..7aa0d1fc69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3601,6 +3601,7 @@ dependencies = [ "log", "nym-bin-common", "nym-statistics-common", + "nym-task", "pretty_env_logger", "rocket", "serde", diff --git a/service-providers/network-statistics/Cargo.toml b/service-providers/network-statistics/Cargo.toml index ee6cb15703..14c4cc317e 100644 --- a/service-providers/network-statistics/Cargo.toml +++ b/service-providers/network-statistics/Cargo.toml @@ -16,6 +16,7 @@ thiserror = "1" tokio = { version = "1.4", features = [ "net", "rt-multi-thread", "macros", "time" ] } nym-bin-common = { path = "../../common/bin-common"} nym-statistics-common = { path = "../../common/statistics" } +nym-task = { path = "../../common/task" } [build-dependencies] sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "sqlite", "macros", "migrate"] } diff --git a/service-providers/network-statistics/src/api/mod.rs b/service-providers/network-statistics/src/api/mod.rs index 151a2c744d..74e47c7108 100644 --- a/service-providers/network-statistics/src/api/mod.rs +++ b/service-providers/network-statistics/src/api/mod.rs @@ -9,6 +9,7 @@ use error::Result; use routes::{post_all_statistics, post_statistic}; use nym_statistics_common::api::STATISTICS_SERVICE_VERSION; +use nym_task::TaskManager; mod error; mod routes; @@ -33,16 +34,12 @@ impl NetworkStatisticsAPI { } pub async fn run(self) { - let shutdown_handle = self.rocket.shutdown(); + let rocket_shutdown_handle = self.rocket.shutdown(); + let shutdown = TaskManager::new(10); tokio::spawn(self.rocket.launch()); - if let Err(e) = tokio::signal::ctrl_c().await { - error!( - "There was an error while capturing SIGINT - {:?}. We will terminate regardless", - e - ); - } - info!("Received SIGINT - the network statistics API will terminate now"); - shutdown_handle.notify(); + shutdown.catch_interrupt().await.ok(); + info!("Stopping network statistics"); + rocket_shutdown_handle.notify(); } }