Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add signal entrypoint #162

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ tokio.workspace = true
tree_hash.workspace = true
tree_hash_derive.workspace = true

# telemetry
tracing.workspace = true

# misc
clap.workspace = true
eyre.workspace = true
Expand Down
22 changes: 20 additions & 2 deletions bin/pbs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use cb_common::{config::load_pbs_config, utils::initialize_pbs_tracing_log};
use cb_common::{
config::load_pbs_config,
utils::{initialize_pbs_tracing_log, wait_for_signal},
};
use cb_pbs::{DefaultBuilderApi, PbsService, PbsState};
use eyre::Result;
use tracing::{error, info};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -13,7 +17,21 @@ async fn main() -> Result<()> {

let pbs_config = load_pbs_config()?;
let _guard = initialize_pbs_tracing_log();

let state = PbsState::new(pbs_config);
PbsService::init_metrics()?;
PbsService::run::<_, DefaultBuilderApi>(state).await
let server = PbsService::run::<_, DefaultBuilderApi>(state);

tokio::select! {
maybe_err = server => {
if let Err(err) = maybe_err {
error!(%err, "PBS service unexpectedly stopped");
}
},
_ = wait_for_signal() => {
info!("shutting down");
}
}

Ok(())
}
18 changes: 16 additions & 2 deletions bin/signer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use cb_common::{
config::{StartSignerConfig, SIGNER_MODULE_NAME},
utils::initialize_tracing_log,
utils::{initialize_tracing_log, wait_for_signal},
};
use cb_signer::service::SigningService;
use eyre::Result;
use tracing::{error, info};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -16,5 +17,18 @@ async fn main() -> Result<()> {

let config = StartSignerConfig::load_from_env()?;
let _guard = initialize_tracing_log(SIGNER_MODULE_NAME);
SigningService::run(config).await
let server = SigningService::run(config);

tokio::select! {
maybe_err = server => {
if let Err(err) = maybe_err {
error!(%err, "signing server unexpectedly stopped");
}
},
_ = wait_for_signal() => {
info!("shutting down");
}
}

Ok(())
}
2 changes: 1 addition & 1 deletion bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod prelude {
get_header, get_status, register_validator, submit_block, BuilderApi, BuilderApiState,
DefaultBuilderApi, PbsService, PbsState,
};
// The TreeHash derive macro requires tree_hash:: as import
// The TreeHash derive macro requires tree_hash as import
pub mod tree_hash {
pub use tree_hash::*;
}
Expand Down
21 changes: 21 additions & 0 deletions crates/common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,24 @@ pub fn get_user_agent_with_version(req_headers: &HeaderMap) -> eyre::Result<Head
let ua = get_user_agent(req_headers);
Ok(HeaderValue::from_str(&format!("commit-boost/{HEADER_VERSION_VALUE} {}", ua))?)
}

#[cfg(unix)]
pub async fn wait_for_signal() -> eyre::Result<()> {
use tokio::signal::unix::{signal, SignalKind};

let mut sigint = signal(SignalKind::interrupt())?;
let mut sigterm = signal(SignalKind::terminate())?;

tokio::select! {
_ = sigint.recv() => {}
_ = sigterm.recv() => {}
}

Ok(())
}

#[cfg(windows)]
pub async fn wait_for_signal() -> eyre::Result<()> {
tokio::signal::ctrl_c().await?;
Ok(())
}
4 changes: 2 additions & 2 deletions crates/pbs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ impl PbsService {
let address = SocketAddr::from(([0, 0, 0, 0], state.config.pbs_config.port));
let events_subs =
state.config.event_publisher.as_ref().map(|e| e.n_subscribers()).unwrap_or_default();
info!(version = COMMIT_BOOST_VERSION, ?address, events_subs, chain =? state.config.chain, "Starting PBS service");
info!(version = COMMIT_BOOST_VERSION, ?address, events_subs, chain =? state.config.chain, "starting PBS service");

let app = create_app_router::<S, A>(state);
let listener = TcpListener::bind(address).await.expect("failed tcp binding");
let listener = TcpListener::bind(address).await?;

axum::serve(listener, app).await.wrap_err("PBS server exited")
}
Expand Down
9 changes: 3 additions & 6 deletions crates/signer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use cb_common::{
constants::COMMIT_BOOST_VERSION,
types::{Jwt, ModuleId},
};
use eyre::{Result, WrapErr};
use eyre::{Context, Result};
use headers::{authorization::Bearer, Authorization};
use tokio::{net::TcpListener, sync::RwLock};
use tracing::{debug, error, info, warn};
Expand Down Expand Up @@ -71,12 +71,9 @@ impl SigningService {
.route_layer(middleware::from_fn_with_state(state.clone(), jwt_auth));

let address = SocketAddr::from(([0, 0, 0, 0], config.server_port));
let listener = TcpListener::bind(address).await.wrap_err("failed tcp binding")?;
let listener = TcpListener::bind(address).await?;

if let Err(err) = axum::serve(listener, app).await {
error!(%err, "Signing server exited")
}
Ok(())
axum::serve(listener, app).await.wrap_err("signer server exited")
}
}

Expand Down
Loading