mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-18 06:59:43 +02:00
working on a queue length
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
use lapin::{
|
||||
options::*, types::FieldTable, Channel, Consumer, Queue
|
||||
message::Delivery, options::*, types::FieldTable, Channel, Consumer, Queue
|
||||
};
|
||||
use futures_lite::stream::StreamExt;
|
||||
use tracing::info;
|
||||
|
||||
use super::{RabbitMQCommon, RabbitMQConfig, RabbitMQError};
|
||||
|
||||
pub struct RabbitMQConsumer {
|
||||
common: RabbitMQCommon,
|
||||
queue: Queue,
|
||||
pub queue: Queue,
|
||||
consumer: Consumer,
|
||||
}
|
||||
|
||||
@@ -66,19 +67,23 @@ impl RabbitMQConsumer {
|
||||
.map_err(|e| RabbitMQError::QueueError(e.to_string()))
|
||||
}
|
||||
|
||||
pub async fn consume(&self) -> Result<String, RabbitMQError> {
|
||||
pub async fn consume(&self) -> Result<(String, Delivery), RabbitMQError> {
|
||||
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()))?;
|
||||
|
||||
let message = String::from_utf8_lossy(&delivery.data).to_string();
|
||||
|
||||
Ok((message, delivery))
|
||||
}
|
||||
|
||||
pub async fn ack_delivery(&self, delivery: Delivery) -> Result<(), RabbitMQError> {
|
||||
self.common.channel
|
||||
.basic_ack(delivery.delivery_tag, BasicAckOptions::default())
|
||||
.await
|
||||
.map_err(|e| RabbitMQError::ConsumeError(e.to_string()))?;
|
||||
|
||||
Ok(message)
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,7 @@ pub mod producer;
|
||||
pub mod consumer;
|
||||
|
||||
use lapin::{
|
||||
Connection, ConnectionProperties, Channel, ExchangeKind,
|
||||
options::ExchangeDeclareOptions,
|
||||
types::FieldTable,
|
||||
options::{ExchangeDeclareOptions, QueueDeclareOptions}, types::FieldTable, Channel, Connection, ConnectionProperties, ExchangeKind, Queue
|
||||
|
||||
};
|
||||
use thiserror::Error;
|
||||
@@ -28,6 +26,7 @@ pub enum RabbitMQError {
|
||||
QueueError(String),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RabbitMQConfig {
|
||||
pub amqp_addr: String,
|
||||
pub exchange: String,
|
||||
|
||||
Reference in New Issue
Block a user