maps-service/validation/src/main.rs

99 lines
3.5 KiB
Rust
Raw Normal View History

use futures::StreamExt;
2024-12-03 05:09:59 +00:00
mod nats_types;
2024-12-03 05:15:10 +00:00
mod validator;
mod publish_new;
mod publish_fix;
mod message_handler;
2024-11-26 01:30:55 +00:00
2024-12-03 06:09:01 +00:00
#[allow(dead_code)]
#[derive(Debug)]
2024-12-11 02:04:02 +00:00
pub enum StartupError{
API(api::ReqwestError),
NatsConnect(async_nats::ConnectError),
2024-12-12 05:07:51 +00:00
NatsGetStream(async_nats::jetstream::context::GetStreamError),
NatsConsumer(async_nats::jetstream::stream::ConsumerError),
NatsStream(async_nats::jetstream::consumer::StreamError),
2024-12-12 01:43:01 +00:00
GRPCConnect(tonic::transport::Error),
2024-12-03 06:09:01 +00:00
}
2024-12-03 06:52:01 +00:00
impl std::fmt::Display for StartupError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for StartupError{}
2024-12-03 06:09:01 +00:00
// annoying mile-long type
pub type MapsServiceClient=rust_grpc::maps::maps_service_client::MapsServiceClient<tonic::transport::channel::Channel>;
2024-12-07 05:00:19 +00:00
pub const GROUP_STRAFESNET:u64=6980477;
pub const PARALLEL_REQUESTS:usize=16;
2024-12-03 05:15:10 +00:00
#[tokio::main]
2024-12-03 06:09:01 +00:00
async fn main()->Result<(),StartupError>{
2024-12-11 01:39:08 +00:00
// talk to roblox through STRAFESNET_CI2 account
let cookie=std::env::var("RBXCOOKIE").expect("RBXCOOKIE env required");
let cookie_context=rbx_asset::cookie::CookieContext::new(rbx_asset::cookie::Cookie::new(cookie));
2024-12-03 06:52:01 +00:00
// maps-service api
2024-12-11 01:39:08 +00:00
let api_host=std::env::var("API_HOST").expect("API_HOST env required");
let api=api::Context::new(api_host).map_err(StartupError::API)?;
2024-12-03 06:52:01 +00:00
// nats
2024-12-11 01:39:08 +00:00
let nats_host=std::env::var("NATS_HOST").expect("NATS_HOST env required");
let nats_fut=async{
let nasty=async_nats::connect(nats_host).await.map_err(StartupError::NatsConnect)?;
// use nats jetstream
async_nats::jetstream::new(nasty)
.get_stream("maptest").await.map_err(StartupError::NatsGetStream)?
.get_or_create_consumer("validation",async_nats::jetstream::consumer::pull::Config{
name:Some("validation".to_owned()),
durable_name:Some("validation".to_owned()),
filter_subject:"maptest.submissions.>".to_owned(),
..Default::default()
}).await.map_err(StartupError::NatsConsumer)?
.messages().await.map_err(StartupError::NatsStream)
};
2024-12-03 06:52:01 +00:00
2024-12-12 01:43:01 +00:00
// data-service grpc for creating map entries
let data_host=std::env::var("DATA_HOST").expect("DATA_HOST env required");
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))
};
2024-12-12 01:43:01 +00:00
2024-12-13 00:41:34 +00:00
// 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");
2024-12-13 00:41:34 +00:00
// run futures
let (mut messages,message_handler)=tokio::try_join!(nats_fut,message_handler_fut)?;
// process up to PARALLEL_REQUESTS in parallel
let main_loop=async move{
static SEM:tokio::sync::Semaphore=tokio::sync::Semaphore::const_new(PARALLEL_REQUESTS);
// use memory leak to make static lifetime
let message_handler=Box::leak(Box::new(message_handler));
// acquire a permit before attempting to receive a message, exit if either fails
while let (Ok(permit),Some(message_result))=(SEM.acquire().await,messages.next().await){
// handle the message on a new thread (mainly to decode the model file)
tokio::spawn(async{
match message_handler.handle_message_result(message_result).await{
Ok(())=>println!("[Validation] Success, hooray!"),
Err(e)=>println!("[Validation] There was an error, oopsie! {e}"),
}
// explicitly call drop to make the move semantics and permit release more obvious
core::mem::drop(permit);
});
}
};
2024-12-03 06:52:01 +00:00
// race sigkill and main loop termination and then die
tokio::select!{
_=sig_term.recv()=>(),
_=main_loop=>(),
};
2024-12-03 06:52:01 +00:00
2024-12-03 06:09:01 +00:00
Ok(())
2024-11-26 01:30:55 +00:00
}