Use ActixJob, save a few lines

This commit is contained in:
asonix
2020-03-30 10:45:44 -05:00
parent b8bc230403
commit 680ccc511c
8 changed files with 29 additions and 56 deletions
View File
+1
View File
@@ -0,0 +1 @@
mod announce;
+7 -16
View File
@@ -1,9 +1,8 @@
use crate::{error::MyError, jobs::JobState};
use activitystreams::primitives::XsdAnyUri;
use anyhow::Error;
use background_jobs::{Backoff, Job, Processor};
use background_jobs::{ActixJob, Backoff, Processor};
use std::{future::Future, pin::Pin};
use tokio::sync::oneshot;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Deliver {
@@ -21,30 +20,22 @@ impl Deliver {
data: serde_json::to_value(data)?,
})
}
async fn perform(self, state: JobState) -> Result<(), Error> {
state.requests.deliver(self.to, &self.data).await?;
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct DeliverProcessor;
impl Job for Deliver {
impl ActixJob for Deliver {
type State = JobState;
type Processor = DeliverProcessor;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
fn run(self, state: Self::State) -> Self::Future {
let (tx, rx) = oneshot::channel();
Box::pin(async move {
state.requests.deliver(self.to, &self.data).await?;
actix::spawn(async move {
let _ = tx.send(self.perform(state).await);
});
Box::pin(async move { rx.await? })
Ok(())
})
}
}
+2 -2
View File
@@ -4,7 +4,7 @@ use crate::{
};
use activitystreams::primitives::XsdAnyUri;
use anyhow::Error;
use background_jobs::{Job, Processor};
use background_jobs::{ActixJob, Processor};
use futures::future::{ready, Ready};
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
@@ -38,7 +38,7 @@ impl DeliverMany {
#[derive(Clone, Debug)]
pub struct DeliverManyProcessor;
impl Job for DeliverMany {
impl ActixJob for DeliverMany {
type State = JobState;
type Processor = DeliverManyProcessor;
type Future = Ready<Result<(), Error>>;
+4 -11
View File
@@ -1,10 +1,9 @@
use crate::{config::UrlKind, jobs::JobState};
use activitystreams::primitives::XsdAnyUri;
use anyhow::Error;
use background_jobs::{Job, Processor};
use background_jobs::{ActixJob, Processor};
use futures::join;
use std::{future::Future, pin::Pin};
use tokio::sync::oneshot;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct QueryInstance {
@@ -85,19 +84,13 @@ impl QueryInstance {
#[derive(Clone, Debug)]
pub struct InstanceProcessor;
impl Job for QueryInstance {
impl ActixJob for QueryInstance {
type State = JobState;
type Processor = InstanceProcessor;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
fn run(self, state: Self::State) -> Self::Future {
let (tx, rx) = oneshot::channel();
actix::spawn(async move {
let _ = tx.send(self.perform(state).await);
});
Box::pin(async move { rx.await? })
Box::pin(self.perform(state))
}
}
+4 -11
View File
@@ -1,9 +1,8 @@
use crate::jobs::JobState;
use activitystreams::primitives::XsdAnyUri;
use anyhow::Error;
use background_jobs::{Job, Processor};
use background_jobs::{ActixJob, Processor};
use std::{future::Future, pin::Pin};
use tokio::sync::oneshot;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct QueryNodeinfo {
@@ -56,19 +55,13 @@ impl QueryNodeinfo {
#[derive(Clone, Debug)]
pub struct NodeinfoProcessor;
impl Job for QueryNodeinfo {
impl ActixJob for QueryNodeinfo {
type State = JobState;
type Processor = NodeinfoProcessor;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
fn run(self, state: Self::State) -> Self::Future {
let (tx, rx) = oneshot::channel();
actix::spawn(async move {
let _ = tx.send(self.perform(state).await);
});
Box::pin(async move { rx.await? })
Box::pin(self.perform(state))
}
}
+4 -11
View File
@@ -1,8 +1,7 @@
use crate::jobs::{instance::QueryInstance, nodeinfo::QueryNodeinfo, JobState};
use anyhow::Error;
use background_jobs::{Job, Processor};
use background_jobs::{ActixJob, Processor};
use std::{future::Future, pin::Pin};
use tokio::sync::oneshot;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Listeners;
@@ -23,19 +22,13 @@ impl Listeners {
}
}
impl Job for Listeners {
impl ActixJob for Listeners {
type State = JobState;
type Processor = ListenersProcessor;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
fn run(self, state: Self::State) -> Self::Future {
let (tx, rx) = oneshot::channel();
actix::spawn(async move {
let _ = tx.send(self.perform(state).await);
});
Box::pin(async move { rx.await? })
Box::pin(self.perform(state))
}
}