chore: switch to lapin (#767)
* chore: begin switching to lapin fully Signed-off-by: Zomatree <me@zomatree.live> * chore: update rest of pushd to lapin Signed-off-by: Zomatree <me@zomatree.live> * chore: cleanup code Signed-off-by: Zomatree <me@zomatree.live> * chore: cleanup code Signed-off-by: Zomatree <me@zomatree.live> * fix: github webui sucks Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> --------- Signed-off-by: Zomatree <me@zomatree.live> Signed-off-by: Tom <iamtomahawkx@gmail.com> Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> Co-authored-by: Tom <iamtomahawkx@gmail.com> Release-As: 0.13.6
This commit is contained in:
91
crates/daemons/pushd/src/utils/consumer.rs
Normal file
91
crates/daemons/pushd/src/utils/consumer.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use std::{
|
||||
future::{ready, Future},
|
||||
pin::Pin,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{
|
||||
message::{Delivery, DeliveryResult},
|
||||
options::BasicPublishOptions,
|
||||
BasicProperties, Channel, Connection, ConsumerDelegate, Error as AMQPError,
|
||||
};
|
||||
use log::debug;
|
||||
use revolt_database::Database;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Consumer: Clone + Send + Sync + 'static {
|
||||
async fn create(
|
||||
db: Database,
|
||||
authifier_db: authifier::Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self;
|
||||
fn channel(&self) -> &Arc<Channel>;
|
||||
async fn consume(&self, delivery: Delivery) -> Result<()>;
|
||||
|
||||
async fn publish_message_with_options(
|
||||
&self,
|
||||
payload: &[u8],
|
||||
exchange: &str,
|
||||
routing_key: &str,
|
||||
options: BasicPublishOptions,
|
||||
properties: BasicProperties,
|
||||
) -> Result<(), AMQPError> {
|
||||
let channel = self.channel();
|
||||
|
||||
channel
|
||||
.basic_publish(
|
||||
exchange.into(),
|
||||
routing_key.into(),
|
||||
options,
|
||||
payload,
|
||||
properties,
|
||||
)
|
||||
.await?;
|
||||
debug!("Sent message to queue for target {}", routing_key);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn publish_message(
|
||||
&self,
|
||||
payload: &[u8],
|
||||
exchange: &str,
|
||||
routing_key: &str,
|
||||
) -> Result<(), AMQPError> {
|
||||
self.publish_message_with_options(
|
||||
payload,
|
||||
exchange,
|
||||
routing_key,
|
||||
BasicPublishOptions::default(),
|
||||
BasicProperties::default(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Delegate<C: Consumer>(pub C);
|
||||
|
||||
impl<C: Consumer> ConsumerDelegate for Delegate<C> {
|
||||
fn on_new_delivery(
|
||||
&self,
|
||||
delivery: DeliveryResult,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
||||
match delivery {
|
||||
Ok(Some(delivery)) => {
|
||||
let consumer = self.0.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
if let Err(e) = consumer.consume(delivery).await {
|
||||
revolt_config::capture_anyhow(&e);
|
||||
log::error!("{e:?}");
|
||||
};
|
||||
})
|
||||
}
|
||||
Ok(None) => Box::pin(ready(())),
|
||||
Err(e) => Box::pin(async move { log::error!("Received bad delivery: {e:?}") }),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user