use sea_orm::sqlx::postgres::PgListener;
use crate::QueueError;
pub struct Listener {
inner: PgListener,
channel: String,
}
impl Listener {
pub async fn connect(url: &str, channel: &str) -> Result<Self, QueueError> {
let mut inner = PgListener::connect(url)
.await
.map_err(|e| QueueError::Backend(format!("cannot open a listener connection: {e}")))?;
inner
.listen(channel)
.await
.map_err(|e| QueueError::Backend(format!("cannot LISTEN on `{channel}`: {e}")))?;
Ok(Listener {
inner,
channel: channel.to_string(),
})
}
pub async fn recv(&mut self) -> Result<String, QueueError> {
let notification = self.inner.recv().await.map_err(|e| {
QueueError::Backend(format!("listening on `{}` failed: {e}", self.channel))
})?;
Ok(notification.payload().to_string())
}
}