refactor(worker): thin handlers + factory — move all business logic to application services
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
mod factory;
|
||||
mod handlers;
|
||||
|
||||
use std::sync::Arc;
|
||||
use futures::StreamExt;
|
||||
use sqlx::PgPool;
|
||||
use domain::ports::EventConsumer;
|
||||
|
||||
#[tokio::main]
|
||||
@@ -14,22 +13,12 @@ async fn main() {
|
||||
|
||||
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL required");
|
||||
let nats_url = std::env::var("NATS_URL").unwrap_or_else(|_| "nats://localhost:4222".into());
|
||||
let base_url = std::env::var("BASE_URL").expect("BASE_URL required");
|
||||
|
||||
tracing::info!("Connecting to postgres...");
|
||||
let pool = PgPool::connect(&database_url).await.expect("DB connect failed");
|
||||
|
||||
tracing::info!("Connecting to NATS at {nats_url}...");
|
||||
let nats_client = async_nats::connect(&nats_url).await.expect("NATS connect failed");
|
||||
let consumer = event_transport::EventConsumerAdapter::new(nats::NatsMessageSource::new(nats_client));
|
||||
|
||||
let notification_handler = handlers::NotificationHandler {
|
||||
thoughts: Arc::new(postgres::thought::PgThoughtRepository::new(pool.clone())),
|
||||
notifications: Arc::new(postgres::notification::PgNotificationRepository::new(pool.clone())),
|
||||
};
|
||||
let federation_handler = handlers::FederationHandler;
|
||||
tracing::info!("Building worker...");
|
||||
let (consumer, handlers) = factory::build(&database_url, &base_url, &nats_url).await;
|
||||
|
||||
tracing::info!("Worker started, consuming events...");
|
||||
|
||||
let mut stream = consumer.consume();
|
||||
while let Some(result) = stream.next().await {
|
||||
match result {
|
||||
@@ -37,20 +26,18 @@ async fn main() {
|
||||
let event = &envelope.event;
|
||||
tracing::debug!(?event, "received event");
|
||||
|
||||
let n_result = notification_handler.handle(event).await;
|
||||
let f_result = federation_handler.handle(event).await;
|
||||
let n = handlers.notification.handle(event).await;
|
||||
let f = handlers.federation.handle(event).await;
|
||||
|
||||
if n_result.is_ok() && f_result.is_ok() {
|
||||
if n.is_ok() && f.is_ok() {
|
||||
(envelope.ack)();
|
||||
} else {
|
||||
if let Err(e) = n_result { tracing::error!("notification handler error: {e}"); }
|
||||
if let Err(e) = f_result { tracing::error!("federation handler error: {e}"); }
|
||||
if let Err(e) = n { tracing::error!("notification handler: {e}"); }
|
||||
if let Err(e) = f { tracing::error!("federation handler: {e}"); }
|
||||
(envelope.nack)();
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("consumer error: {e}");
|
||||
}
|
||||
Err(e) => tracing::error!("consumer error: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user