backie/src/asynk/async_queue.rs

136 lines
4.3 KiB
Rust

use crate::asynk::AsyncRunnable;
use bb8_postgres::bb8::Pool;
use bb8_postgres::bb8::RunError;
use bb8_postgres::tokio_postgres::tls::MakeTlsConnect;
use bb8_postgres::tokio_postgres::tls::TlsConnect;
use bb8_postgres::tokio_postgres::types::ToSql;
use bb8_postgres::tokio_postgres::Socket;
use bb8_postgres::tokio_postgres::Transaction;
use bb8_postgres::PostgresConnectionManager;
use thiserror::Error;
use typed_builder::TypedBuilder;
#[derive(Debug, Error)]
pub enum AsyncQueueError {
#[error(transparent)]
PoolError(#[from] RunError<bb8_postgres::tokio_postgres::Error>),
#[error(transparent)]
PgError(#[from] bb8_postgres::tokio_postgres::Error),
#[error("returned invalid result (expected {expected:?}, found {found:?})")]
ResultError { expected: u64, found: u64 },
#[error("Queue doesn't have a connection")]
PoolAndTransactionEmpty,
}
#[derive(TypedBuilder)]
pub struct AsyncQueue<'a, Tls>
where
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
<Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
<Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
<<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
#[builder(default, setter(into))]
pool: Option<Pool<PostgresConnectionManager<Tls>>>,
#[builder(default, setter(into))]
transaction: Option<Transaction<'a>>,
}
const INSERT_TASK_QUERY: &str = include_str!("queries/insert_task.sql");
impl<'a, Tls> AsyncQueue<'a, Tls>
where
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
<Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
<Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
<<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
pub fn new(pool: Pool<PostgresConnectionManager<Tls>>) -> Self {
AsyncQueue::builder().pool(pool).build()
}
pub fn new_with_transaction(transaction: Transaction<'a>) -> Self {
AsyncQueue::builder().transaction(transaction).build()
}
pub async fn insert_task(&mut self, task: &dyn AsyncRunnable) -> Result<u64, AsyncQueueError> {
let json_task = serde_json::to_value(task).unwrap();
let task_type = task.task_type();
self.execute_one(INSERT_TASK_QUERY, &[&json_task, &task_type])
.await
}
async fn execute_one(
&mut self,
query: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<u64, AsyncQueueError> {
let result = if let Some(pool) = &self.pool {
let connection = pool.get().await?;
connection.execute(query, params).await?
} else if let Some(transaction) = &self.transaction {
transaction.execute(query, params).await?
} else {
return Err(AsyncQueueError::PoolAndTransactionEmpty);
};
if result != 1 {
return Err(AsyncQueueError::ResultError {
expected: 1,
found: result,
});
}
Ok(result)
}
}
#[cfg(test)]
mod async_queue_tests {
use super::AsyncQueue;
use crate::asynk::AsyncRunnable;
use crate::asynk::Error;
use async_trait::async_trait;
use bb8_postgres::bb8::Pool;
use bb8_postgres::tokio_postgres::Client;
use bb8_postgres::tokio_postgres::NoTls;
use bb8_postgres::PostgresConnectionManager;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Job {
pub number: u16,
}
#[typetag::serde]
#[async_trait]
impl AsyncRunnable for Job {
async fn run(&self, _connection: &Client) -> Result<(), Error> {
Ok(())
}
}
#[tokio::test]
async fn insert_task_creates_new_task() {
let pool = pool().await;
let mut connection = pool.get().await.unwrap();
let transaction = connection.transaction().await.unwrap();
let mut queue = AsyncQueue::<NoTls>::new_with_transaction(transaction);
let result = queue.insert_task(&Job { number: 1 }).await.unwrap();
assert_eq!(1, result);
}
async fn pool() -> Pool<PostgresConnectionManager<NoTls>> {
let pg_mgr = PostgresConnectionManager::new_from_stringlike(
"postgres://postgres:postgres@localhost/fang",
NoTls,
)
.unwrap();
Pool::builder().build(pg_mgr).await.unwrap()
}
}