mod batches;
mod database;
mod memory;
use std::sync::Arc;
pub(crate) use database::DatabaseStore;
pub(crate) use memory::MemoryStore;
use chrono::{DateTime, Utc};
use navree_contracts::{BoxFuture, JobEnvelope};
use navree_db::Uow;
use crate::batch::{BatchRow, Finish, Finished};
use crate::record::{
FailedJob, FailedSummary, NewJob, PendingJob, PendingQuery, QueueStatus, ReservedJob,
RetryTarget, from_millis,
};
use crate::{QueueError, QueueFake};
pub(crate) type PrepareCallback<'a> =
dyn Fn(JobEnvelope, &BatchRow) -> Result<NewJob, QueueError> + Send + Sync + 'a;
pub trait QueueStore: Send + Sync + 'static {
fn insert(&self, job: NewJob) -> BoxFuture<'_, Result<String, QueueError>>;
fn reserve<'a>(
&'a self,
queue: &'a str,
worker: &'a str,
now: i64,
) -> BoxFuture<'a, Result<Option<ReservedJob>, QueueError>>;
fn complete<'a>(&'a self, job: &'a ReservedJob) -> BoxFuture<'a, Result<(), QueueError>>;
fn release<'a>(
&'a self,
job: &'a ReservedJob,
worker: &'a str,
available_at: i64,
) -> BoxFuture<'a, Result<bool, QueueError>>;
fn fail<'a>(
&'a self,
job: &'a ReservedJob,
worker: &'a str,
error: &'a str,
now: i64,
) -> BoxFuture<'a, Result<bool, QueueError>>;
fn failed(&self) -> BoxFuture<'_, Result<Vec<FailedJob>, QueueError>>;
fn failed_summary(
&self,
since: DateTime<Utc>,
limit: usize,
) -> BoxFuture<'_, Result<FailedSummary, QueueError>> {
Box::pin(async move { Ok(FailedSummary::of(&self.failed().await?, since, limit)) })
}
fn retry<'a>(
&'a self,
target: &'a RetryTarget,
now: i64,
) -> BoxFuture<'a, Result<u64, QueueError>>;
fn flush_failed(&self) -> BoxFuture<'_, Result<u64, QueueError>>;
fn pending_count<'a>(&'a self, queue: &'a str) -> BoxFuture<'a, Result<u64, QueueError>>;
}
pub(crate) enum Store {
Database(DatabaseStore),
Memory(MemoryStore),
Fake(QueueFake),
Custom(&'static str, Arc<dyn QueueStore>),
}
impl std::fmt::Debug for Store {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Store::Database(db) => f.debug_tuple("Database").field(db).finish(),
Store::Memory(memory) => f.debug_tuple("Memory").field(memory).finish(),
Store::Fake(fake) => f.debug_tuple("Fake").field(fake).finish(),
Store::Custom(name, _) => f.debug_tuple("Custom").field(name).finish(),
}
}
}
impl Store {
pub(crate) fn name(&self) -> &'static str {
match self {
Store::Database(_) => "Database",
Store::Memory(_) => "Memory",
Store::Fake(_) => "Fake",
Store::Custom(name, _) => name,
}
}
pub(crate) async fn insert(&self, job: NewJob) -> Result<String, QueueError> {
match self {
Store::Database(db) => db.insert(&job).await,
Store::Memory(memory) => Ok(memory.insert(job)),
Store::Fake(fake) => {
let id = job.id.clone();
fake.record(envelope_of(job));
Ok(id)
}
Store::Custom(_, store) => store.insert(job).await,
}
}
pub(crate) async fn reserve(
&self,
queue: &str,
worker: &str,
now: i64,
) -> Result<Option<ReservedJob>, QueueError> {
match self {
Store::Database(db) => db.reserve(queue, worker, now).await,
Store::Memory(memory) => Ok(memory.reserve(queue, worker, now)),
Store::Fake(_) => Ok(None),
Store::Custom(_, store) => store.reserve(queue, worker, now).await,
}
}
pub(crate) async fn complete(&self, job: &ReservedJob) -> Result<(), QueueError> {
match self {
Store::Database(db) => db.complete(&job.id).await,
Store::Memory(memory) => {
memory.complete(&job.id);
Ok(())
}
Store::Fake(_) => Ok(()),
Store::Custom(_, store) => store.complete(job).await,
}
}
pub(crate) async fn release(
&self,
job: &ReservedJob,
worker: &str,
available_at: i64,
) -> Result<bool, QueueError> {
match self {
Store::Database(db) => db.release(&job.id, worker, available_at).await,
Store::Memory(memory) => Ok(memory.release(&job.id, worker, available_at)),
Store::Fake(_) => Ok(false),
Store::Custom(_, store) => store.release(job, worker, available_at).await,
}
}
pub(crate) async fn fail(
&self,
job: &ReservedJob,
worker: &str,
error: &str,
now: i64,
) -> Result<bool, QueueError> {
match self {
Store::Database(db) => db.fail(job, worker, error, now).await,
Store::Memory(memory) => Ok(memory.fail(job, worker, error, now)),
Store::Fake(_) => Ok(false),
Store::Custom(_, store) => store.fail(job, worker, error, now).await,
}
}
pub(crate) async fn failed_summary(
&self,
since: DateTime<Utc>,
limit: usize,
) -> Result<FailedSummary, QueueError> {
match self {
Store::Database(db) => db.failed_summary(since, limit).await,
Store::Memory(memory) => Ok(FailedSummary::of(&memory.failed(), since, limit)),
Store::Fake(_) => Ok(FailedSummary::default()),
Store::Custom(_, store) => store.failed_summary(since, limit).await,
}
}
pub(crate) async fn failed(&self) -> Result<Vec<FailedJob>, QueueError> {
match self {
Store::Database(db) => db.failed().await,
Store::Memory(memory) => Ok(memory.failed()),
Store::Fake(_) => Ok(Vec::new()),
Store::Custom(_, store) => store.failed().await,
}
}
pub(crate) async fn retry(&self, target: &RetryTarget, now: i64) -> Result<u64, QueueError> {
let retried = match self {
Store::Database(db) => db.retry(target, now).await?,
Store::Memory(memory) => memory.retry(target, now),
Store::Fake(_) => 0,
Store::Custom(_, store) => store.retry(target, now).await?,
};
match target {
RetryTarget::Id(id) if retried == 0 => {
Err(QueueError::FailedJobNotFound { id: id.clone() })
}
_ => Ok(retried),
}
}
pub(crate) async fn flush_failed(&self) -> Result<u64, QueueError> {
match self {
Store::Database(db) => db.flush_failed().await,
Store::Memory(memory) => Ok(memory.flush_failed()),
Store::Fake(_) => Ok(0),
Store::Custom(_, store) => store.flush_failed().await,
}
}
pub(crate) async fn pending_count(&self, queue: &str) -> Result<u64, QueueError> {
match self {
Store::Database(db) => db.pending_count(queue).await,
Store::Memory(memory) => Ok(memory.pending_count(queue)),
Store::Fake(fake) => Ok(fake.count_on(queue)),
Store::Custom(_, store) => store.pending_count(queue).await,
}
}
pub(crate) async fn status(&self, queue: &str, now: i64) -> Result<QueueStatus, QueueError> {
match self {
Store::Database(db) => db.status(queue, now).await,
Store::Memory(memory) => Ok(memory.status(queue, now)),
Store::Fake(fake) => {
let waiting = fake.count_on(queue);
Ok(QueueStatus {
queue: queue.to_owned(),
waiting,
delayed: 0,
running: 0,
oldest_available_at: (waiting > 0).then(|| from_millis(now)),
at: from_millis(now),
})
}
Store::Custom(_, store) => Ok(QueueStatus {
queue: queue.to_owned(),
waiting: store.pending_count(queue).await?,
delayed: 0,
running: 0,
oldest_available_at: None,
at: from_millis(now),
}),
}
}
pub(crate) async fn pending_jobs(
&self,
query: &PendingQuery,
now: i64,
) -> Result<Vec<PendingJob>, QueueError> {
match self {
Store::Database(db) => db.pending_jobs(query, now).await,
Store::Memory(memory) => Ok(memory.pending_jobs(query, now)),
Store::Fake(_) | Store::Custom(..) => Ok(Vec::new()),
}
}
pub(crate) async fn pending_job(
&self,
id: &str,
now: i64,
) -> Result<Option<PendingJob>, QueueError> {
match self {
Store::Database(db) => db.pending_job(id, now).await,
Store::Memory(memory) => Ok(memory.pending_job(id, now)),
Store::Fake(_) | Store::Custom(..) => Ok(None),
}
}
pub(crate) async fn failed_job(&self, id: &str) -> Result<Option<FailedJob>, QueueError> {
match self {
Store::Database(db) => db.failed_job(id).await,
Store::Memory(memory) => Ok(memory.failed_job(id)),
Store::Fake(_) => Ok(None),
Store::Custom(_, store) => {
Ok(store.failed().await?.into_iter().find(|job| job.id == id))
}
}
}
pub(crate) async fn forget(&self, id: &str) -> Result<(), QueueError> {
let deleted = match self {
Store::Database(db) => db.forget(id).await?,
Store::Memory(memory) => memory.forget(id),
Store::Fake(_) => false,
Store::Custom(name, _) => {
return Err(QueueError::Store(
format!("the {name} queue driver cannot forget one failed job").into(),
));
}
};
if deleted {
Ok(())
} else {
Err(QueueError::FailedJobNotFound { id: id.to_owned() })
}
}
pub(crate) async fn insert_batch(
&self,
row: BatchRow,
jobs: Vec<NewJob>,
) -> Result<(), QueueError> {
match self {
Store::Database(db) => db.insert_batch(&row, &jobs).await,
Store::Memory(memory) => {
memory.insert_batch(row, jobs);
Ok(())
}
Store::Fake(fake) => {
let (callbacks, members): (Vec<_>, Vec<_>) =
jobs.into_iter().partition(|job| job.membership.callback);
fake.record_batch(row, members.into_iter().map(envelope_of).collect());
for callback in callbacks {
fake.record(envelope_of(callback));
}
Ok(())
}
Store::Custom(name, _) => Err(no_batches(name)),
}
}
pub(crate) async fn insert_batch_in(
&self,
uow: &Uow,
row: &BatchRow,
jobs: &[NewJob],
) -> Result<(), QueueError> {
match self {
Store::Database(db) => db.insert_batch_in(uow, row, jobs).await,
Store::Memory(_) | Store::Fake(_) => Ok(()),
Store::Custom(name, _) => Err(no_batches(name)),
}
}
pub(crate) async fn finish_batch_job(
&self,
job: &ReservedJob,
worker: &str,
finish: Finish,
error: &str,
now: i64,
prepare: &PrepareCallback<'_>,
) -> Result<Finished, QueueError> {
match self {
Store::Database(db) => {
db.finish_batch_job(job, worker, finish, error, now, prepare)
.await
}
Store::Memory(memory) => {
memory.finish_batch_job(job, worker, finish, error, now, prepare)
}
Store::Fake(_) => Ok(Finished::Lost),
Store::Custom(name, _) => Err(no_batches(name)),
}
}
pub(crate) async fn find_batch(&self, id: &str) -> Result<Option<BatchRow>, QueueError> {
match self {
Store::Database(db) => db.find_batch(id).await,
Store::Memory(memory) => Ok(memory.find_batch(id)),
Store::Fake(fake) => Ok(fake.find_batch(id)),
Store::Custom(..) => Ok(None),
}
}
pub(crate) async fn batches(&self, limit: usize) -> Result<Vec<BatchRow>, QueueError> {
match self {
Store::Database(db) => db.batches(limit).await,
Store::Memory(memory) => Ok(memory.batches(limit)),
Store::Fake(fake) => Ok(fake.batch_rows(limit)),
Store::Custom(..) => Ok(Vec::new()),
}
}
pub(crate) async fn cancel_batch(
&self,
id: &str,
now: i64,
) -> Result<Option<BatchRow>, QueueError> {
match self {
Store::Database(db) => db.cancel_batch(id, now).await,
Store::Memory(memory) => Ok(memory.cancel_batch(id, now)),
Store::Fake(fake) => Ok(fake.cancel_batch(id, now)),
Store::Custom(..) => Ok(None),
}
}
pub(crate) async fn prune_batches(&self, before: i64) -> Result<u64, QueueError> {
match self {
Store::Database(db) => db.prune_batches(before).await,
Store::Memory(memory) => Ok(memory.prune_batches(before)),
Store::Fake(_) | Store::Custom(..) => Ok(0),
}
}
pub(crate) async fn count_for_operation(&self, operation_id: &str) -> Result<u64, QueueError> {
match self {
Store::Database(db) => db.count_for_operation(operation_id).await,
Store::Memory(memory) => Ok(memory.count_for_operation(operation_id)),
Store::Fake(_) => Ok(0),
Store::Custom(..) => Ok(1),
}
}
}
fn no_batches(driver: &str) -> QueueError {
QueueError::Store(format!("the {driver} queue driver does not support batches").into())
}
pub(crate) fn envelope_of(job: NewJob) -> JobEnvelope {
let delay_ms = job.available_at.saturating_sub(job.created_at).max(0);
let delay_secs = u64::try_from(delay_ms.div_euclid(1000) + i64::from(delay_ms % 1000 != 0))
.unwrap_or_default();
JobEnvelope {
job_type: job.job_type,
payload: job.payload,
options: job.options,
delay_secs: (delay_secs > 0).then_some(delay_secs),
unique_key: job.unique_key,
}
}