2020-04-11 16:51:11 +00:00
|
|
|
use core::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::Arc;
|
2021-03-15 22:14:12 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use futures::future::*;
|
|
|
|
use futures::select;
|
|
|
|
use tokio::sync::{mpsc, watch, Mutex};
|
2020-04-11 16:51:11 +00:00
|
|
|
|
|
|
|
use crate::error::Error;
|
|
|
|
|
|
|
|
type JobOutput = Result<(), Error>;
|
|
|
|
type Job = Pin<Box<dyn Future<Output = JobOutput> + Send>>;
|
|
|
|
|
|
|
|
pub struct BackgroundRunner {
|
2020-04-16 16:41:10 +00:00
|
|
|
pub stop_signal: watch::Receiver<bool>,
|
2020-04-11 16:51:11 +00:00
|
|
|
|
2021-03-15 22:14:12 +00:00
|
|
|
queue_in: mpsc::UnboundedSender<(Job, bool)>,
|
|
|
|
worker_in: mpsc::UnboundedSender<tokio::task::JoinHandle<()>>,
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BackgroundRunner {
|
2021-03-15 22:14:12 +00:00
|
|
|
pub fn new(
|
|
|
|
n_runners: usize,
|
|
|
|
stop_signal: watch::Receiver<bool>,
|
|
|
|
) -> (Arc<Self>, tokio::task::JoinHandle<()>) {
|
|
|
|
let (worker_in, mut worker_out) = mpsc::unbounded_channel();
|
|
|
|
|
|
|
|
let stop_signal_2 = stop_signal.clone();
|
|
|
|
let await_all_done = tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
let wkr = {
|
|
|
|
select! {
|
|
|
|
item = worker_out.recv().fuse() => {
|
|
|
|
match item {
|
|
|
|
Some(x) => x,
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 14:58:40 +00:00
|
|
|
_ = tokio::time::sleep(Duration::from_secs(5)).fuse() => {
|
2021-03-15 22:14:12 +00:00
|
|
|
if *stop_signal_2.borrow() {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Err(e) = wkr.await {
|
|
|
|
error!("Error while awaiting for worker: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-03-15 21:36:41 +00:00
|
|
|
|
2021-03-15 22:14:12 +00:00
|
|
|
let (queue_in, queue_out) = mpsc::unbounded_channel();
|
|
|
|
let queue_out = Arc::new(Mutex::new(queue_out));
|
2021-03-15 21:36:41 +00:00
|
|
|
|
|
|
|
for i in 0..n_runners {
|
|
|
|
let queue_out = queue_out.clone();
|
|
|
|
let stop_signal = stop_signal.clone();
|
|
|
|
|
2021-03-15 22:14:12 +00:00
|
|
|
worker_in
|
|
|
|
.send(tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
let (job, cancellable) = {
|
|
|
|
select! {
|
|
|
|
item = wait_job(&queue_out).fuse() => match item {
|
|
|
|
// We received a task, process it
|
|
|
|
Some(x) => x,
|
|
|
|
// We received a signal that no more tasks will ever be sent
|
|
|
|
// because the sending side was dropped. Exit now.
|
|
|
|
None => break,
|
|
|
|
},
|
2021-03-16 14:58:40 +00:00
|
|
|
_ = tokio::time::sleep(Duration::from_secs(5)).fuse() => {
|
2021-03-15 22:14:12 +00:00
|
|
|
if *stop_signal.borrow() {
|
2021-03-16 14:58:40 +00:00
|
|
|
// Nothing has been going on for 5 secs, and we are shutting
|
2021-03-15 22:14:12 +00:00
|
|
|
// down. Exit now.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Nothing is going on but we don't want to exit.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if cancellable && *stop_signal.borrow() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Err(e) = job.await {
|
|
|
|
error!("Job failed: {}", e)
|
|
|
|
}
|
2021-03-15 21:36:41 +00:00
|
|
|
}
|
2021-03-15 22:14:12 +00:00
|
|
|
info!("Background worker {} exiting", i);
|
|
|
|
}))
|
|
|
|
.unwrap();
|
2021-03-15 21:36:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 22:14:12 +00:00
|
|
|
let bgrunner = Arc::new(Self {
|
2020-04-11 16:51:11 +00:00
|
|
|
stop_signal,
|
2021-03-15 22:14:12 +00:00
|
|
|
queue_in,
|
|
|
|
worker_in,
|
|
|
|
});
|
|
|
|
(bgrunner, await_all_done)
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:36:41 +00:00
|
|
|
// Spawn a task to be run in background
|
2021-03-15 22:14:12 +00:00
|
|
|
pub fn spawn<T>(&self, job: T)
|
2020-04-11 16:51:11 +00:00
|
|
|
where
|
|
|
|
T: Future<Output = JobOutput> + Send + 'static,
|
|
|
|
{
|
2021-03-15 22:14:12 +00:00
|
|
|
let boxed: Job = Box::pin(job);
|
|
|
|
self.queue_in
|
|
|
|
.send((boxed, false))
|
|
|
|
.map_err(|_| "could not put job in queue")
|
|
|
|
.unwrap();
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spawn_cancellable<T>(&self, job: T)
|
|
|
|
where
|
|
|
|
T: Future<Output = JobOutput> + Send + 'static,
|
|
|
|
{
|
2021-03-15 22:14:12 +00:00
|
|
|
let boxed: Job = Box::pin(job);
|
|
|
|
self.queue_in
|
|
|
|
.send((boxed, true))
|
|
|
|
.map_err(|_| "could not put job in queue")
|
|
|
|
.unwrap();
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
pub fn spawn_worker<F, T>(&self, name: String, worker: F)
|
2020-04-11 16:51:11 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(watch::Receiver<bool>) -> T + Send + 'static,
|
2021-03-15 19:09:44 +00:00
|
|
|
T: Future<Output = ()> + Send + 'static,
|
2020-04-11 16:51:11 +00:00
|
|
|
{
|
|
|
|
let stop_signal = self.stop_signal.clone();
|
2021-03-15 22:14:12 +00:00
|
|
|
let task = tokio::spawn(async move {
|
|
|
|
info!("Worker started: {}", name);
|
2021-03-15 19:09:44 +00:00
|
|
|
worker(stop_signal).await;
|
|
|
|
info!("Worker exited: {}", name);
|
2021-03-15 22:14:12 +00:00
|
|
|
});
|
|
|
|
self.worker_in
|
|
|
|
.send(task)
|
|
|
|
.map_err(|_| "could not put job in queue")
|
|
|
|
.unwrap();
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 22:14:12 +00:00
|
|
|
|
|
|
|
async fn wait_job(q: &Mutex<mpsc::UnboundedReceiver<(Job, bool)>>) -> Option<(Job, bool)> {
|
|
|
|
q.lock().await.recv().await
|
|
|
|
}
|