Skip to content

Commit

Permalink
fix: remove unneeded termination handler (#30)
Browse files Browse the repository at this point in the history
Description
---
Windows build were failing because we were using a unix only compatible
termination handling lib.

Motivation and Context
---

How Has This Been Tested?
---

What process can a PR reviewer use to test or verify this change?
---


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify
ksrichard authored Aug 16, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7b3568b commit 1952cd4
Showing 8 changed files with 33 additions and 34 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -52,7 +52,6 @@ hex = "0.4.3"
serde_json = "1.0.122"
hickory-resolver = { version = "*", features = ["dns-over-rustls"] }
convert_case = "0.6.0"
signal-hook = "0.3.17"

[package.metadata.cargo-machete]
ignored = ["log4rs"]
16 changes: 1 addition & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@
// SPDX-License-Identifier: BSD-3-Clause

use clap::Parser;
use signal_hook::consts::{SIGINT, SIGQUIT, SIGTERM};
use signal_hook::iterator::Signals;
use tari_shutdown::Shutdown;

use crate::cli::Cli;
@@ -14,18 +12,6 @@ mod sharechain;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// shutdown hook
let mut cli_shutdown = Shutdown::new();
let cli_shutdown_signal = cli_shutdown.to_signal();
let mut signals = Signals::new([SIGINT, SIGTERM, SIGQUIT])?;
let signals_handle = signals.handle();
tokio::spawn(async move {
let _ = signals.forever().next();
cli_shutdown.trigger();
});

// cli
Cli::parse().handle_command(cli_shutdown_signal).await?;
signals_handle.close();
Cli::parse().handle_command(Shutdown::new().to_signal()).await?;
Ok(())
}
7 changes: 5 additions & 2 deletions src/server/grpc/base_node.rs
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ use minotari_app_grpc::{
ValueAtHeightResponse,
},
};
use tari_shutdown::ShutdownSignal;
use tokio::sync::Mutex;
use tonic::{transport::Channel, Request, Response, Status, Streaming};

@@ -100,9 +101,11 @@ pub struct TariBaseNodeGrpc {
}

impl TariBaseNodeGrpc {
pub async fn new(base_node_address: String) -> Result<Self, Error> {
pub async fn new(base_node_address: String, shutdown_signal: ShutdownSignal) -> Result<Self, Error> {
Ok(Self {
client: Arc::new(Mutex::new(util::connect_base_node(base_node_address).await?)),
client: Arc::new(Mutex::new(
util::connect_base_node(base_node_address, shutdown_signal).await?,
)),
})
}
}
2 changes: 2 additions & 0 deletions src/server/grpc/error.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ use thiserror::Error;
pub enum Error {
#[error("Tonic error: {0}")]
Tonic(#[from] TonicError),
#[error("Shutdown")]
Shutdown,
}

#[derive(Error, Debug)]
6 changes: 5 additions & 1 deletion src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ use minotari_app_grpc::tari_rpc::{
SubmitBlockRequest, SubmitBlockResponse,
};
use tari_core::proof_of_work::sha3x_difficulty;
use tari_shutdown::ShutdownSignal;
use tari_utilities::hex::Hex;
use tokio::sync::Mutex;
use tonic::{Request, Response, Status};
@@ -53,9 +54,12 @@ where
p2p_client: p2p::ServiceClient,
share_chain: Arc<S>,
stats_store: Arc<StatsStore>,
shutdown_signal: ShutdownSignal,
) -> Result<Self, Error> {
Ok(Self {
client: Arc::new(Mutex::new(util::connect_base_node(base_node_address).await?)),
client: Arc::new(Mutex::new(
util::connect_base_node(base_node_address, shutdown_signal).await?,
)),
p2p_client,
share_chain,
stats_store,
16 changes: 15 additions & 1 deletion src/server/grpc/util.rs
Original file line number Diff line number Diff line change
@@ -6,13 +6,18 @@ use std::time::Duration;
use log::error;
use minotari_app_grpc::tari_rpc::base_node_client::BaseNodeClient;
use minotari_node_grpc_client::BaseNodeGrpcClient;
use tari_shutdown::ShutdownSignal;
use tokio::select;
use tokio::time::sleep;
use tonic::transport::Channel;

use crate::server::grpc::error::{Error, TonicError};

/// Utility function to connect to a Base node and try infinitely when it fails until gets connected.
pub async fn connect_base_node(base_node_address: String) -> Result<BaseNodeClient<Channel>, Error> {
pub async fn connect_base_node(
base_node_address: String,
shutdown_signal: ShutdownSignal,
) -> Result<BaseNodeClient<Channel>, Error> {
let client_result = BaseNodeGrpcClient::connect(base_node_address.clone())
.await
.map_err(|e| Error::Tonic(TonicError::Transport(e)));
@@ -21,6 +26,7 @@ pub async fn connect_base_node(base_node_address: String) -> Result<BaseNodeClie
Err(error) => {
error!("[Retry] Failed to connect to Tari base node: {:?}", error.to_string());
let mut client = None;
tokio::pin!(shutdown_signal);
while client.is_none() {
sleep(Duration::from_secs(5)).await;
match BaseNodeGrpcClient::connect(base_node_address.clone())
@@ -30,6 +36,14 @@ pub async fn connect_base_node(base_node_address: String) -> Result<BaseNodeClie
Ok(curr_client) => client = Some(curr_client),
Err(error) => error!("[Retry] Failed to connect to Tari base node: {:?}", error.to_string()),
}
select! {
() = &mut shutdown_signal => {
return Err(Error::Shutdown);
}
else => {
continue;
}
}
}
client.unwrap()
},
8 changes: 5 additions & 3 deletions src/server/server.rs
Original file line number Diff line number Diff line change
@@ -75,16 +75,18 @@ where
let mut base_node_grpc_server = None;
let mut p2pool_server = None;
if config.mining_enabled {
let base_node_grpc_service = TariBaseNodeGrpc::new(config.base_node_address.clone())
.await
.map_err(Error::Grpc)?;
let base_node_grpc_service =
TariBaseNodeGrpc::new(config.base_node_address.clone(), shutdown_signal.clone())
.await
.map_err(Error::Grpc)?;
base_node_grpc_server = Some(BaseNodeServer::new(base_node_grpc_service));

let p2pool_grpc_service = ShaP2PoolGrpc::new(
config.base_node_address.clone(),
p2p_service.client(),
share_chain.clone(),
stats_store.clone(),
shutdown_signal.clone(),
)
.await
.map_err(Error::Grpc)?;

0 comments on commit 1952cd4

Please sign in to comment.