use super::rows::InboxRow;
pub(in crate::inbox) struct FindRowParams<'a> {
pub(in crate::inbox) scope: &'a str,
pub(in crate::inbox) message_id: uuid::Uuid,
}
pub(in crate::inbox) async fn find_row<'e>(
executor: impl sqlx::PgExecutor<'e>,
params: FindRowParams<'_>,
) -> Result<Option<InboxRow>, sqlx::Error> {
sqlx::query_as!(
InboxRow,
r#"SELECT id, scope, message_id, message_type, message_version, conversation_id,
correlation_id, causation_id, received_at, updated_at, completed_at, dead_at,
attempts, last_error
FROM inbox WHERE scope = $1 AND message_id = $2"#,
params.scope,
params.message_id,
)
.fetch_optional(executor)
.await
}
pub(in crate::inbox) struct PurgeCompletedRowsParams {
pub(in crate::inbox) retention_ms: i64,
pub(in crate::inbox) batch_size: i64,
}
pub(in crate::inbox) async fn purge_completed_rows<'e>(
executor: impl sqlx::PgExecutor<'e>,
params: PurgeCompletedRowsParams,
) -> Result<u64, sqlx::Error> {
let result = sqlx::query!(
r#"DELETE FROM inbox WHERE id IN (
SELECT id FROM inbox
WHERE completed_at IS NOT NULL
AND completed_at < now() - ($1::bigint * interval '1 millisecond')
LIMIT $2)
AND completed_at IS NOT NULL
AND completed_at < now() - ($1::bigint * interval '1 millisecond')"#,
params.retention_ms,
params.batch_size,
)
.execute(executor)
.await?;
Ok(result.rows_affected())
}
pub(in crate::inbox) struct PurgeIncompleteRowsParams {
pub(in crate::inbox) retention_ms: i64,
pub(in crate::inbox) batch_size: i64,
}
pub(in crate::inbox) async fn purge_incomplete_rows<'e>(
executor: impl sqlx::PgExecutor<'e>,
params: PurgeIncompleteRowsParams,
) -> Result<u64, sqlx::Error> {
let result = sqlx::query!(
r#"DELETE FROM inbox WHERE id IN (
SELECT id FROM inbox
WHERE completed_at IS NULL AND dead_at IS NULL
AND updated_at < now() - ($1::bigint * interval '1 millisecond')
LIMIT $2)
AND completed_at IS NULL AND dead_at IS NULL
AND updated_at < now() - ($1::bigint * interval '1 millisecond')"#,
params.retention_ms,
params.batch_size,
)
.execute(executor)
.await?;
Ok(result.rows_affected())
}
pub(in crate::inbox) struct PurgeDeadRetentionRowsParams {
pub(in crate::inbox) retention_ms: i64,
pub(in crate::inbox) batch_size: i64,
}
pub(in crate::inbox) async fn purge_dead_retention_rows<'e>(
executor: impl sqlx::PgExecutor<'e>,
params: PurgeDeadRetentionRowsParams,
) -> Result<u64, sqlx::Error> {
let result = sqlx::query!(
r#"DELETE FROM inbox WHERE id IN (
SELECT id FROM inbox
WHERE dead_at IS NOT NULL
AND dead_at < now() - ($1::bigint * interval '1 millisecond')
LIMIT $2)
AND dead_at IS NOT NULL
AND dead_at < now() - ($1::bigint * interval '1 millisecond')"#,
params.retention_ms,
params.batch_size,
)
.execute(executor)
.await?;
Ok(result.rows_affected())
}