this repo has no description
1use bspds::crawlers::{Crawlers, start_crawlers_service}; 2use bspds::notifications::{DiscordSender, EmailSender, NotificationService, SignalSender, TelegramSender}; 3use bspds::state::AppState; 4use std::net::SocketAddr; 5use std::process::ExitCode; 6use std::sync::Arc; 7use tokio::sync::watch; 8use tracing::{error, info, warn}; 9 10#[tokio::main] 11async fn main() -> ExitCode { 12 dotenvy::dotenv().ok(); 13 tracing_subscriber::fmt::init(); 14 15 bspds::metrics::init_metrics(); 16 17 match run().await { 18 Ok(()) => ExitCode::SUCCESS, 19 Err(e) => { 20 error!("Fatal error: {}", e); 21 ExitCode::FAILURE 22 } 23 } 24} 25 26async fn run() -> Result<(), Box<dyn std::error::Error>> { 27 let database_url = std::env::var("DATABASE_URL") 28 .map_err(|_| "DATABASE_URL environment variable must be set")?; 29 30 let max_connections: u32 = std::env::var("DATABASE_MAX_CONNECTIONS") 31 .ok() 32 .and_then(|v| v.parse().ok()) 33 .unwrap_or(100); 34 let min_connections: u32 = std::env::var("DATABASE_MIN_CONNECTIONS") 35 .ok() 36 .and_then(|v| v.parse().ok()) 37 .unwrap_or(10); 38 let acquire_timeout_secs: u64 = std::env::var("DATABASE_ACQUIRE_TIMEOUT_SECS") 39 .ok() 40 .and_then(|v| v.parse().ok()) 41 .unwrap_or(10); 42 43 info!( 44 "Configuring database pool: max={}, min={}, acquire_timeout={}s", 45 max_connections, min_connections, acquire_timeout_secs 46 ); 47 48 let pool = sqlx::postgres::PgPoolOptions::new() 49 .max_connections(max_connections) 50 .min_connections(min_connections) 51 .acquire_timeout(std::time::Duration::from_secs(acquire_timeout_secs)) 52 .idle_timeout(std::time::Duration::from_secs(300)) 53 .max_lifetime(std::time::Duration::from_secs(1800)) 54 .connect(&database_url) 55 .await 56 .map_err(|e| format!("Failed to connect to Postgres: {}", e))?; 57 58 sqlx::migrate!("./migrations") 59 .run(&pool) 60 .await 61 .map_err(|e| format!("Failed to run migrations: {}", e))?; 62 63 let state = AppState::new(pool.clone()).await; 64 65 bspds::sync::listener::start_sequencer_listener(state.clone()).await; 66 67 let (shutdown_tx, shutdown_rx) = watch::channel(false); 68 69 let mut notification_service = NotificationService::new(pool); 70 71 if let Some(email_sender) = EmailSender::from_env() { 72 info!("Email notifications enabled"); 73 notification_service = notification_service.register_sender(email_sender); 74 } else { 75 warn!("Email notifications disabled (MAIL_FROM_ADDRESS not set)"); 76 } 77 78 if let Some(discord_sender) = DiscordSender::from_env() { 79 info!("Discord notifications enabled"); 80 notification_service = notification_service.register_sender(discord_sender); 81 } 82 83 if let Some(telegram_sender) = TelegramSender::from_env() { 84 info!("Telegram notifications enabled"); 85 notification_service = notification_service.register_sender(telegram_sender); 86 } 87 88 if let Some(signal_sender) = SignalSender::from_env() { 89 info!("Signal notifications enabled"); 90 notification_service = notification_service.register_sender(signal_sender); 91 } 92 93 let notification_handle = tokio::spawn(notification_service.run(shutdown_rx.clone())); 94 95 let crawlers_handle = if let Some(crawlers) = Crawlers::from_env() { 96 let crawlers = Arc::new( 97 crawlers.with_circuit_breaker(state.circuit_breakers.relay_notification.clone()) 98 ); 99 let firehose_rx = state.firehose_tx.subscribe(); 100 info!("Crawlers notification service enabled"); 101 Some(tokio::spawn(start_crawlers_service(crawlers, firehose_rx, shutdown_rx))) 102 } else { 103 warn!("Crawlers notification service disabled (PDS_HOSTNAME or CRAWLERS not set)"); 104 None 105 }; 106 107 let app = bspds::app(state); 108 109 let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); 110 info!("listening on {}", addr); 111 let listener = tokio::net::TcpListener::bind(addr) 112 .await 113 .map_err(|e| format!("Failed to bind to {}: {}", addr, e))?; 114 115 let server_result = axum::serve(listener, app) 116 .with_graceful_shutdown(shutdown_signal(shutdown_tx)) 117 .await; 118 119 notification_handle.await.ok(); 120 if let Some(handle) = crawlers_handle { 121 handle.await.ok(); 122 } 123 124 if let Err(e) = server_result { 125 return Err(format!("Server error: {}", e).into()); 126 } 127 128 Ok(()) 129} 130 131async fn shutdown_signal(shutdown_tx: watch::Sender<bool>) { 132 let ctrl_c = async { 133 match tokio::signal::ctrl_c().await { 134 Ok(()) => {} 135 Err(e) => { 136 error!("Failed to install Ctrl+C handler: {}", e); 137 } 138 } 139 }; 140 141 #[cfg(unix)] 142 let terminate = async { 143 match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) { 144 Ok(mut signal) => { 145 signal.recv().await; 146 } 147 Err(e) => { 148 error!("Failed to install SIGTERM handler: {}", e); 149 std::future::pending::<()>().await; 150 } 151 } 152 }; 153 154 #[cfg(not(unix))] 155 let terminate = std::future::pending::<()>(); 156 157 tokio::select! { 158 _ = ctrl_c => {}, 159 _ = terminate => {}, 160 } 161 162 info!("Shutdown signal received, stopping services..."); 163 shutdown_tx.send(true).ok(); 164}