use super::QueueManagerInternals as _;
use std::fmt;
use std::time::Duration;
use super::{Attempt, Job, JobOptions};
use crate::time::Moment;
use crate::{Error, Result};
#[derive(Debug, Default)]
pub struct Placement {
pub connection: Option<String>,
pub queue: Option<String>,
pub delay: Option<Moment>,
}
impl Placement {
pub fn resolve(
&self,
services: &crate::container::Services,
connection: Option<&str>,
queue: Option<&str>,
) -> Result<(String, String)> {
super::QueueManager::of(services).route(
self.connection.as_deref().or(connection),
self.queue.as_deref().or(queue),
)
}
}
pub fn options() -> JobOptions {
JobOptions::new()
.tries(3)
.backoff([Duration::from_secs(10), Duration::from_secs(60)])
}
pub fn settle<T>(attempt: &Attempt, delivered: Result<T>) -> Result<()> {
match delivered {
Ok(_) => Ok(()),
Err(error) if refused(&error) => {
attempt.fail(error);
Ok(())
}
Err(error) => Err(error),
}
}
pub fn refused(error: &Error) -> bool {
error.chain().any(|source| {
source.is::<Refused>()
|| source
.downcast_ref::<Rejection>()
.is_some_and(|rejection| rejection.refused)
|| source
.downcast_ref::<crate::http::client::Error>()
.is_some_and(crate::http::client::Error::is_permanent)
})
}
#[derive(Debug)]
pub struct Refused(#[doc(hidden)] pub String);
impl fmt::Display for Refused {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for Refused {}
#[derive(Debug)]
pub struct Rejection {
provider: &'static str,
message_kind: &'static str,
message: String,
response: crate::http::client::Error,
refused: bool,
}
impl Rejection {
pub fn new(
provider: &'static str,
message_kind: &'static str,
response: crate::http::client::Response,
message: String,
) -> Self {
Self {
provider,
message_kind,
message,
response: crate::http::client::Error::Status(Box::new(response)),
refused: false,
}
}
#[must_use]
pub fn refused(mut self) -> Self {
self.refused = true;
self
}
fn status(&self) -> u16 {
self.response
.response()
.map_or(0, |response| response.status().as_u16())
}
fn summary(&self) -> String {
format!(
"{} rejected the {} with HTTP {}",
self.provider,
self.message_kind,
self.status()
)
}
}
impl fmt::Display for Rejection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.summary(), self.message)
}
}
impl std::error::Error for Rejection {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.response)
}
}
inventory::submit! {
crate::foundation::telemetry::Summarizer(|error| {
use crate::foundation::telemetry::Summary;
if let Some(rejection) = error.downcast_ref::<Rejection>() {
return Some(Summary::new(rejection.status().to_string(), rejection.summary()));
}
error
.is::<Refused>()
.then(|| Summary::new("refused", "the provider refused the message"))
})
}
pub async fn dispatch<J: Job>(
job: J,
display_name: Option<&str>,
connection: String,
queue: String,
delay: Option<Moment>,
) -> Result<()> {
let mut dispatch = super::dispatch(job).on_connection(connection).on_queue(queue);
if let Some(name) = display_name {
dispatch = dispatch.display_name(name);
}
if let Some(delay) = delay {
dispatch = dispatch.delay(delay);
}
dispatch.await.map(drop)
}