validation: connect to nats and grpc concurrently

This commit is contained in:
Quaternions 2024-12-12 17:40:40 -08:00
parent 8250686477
commit 727e358cf9

View File

@ -40,26 +40,32 @@ async fn main()->Result<(),StartupError>{
// nats // nats
let nats_host=std::env::var("NATS_HOST").expect("NATS_HOST env required"); let nats_host=std::env::var("NATS_HOST").expect("NATS_HOST env required");
let nasty=async_nats::connect(nats_host).await.map_err(StartupError::NatsConnect)?; // use nats jetstream let nats_fut=async{
let mut messages=async_nats::jetstream::new(nasty) let nasty=async_nats::connect(nats_host).await.map_err(StartupError::NatsConnect)?;
.get_stream("maptest").await.map_err(StartupError::NatsGetStream)? // use nats jetstream
.get_or_create_consumer("validation",async_nats::jetstream::consumer::pull::Config{ async_nats::jetstream::new(nasty)
name:Some("validation".to_owned()), .get_stream("maptest").await.map_err(StartupError::NatsGetStream)?
durable_name:Some("validation".to_owned()), .get_or_create_consumer("validation",async_nats::jetstream::consumer::pull::Config{
filter_subject:"maptest.submissions.>".to_owned(), name:Some("validation".to_owned()),
..Default::default() durable_name:Some("validation".to_owned()),
}).await.map_err(StartupError::NatsConsumer)? filter_subject:"maptest.submissions.>".to_owned(),
.messages().await.map_err(StartupError::NatsStream)?; ..Default::default()
}).await.map_err(StartupError::NatsConsumer)?
.messages().await.map_err(StartupError::NatsStream)
};
// data-service grpc for creating map entries // data-service grpc for creating map entries
let data_host=std::env::var("DATA_HOST").expect("DATA_HOST env required"); let data_host=std::env::var("DATA_HOST").expect("DATA_HOST env required");
let maps_grpc=crate::MapsServiceClient::connect(data_host).await.map_err(StartupError::GRPCConnect)?; let message_handler_fut=async{
let maps_grpc=crate::MapsServiceClient::connect(data_host).await.map_err(StartupError::GRPCConnect)?;
Ok(message_handler::MessageHandler::new(cookie_context,api,maps_grpc))
};
// Create a signal listener for SIGTERM // Create a signal listener for SIGTERM
let mut sig_term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("Failed to create SIGTERM signal listener"); let mut sig_term=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("Failed to create SIGTERM signal listener");
// the guy // run futures
let message_handler=message_handler::MessageHandler::new(cookie_context,api,maps_grpc); let (mut messages,message_handler)=tokio::try_join!(nats_fut,message_handler_fut)?;
// nats consumer thread // nats consumer thread
tokio::spawn(async move{ tokio::spawn(async move{