serve.rs
1 use tokio_util::sync::CancellationToken; 2 3 use crate::config::Config; 4 5 #[derive(Debug, clap::Subcommand)] 6 pub enum ServeCommand { 7 // Serve everything 8 Everything {}, 9 } 10 11 impl ServeCommand { 12 #[allow(clippy::too_many_arguments)] 13 pub async fn run( 14 self, 15 process_state: &crate::systemd::ProcessState, 16 _config: &Config, 17 cancellation_token: CancellationToken, 18 instance_handle: distrox_instance::handle::InstanceHandle, 19 _database: distrox_persistence_diesel::database::Database, 20 _connection_handlers: distrox_network::connection::Services, 21 ) -> Result<(), ServeCommandError> { 22 match self { 23 Self::Everything {} => { 24 tracing::info!(instance_id = %instance_handle.endpoint_id(), "Started instance"); 25 let endpoint_addr = instance_handle.endpoint_address(); 26 for addr in endpoint_addr.addrs.iter() { 27 tracing::info!(addr = ?addr); 28 } 29 30 process_state.set_running(); 31 32 match tokio::signal::ctrl_c().await { 33 Ok(()) => { 34 tracing::info!("Cancelling ongoing operations"); 35 cancellation_token.cancel(); 36 37 tracing::info!("Shutting down instance"); 38 instance_handle 39 .shutdown() 40 .await 41 .map_err(ServeCommandError::InstanceShutdown)?; 42 } 43 Err(error) => { 44 tracing::error!( 45 ?error, 46 "Failed to wait for CTRL-C, shutting down instance" 47 ); 48 cancellation_token.cancel(); 49 instance_handle 50 .shutdown() 51 .await 52 .map_err(ServeCommandError::InstanceShutdown)?; 53 } 54 } 55 56 Ok(()) 57 } 58 } 59 } 60 } 61 62 #[derive(Debug, thiserror::Error)] 63 pub enum ServeCommandError { 64 #[error("Public Key not valid, too short: {} (expected {} bytes)", distrox_multihash::utils::multihash_to_display(&hash.into_multihash()), .expected)] 65 PublicKeyLength { 66 hash: distrox_multihash::cli::node::CliNodeId, 67 expected: usize, 68 }, 69 70 #[error("Failed to parse public key: {}", distrox_multihash::utils::multihash_to_display(&hash.into_multihash()))] 71 ParsingPublicKey { 72 hash: distrox_multihash::cli::node::CliNodeId, 73 #[source] 74 source: iroh::KeyParsingError, 75 }, 76 77 #[error(transparent)] 78 Config(#[from] crate::config::ConfigError), 79 80 #[error("Loading database failed")] 81 LoadingDatabase(#[source] crate::error::Error), 82 83 #[error("Loading signing key from '{}' failed", .path)] 84 LoadingSecretKey { 85 path: camino::Utf8PathBuf, 86 87 #[source] 88 source: std::io::Error, 89 }, 90 91 #[error("Failed to start distrox instance")] 92 StartingInstance(#[source] distrox_instance::error::Error), 93 94 #[error("Failed to shutdown distrox instance")] 95 InstanceShutdown(#[source] distrox_instance::error::Error), 96 }