2020-03-21 20:24:05 +00:00
|
|
|
use crate::{error::MyError, jobs::JobState};
|
|
|
|
use activitystreams::primitives::XsdAnyUri;
|
|
|
|
use anyhow::Error;
|
2020-03-30 15:45:44 +00:00
|
|
|
use background_jobs::{ActixJob, Backoff, Processor};
|
2020-03-21 20:24:05 +00:00
|
|
|
use std::{future::Future, pin::Pin};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
pub struct Deliver {
|
|
|
|
to: XsdAnyUri,
|
|
|
|
data: serde_json::Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deliver {
|
|
|
|
pub fn new<T>(to: XsdAnyUri, data: T) -> Result<Self, MyError>
|
|
|
|
where
|
|
|
|
T: serde::ser::Serialize,
|
|
|
|
{
|
|
|
|
Ok(Deliver {
|
|
|
|
to,
|
|
|
|
data: serde_json::to_value(data)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct DeliverProcessor;
|
|
|
|
|
2020-03-30 15:45:44 +00:00
|
|
|
impl ActixJob for Deliver {
|
2020-03-21 20:24:05 +00:00
|
|
|
type State = JobState;
|
|
|
|
type Processor = DeliverProcessor;
|
2020-03-30 15:45:44 +00:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
2020-03-21 20:24:05 +00:00
|
|
|
|
|
|
|
fn run(self, state: Self::State) -> Self::Future {
|
2020-03-30 15:45:44 +00:00
|
|
|
Box::pin(async move {
|
|
|
|
state.requests.deliver(self.to, &self.data).await?;
|
2020-03-21 20:24:05 +00:00
|
|
|
|
2020-03-30 15:45:44 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
2020-03-21 20:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Processor for DeliverProcessor {
|
|
|
|
type Job = Deliver;
|
|
|
|
|
|
|
|
const NAME: &'static str = "DeliverProcessor";
|
|
|
|
const QUEUE: &'static str = "default";
|
2020-03-21 22:12:50 +00:00
|
|
|
const BACKOFF_STRATEGY: Backoff = Backoff::Exponential(8);
|
2020-03-21 20:24:05 +00:00
|
|
|
}
|