relay/src/jobs/deliver.rs

71 lines
2 KiB
Rust
Raw Normal View History

use crate::{
error::Error,
jobs::{debug_object, JobState},
requests::BreakerStrategy,
};
2022-01-17 22:54:45 +00:00
use activitystreams::iri_string::types::IriString;
2020-04-21 00:56:50 +00:00
use background_jobs::{ActixJob, Backoff};
use std::{future::Future, pin::Pin};
2021-09-21 19:32:25 +00:00
#[derive(Clone, serde::Deserialize, serde::Serialize)]
2021-02-10 04:17:20 +00:00
pub(crate) struct Deliver {
2022-01-17 22:54:45 +00:00
to: IriString,
data: serde_json::Value,
}
2021-09-21 19:32:25 +00:00
impl std::fmt::Debug for Deliver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Deliver")
.field("to", &self.to.to_string())
2022-11-13 19:57:52 +00:00
.field("activity", &self.data["type"])
.field("object", debug_object(&self.data))
2021-09-21 19:32:25 +00:00
.finish()
}
}
impl Deliver {
2022-01-17 22:54:45 +00:00
pub(crate) fn new<T>(to: IriString, data: T) -> Result<Self, Error>
where
T: serde::ser::Serialize,
{
Ok(Deliver {
to,
data: serde_json::to_value(data)?,
})
}
2021-09-18 17:55:39 +00:00
2022-11-01 20:57:33 +00:00
#[tracing::instrument(name = "Deliver", skip(state))]
2021-09-18 17:55:39 +00:00
async fn permform(self, state: JobState) -> Result<(), Error> {
if let Err(e) = state
.state
.requests
.deliver(&self.to, &self.data, BreakerStrategy::Allow401AndBelow)
.await
{
2022-11-16 01:56:13 +00:00
if e.is_breaker() {
tracing::debug!("Not trying due to failed breaker");
return Ok(());
}
if e.is_bad_request() {
tracing::debug!("Server didn't understand the activity");
return Ok(());
}
2022-11-16 01:56:13 +00:00
return Err(e);
}
2021-09-18 17:55:39 +00:00
Ok(())
}
}
2020-03-30 15:45:44 +00:00
impl ActixJob for Deliver {
type State = JobState;
2021-09-18 17:55:39 +00:00
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
2020-04-21 01:03:46 +00:00
const NAME: &'static str = "relay::jobs::Deliver";
2022-11-20 03:32:45 +00:00
const QUEUE: &'static str = "deliver";
2020-04-21 00:56:50 +00:00
const BACKOFF: Backoff = Backoff::Exponential(8);
fn run(self, state: Self::State) -> Self::Future {
2021-09-18 17:55:39 +00:00
Box::pin(async move { self.permform(state).await.map_err(Into::into) })
}
}