mirror of
https://github.com/perstarkse/minne.git
synced 2026-12-06 18:56:18 +01:00
feat: refactoring complete?
This commit is contained in:
1 parent
94f328e542
commit
1e789e1153
27 files changed
+428
-338
No files matched your search
+55
-38
@@ -1,12 +1,16 @@
|
||||
use lapin::{
|
||||
message::Delivery, options::*, types::FieldTable, Channel, Consumer, Queue
|
||||
};
|
||||
use futures_lite::stream::StreamExt;
|
||||
use lapin::{message::Delivery, options::*, types::FieldTable, Channel, Consumer, Queue};
|
||||
|
||||
use crate::models::{ingress_content::IngressContentError, ingress_object::IngressObject };
|
||||
use crate::{
|
||||
error::IngressConsumerError,
|
||||
ingress::{
|
||||
content_processor::ContentProcessor,
|
||||
types::{ingress_input::IngressContentError, ingress_object::IngressObject},
|
||||
},
|
||||
};
|
||||
|
||||
use super::{RabbitMQCommon, RabbitMQCommonTrait, RabbitMQConfig, RabbitMQError};
|
||||
use tracing::{info, error};
|
||||
use tracing::{error, info};
|
||||
|
||||
/// Struct to consume messages from RabbitMQ.
|
||||
pub struct RabbitMQConsumer {
|
||||
@@ -26,18 +30,22 @@ impl RabbitMQConsumer {
|
||||
/// * `Result<Self, RabbitMQError>` - The created client or an error.
|
||||
pub async fn new(config: &RabbitMQConfig) -> Result<Self, RabbitMQError> {
|
||||
let common = RabbitMQCommon::new(config).await?;
|
||||
|
||||
|
||||
// Passively declare the exchange (it should already exist)
|
||||
common.declare_exchange(config, true).await?;
|
||||
|
||||
|
||||
// Declare queue and bind it to the channel
|
||||
let queue = Self::declare_queue(&common.channel, config).await?;
|
||||
Self::bind_queue(&common.channel, &config.exchange, &queue, config).await?;
|
||||
|
||||
|
||||
// Initialize the consumer
|
||||
let consumer = Self::initialize_consumer(&common.channel, config).await?;
|
||||
|
||||
Ok(Self { common, queue, consumer })
|
||||
Ok(Self {
|
||||
common,
|
||||
queue,
|
||||
consumer,
|
||||
})
|
||||
}
|
||||
|
||||
/// Sets up the consumer based on the channel and `RabbitMQConfig`.
|
||||
@@ -48,7 +56,10 @@ impl RabbitMQConsumer {
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<Consumer, RabbitMQError>` - The initialized consumer or error
|
||||
async fn initialize_consumer(channel: &Channel, config: &RabbitMQConfig) -> Result<Consumer, RabbitMQError> {
|
||||
async fn initialize_consumer(
|
||||
channel: &Channel,
|
||||
config: &RabbitMQConfig,
|
||||
) -> Result<Consumer, RabbitMQError> {
|
||||
channel
|
||||
.basic_consume(
|
||||
&config.queue,
|
||||
@@ -56,7 +67,8 @@ impl RabbitMQConsumer {
|
||||
BasicConsumeOptions::default(),
|
||||
FieldTable::default(),
|
||||
)
|
||||
.await.map_err(|e| RabbitMQError::InitializeConsumerError(e.to_string()))
|
||||
.await
|
||||
.map_err(|e| RabbitMQError::InitializeConsumerError(e.to_string()))
|
||||
}
|
||||
/// Declares the queue based on the channel and `RabbitMQConfig`.
|
||||
/// # Arguments
|
||||
@@ -65,7 +77,10 @@ impl RabbitMQConsumer {
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<Queue, RabbitMQError>` - The initialized queue or error
|
||||
async fn declare_queue(channel: &Channel, config: &RabbitMQConfig) -> Result<Queue, RabbitMQError> {
|
||||
async fn declare_queue(
|
||||
channel: &Channel,
|
||||
config: &RabbitMQConfig,
|
||||
) -> Result<Queue, RabbitMQError> {
|
||||
channel
|
||||
.queue_declare(
|
||||
&config.queue,
|
||||
@@ -88,7 +103,12 @@ impl RabbitMQConsumer {
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), RabbitMQError>` - Ok or error
|
||||
async fn bind_queue(channel: &Channel, exchange: &str, queue: &Queue, config: &RabbitMQConfig) -> Result<(), RabbitMQError> {
|
||||
async fn bind_queue(
|
||||
channel: &Channel,
|
||||
exchange: &str,
|
||||
queue: &Queue,
|
||||
config: &RabbitMQConfig,
|
||||
) -> Result<(), RabbitMQError> {
|
||||
channel
|
||||
.queue_bind(
|
||||
queue.name().as_str(),
|
||||
@@ -111,7 +131,11 @@ impl RabbitMQConsumer {
|
||||
/// `Delivery` - A delivery reciept, required to ack or nack the delivery.
|
||||
pub async fn consume(&self) -> Result<(IngressObject, Delivery), RabbitMQError> {
|
||||
// Receive the next message
|
||||
let delivery = self.consumer.clone().next().await
|
||||
let delivery = self
|
||||
.consumer
|
||||
.clone()
|
||||
.next()
|
||||
.await
|
||||
.ok_or_else(|| RabbitMQError::ConsumeError("No message received".to_string()))?
|
||||
.map_err(|e| RabbitMQError::ConsumeError(e.to_string()))?;
|
||||
|
||||
@@ -131,7 +155,8 @@ impl RabbitMQConsumer {
|
||||
/// # Returns
|
||||
/// * `Result<(), RabbitMQError>` - Ok or error
|
||||
pub async fn ack_delivery(&self, delivery: Delivery) -> Result<(), RabbitMQError> {
|
||||
self.common.channel
|
||||
self.common
|
||||
.channel
|
||||
.basic_ack(delivery.delivery_tag, BasicAckOptions::default())
|
||||
.await
|
||||
.map_err(|e| RabbitMQError::ConsumeError(e.to_string()))?;
|
||||
@@ -139,33 +164,22 @@ impl RabbitMQConsumer {
|
||||
Ok(())
|
||||
}
|
||||
/// Function to continually consume messages as they come in
|
||||
/// WIP
|
||||
pub async fn process_messages(&self) -> Result<(), RabbitMQError> {
|
||||
pub async fn process_messages(&self) -> Result<(), IngressConsumerError> {
|
||||
loop {
|
||||
match self.consume().await {
|
||||
Ok((ingress, delivery)) => {
|
||||
info!("Received IngressObject: {:?}", ingress);
|
||||
let text_content = ingress.to_text_content().await.unwrap();
|
||||
text_content.process().await.unwrap();
|
||||
|
||||
// Get the TextContent
|
||||
let text_content = ingress.to_text_content().await?;
|
||||
|
||||
// Initialize ContentProcessor which handles LLM analysis and storage
|
||||
let content_processor = ContentProcessor::new().await?;
|
||||
|
||||
// Begin processing of TextContent
|
||||
content_processor.process(&text_content).await?;
|
||||
|
||||
// Remove from queue
|
||||
self.ack_delivery(delivery).await?;
|
||||
// Process the IngressContent
|
||||
// match self.handle_ingress_content(&ingress).await {
|
||||
// Ok(_) => {
|
||||
// info!("Successfully handled IngressContent");
|
||||
// // Acknowledge the message
|
||||
// if let Err(e) = self.ack_delivery(delivery).await {
|
||||
// error!("Failed to acknowledge message: {:?}", e);
|
||||
// }
|
||||
// },
|
||||
// Err(e) => {
|
||||
// error!("Failed to handle IngressContent: {:?}", e);
|
||||
// // For now, we'll acknowledge to remove it from the queue. Change to nack?
|
||||
// if let Err(ack_err) = self.ack_delivery(delivery).await {
|
||||
// error!("Failed to acknowledge message after handling error: {:?}", ack_err);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Err(RabbitMQError::ConsumeError(e)) => {
|
||||
error!("Error consuming message: {}", e);
|
||||
@@ -182,7 +196,10 @@ impl RabbitMQConsumer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_ingress_content(&self, ingress: &IngressObject) -> Result<(), IngressContentError> {
|
||||
pub async fn handle_ingress_content(
|
||||
&self,
|
||||
ingress: &IngressObject,
|
||||
) -> Result<(), IngressContentError> {
|
||||
info!("Processing IngressContent: {:?}", ingress);
|
||||
|
||||
unimplemented!()
|
||||
|
||||
+23
-8
@@ -1,13 +1,16 @@
|
||||
pub mod publisher;
|
||||
pub mod consumer;
|
||||
pub mod publisher;
|
||||
|
||||
use axum::async_trait;
|
||||
use lapin::{
|
||||
options::ExchangeDeclareOptions, types::FieldTable, Channel, Connection, ConnectionProperties, ExchangeKind
|
||||
options::ExchangeDeclareOptions, types::FieldTable, Channel, Connection, ConnectionProperties,
|
||||
ExchangeKind,
|
||||
};
|
||||
use thiserror::Error;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::error::ProcessingError;
|
||||
|
||||
/// Possible errors related to RabbitMQ operations.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RabbitMQError {
|
||||
@@ -25,6 +28,8 @@ pub enum RabbitMQError {
|
||||
InitializeConsumerError(String),
|
||||
#[error("Queue error: {0}")]
|
||||
QueueError(String),
|
||||
#[error("Processing error: {0}")]
|
||||
ProcessingError(#[from] ProcessingError),
|
||||
}
|
||||
|
||||
/// Struct containing the information required to set up a client and connection.
|
||||
@@ -42,18 +47,21 @@ pub struct RabbitMQCommon {
|
||||
pub channel: Channel,
|
||||
}
|
||||
|
||||
|
||||
/// Defines the behavior for RabbitMQCommon client operations.
|
||||
#[cfg_attr(test, mockall::automock)]
|
||||
#[async_trait]
|
||||
pub trait RabbitMQCommonTrait: Send + Sync {
|
||||
async fn create_connection(config: &RabbitMQConfig) -> Result<Connection, RabbitMQError>;
|
||||
async fn declare_exchange(&self, config: &RabbitMQConfig, passive: bool) -> Result<(), RabbitMQError>;
|
||||
}
|
||||
async fn declare_exchange(
|
||||
&self,
|
||||
config: &RabbitMQConfig,
|
||||
passive: bool,
|
||||
) -> Result<(), RabbitMQError>;
|
||||
}
|
||||
|
||||
impl RabbitMQCommon {
|
||||
/// Sets up a new RabbitMQ client or error
|
||||
///
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `RabbitMQConfig` - Configuration object with required information
|
||||
///
|
||||
@@ -62,7 +70,10 @@ impl RabbitMQCommon {
|
||||
pub async fn new(config: &RabbitMQConfig) -> Result<Self, RabbitMQError> {
|
||||
let connection = Self::create_connection(config).await?;
|
||||
let channel = connection.create_channel().await?;
|
||||
Ok(Self { connection, channel })
|
||||
Ok(Self {
|
||||
connection,
|
||||
channel,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +88,11 @@ impl RabbitMQCommonTrait for RabbitMQCommon {
|
||||
}
|
||||
|
||||
/// Function to declare the exchange required
|
||||
async fn declare_exchange(&self, config: &RabbitMQConfig, passive: bool) -> Result<(), RabbitMQError> {
|
||||
async fn declare_exchange(
|
||||
&self,
|
||||
config: &RabbitMQConfig,
|
||||
passive: bool,
|
||||
) -> Result<(), RabbitMQError> {
|
||||
debug!("Declaring exchange");
|
||||
self.channel
|
||||
.exchange_declare(
|
||||
|
||||
+23
-18
@@ -1,11 +1,9 @@
|
||||
use lapin::{
|
||||
options::*, publisher_confirm::Confirmation, BasicProperties,
|
||||
};
|
||||
use lapin::{options::*, publisher_confirm::Confirmation, BasicProperties};
|
||||
|
||||
use crate::models::ingress_object::IngressObject;
|
||||
use crate::ingress::types::ingress_object::IngressObject;
|
||||
|
||||
use super::{RabbitMQCommon, RabbitMQCommonTrait, RabbitMQConfig, RabbitMQError};
|
||||
use tracing::{info, error};
|
||||
use tracing::{error, info};
|
||||
|
||||
/// Struct to publish messages to RabbitMQ.
|
||||
pub struct RabbitMQProducer {
|
||||
@@ -27,7 +25,7 @@ impl RabbitMQProducer {
|
||||
let common = RabbitMQCommon::new(config).await?;
|
||||
common.declare_exchange(config, false).await?;
|
||||
|
||||
Ok(Self {
|
||||
Ok(Self {
|
||||
common,
|
||||
exchange_name: config.exchange.clone(),
|
||||
routing_key: config.routing_key.clone(),
|
||||
@@ -39,19 +37,23 @@ impl RabbitMQProducer {
|
||||
/// # Arguments
|
||||
/// * `self` - Reference to self
|
||||
/// * `ingress_object` - A initialized IngressObject
|
||||
///
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<Confirmation, RabbitMQError>` - Confirmation of sent message or error
|
||||
pub async fn publish(&self, ingress_object: &IngressObject) -> Result<Confirmation, RabbitMQError> {
|
||||
pub async fn publish(
|
||||
&self,
|
||||
ingress_object: &IngressObject,
|
||||
) -> Result<Confirmation, RabbitMQError> {
|
||||
// Serialize IngressObject to JSON
|
||||
let payload = serde_json::to_vec(ingress_object)
|
||||
.map_err(|e| {
|
||||
error!("Serialization Error: {}", e);
|
||||
RabbitMQError::PublishError(format!("Serialization Error: {}", e))
|
||||
})?;
|
||||
|
||||
let payload = serde_json::to_vec(ingress_object).map_err(|e| {
|
||||
error!("Serialization Error: {}", e);
|
||||
RabbitMQError::PublishError(format!("Serialization Error: {}", e))
|
||||
})?;
|
||||
|
||||
// Publish the serialized payload to RabbitMQ
|
||||
let confirmation = self.common.channel
|
||||
let confirmation = self
|
||||
.common
|
||||
.channel
|
||||
.basic_publish(
|
||||
&self.exchange_name,
|
||||
&self.routing_key,
|
||||
@@ -69,9 +71,12 @@ impl RabbitMQProducer {
|
||||
error!("Publish Confirmation Error: {}", e);
|
||||
RabbitMQError::PublishError(format!("Publish Confirmation Error: {}", e))
|
||||
})?;
|
||||
|
||||
info!("Published IngressObject to exchange '{}' with routing key '{}'", self.exchange_name, self.routing_key);
|
||||
|
||||
|
||||
info!(
|
||||
"Published IngressObject to exchange '{}' with routing key '{}'",
|
||||
self.exchange_name, self.routing_key
|
||||
);
|
||||
|
||||
Ok(confirmation)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user