use boxfuture for readability

This commit is contained in:
phiresky 2023-07-30 18:16:48 +00:00
parent 14479cefd2
commit 59f08d81c9
2 changed files with 9 additions and 15 deletions

View File

@ -6,7 +6,6 @@ use activitypub_federation::config::FederationConfig;
use chrono::{Local, Timelike};
use clap::Parser;
use federation_queue_state::FederationQueueState;
use futures::Future;
use lemmy_db_schema::{
source::instance::Instance,
utils::{ActualDbPool, DbPool},
@ -114,7 +113,7 @@ pub fn start_stop_federation_workers_cancellable(
opts: Opts,
pool: ActualDbPool,
config: FederationConfig<impl Clone + Send + Sync + 'static>,
) -> CancellableTask<(), impl Future<Output = anyhow::Result<()>>> {
) -> CancellableTask<()> {
CancellableTask::spawn(WORKER_EXIT_TIMEOUT, move |c| {
start_stop_federation_workers(opts, pool, config, c)
})

View File

@ -23,28 +23,23 @@ use serde_json::Value;
use std::{
borrow::{Borrow, Cow},
future::Future,
pin::Pin,
sync::Arc,
time::Duration,
};
use tokio::{task::JoinHandle, time::sleep};
use tokio_util::sync::CancellationToken;
pub struct CancellableTask<R: Send + 'static, F>
where
F: Future<Output = Result<R>>,
{
f: F,
pub struct CancellableTask<R: Send + 'static> {
f: Pin<Box<dyn Future<Output = Result<R, anyhow::Error>> + Send + 'static>>,
}
impl<R: Send + 'static, F> CancellableTask<R, F>
where
F: Future<Output = Result<R>>,
{
impl<R: Send + 'static> CancellableTask<R> {
/// spawn a task but with graceful shutdown
pub fn spawn(
pub fn spawn<F>(
timeout: Duration,
task: impl FnOnce(CancellationToken) -> F,
) -> CancellableTask<R, impl Future<Output = Result<R>>>
) -> CancellableTask<R>
where
F: Future<Output = Result<R>> + Send + 'static,
{
@ -62,7 +57,7 @@ where
});
let abort = task.abort_handle();
CancellableTask {
f: async move {
f: Box::pin(async move {
stop.cancel();
tokio::select! {
r = task => {
@ -74,7 +69,7 @@ where
Err(anyhow!("task aborted due to timeout"))
}
}
},
}),
}
}