2020-04-11 16:51:11 +00:00
|
|
|
use core::future::Future;
|
|
|
|
use std::pin::Pin;
|
2021-03-11 12:47:21 +00:00
|
|
|
use std::sync::Mutex;
|
2020-04-11 16:51:11 +00:00
|
|
|
|
|
|
|
use futures::future::join_all;
|
2020-04-22 17:04:33 +00:00
|
|
|
use futures::select;
|
|
|
|
use futures_util::future::*;
|
2020-04-11 16:51:11 +00:00
|
|
|
use std::sync::Arc;
|
2020-04-22 16:51:52 +00:00
|
|
|
use tokio::sync::{mpsc, watch, Notify};
|
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 {
|
|
|
|
n_runners: usize,
|
2020-04-16 16:41:10 +00:00
|
|
|
pub stop_signal: watch::Receiver<bool>,
|
2020-04-11 16:51:11 +00:00
|
|
|
|
|
|
|
queue_in: mpsc::UnboundedSender<(Job, bool)>,
|
|
|
|
queue_out: Mutex<mpsc::UnboundedReceiver<(Job, bool)>>,
|
2020-04-22 16:51:52 +00:00
|
|
|
job_notify: Notify,
|
2020-04-11 16:51:11 +00:00
|
|
|
|
|
|
|
workers: Mutex<Vec<tokio::task::JoinHandle<()>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BackgroundRunner {
|
|
|
|
pub fn new(n_runners: usize, stop_signal: watch::Receiver<bool>) -> Arc<Self> {
|
|
|
|
let (queue_in, queue_out) = mpsc::unbounded_channel();
|
|
|
|
Arc::new(Self {
|
|
|
|
n_runners,
|
|
|
|
stop_signal,
|
|
|
|
queue_in,
|
|
|
|
queue_out: Mutex::new(queue_out),
|
2020-04-22 16:51:52 +00:00
|
|
|
job_notify: Notify::new(),
|
2020-04-11 16:51:11 +00:00
|
|
|
workers: Mutex::new(Vec::new()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(self: Arc<Self>) {
|
2021-03-11 12:47:21 +00:00
|
|
|
let mut workers = self.workers.lock().unwrap();
|
2020-04-19 21:33:38 +00:00
|
|
|
for i in 0..self.n_runners {
|
|
|
|
workers.push(tokio::spawn(self.clone().runner(i)));
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
drop(workers);
|
|
|
|
|
|
|
|
let mut stop_signal = self.stop_signal.clone();
|
|
|
|
while let Some(exit_now) = stop_signal.recv().await {
|
|
|
|
if exit_now {
|
2021-03-11 12:47:21 +00:00
|
|
|
let mut workers = self.workers.lock().unwrap();
|
2020-04-11 16:51:11 +00:00
|
|
|
let workers_vec = workers.drain(..).collect::<Vec<_>>();
|
|
|
|
join_all(workers_vec).await;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spawn<T>(&self, job: T)
|
|
|
|
where
|
|
|
|
T: Future<Output = JobOutput> + Send + 'static,
|
|
|
|
{
|
|
|
|
let boxed: Job = Box::pin(job);
|
|
|
|
let _: Result<_, _> = self.queue_in.clone().send((boxed, false));
|
2020-04-22 16:51:52 +00:00
|
|
|
self.job_notify.notify();
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spawn_cancellable<T>(&self, job: T)
|
|
|
|
where
|
|
|
|
T: Future<Output = JobOutput> + Send + 'static,
|
|
|
|
{
|
|
|
|
let boxed: Job = Box::pin(job);
|
|
|
|
let _: Result<_, _> = self.queue_in.clone().send((boxed, true));
|
2020-04-22 16:51:52 +00:00
|
|
|
self.job_notify.notify();
|
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
|
|
|
{
|
2021-03-11 12:47:21 +00:00
|
|
|
let mut workers = self.workers.lock().unwrap();
|
2020-04-11 16:51:11 +00:00
|
|
|
let stop_signal = self.stop_signal.clone();
|
|
|
|
workers.push(tokio::spawn(async move {
|
2021-03-15 19:09:44 +00:00
|
|
|
worker(stop_signal).await;
|
|
|
|
info!("Worker exited: {}", name);
|
2020-04-11 16:51:11 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:33:38 +00:00
|
|
|
async fn runner(self: Arc<Self>, i: usize) {
|
2020-04-22 17:04:33 +00:00
|
|
|
let mut stop_signal = self.stop_signal.clone();
|
2020-04-11 16:51:11 +00:00
|
|
|
loop {
|
|
|
|
let must_exit: bool = *stop_signal.borrow();
|
2021-03-11 12:47:21 +00:00
|
|
|
if let Some(job) = self.dequeue_job(must_exit) {
|
2020-04-11 16:51:11 +00:00
|
|
|
if let Err(e) = job.await {
|
2020-04-21 12:54:55 +00:00
|
|
|
error!("Job failed: {}", e)
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if must_exit {
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Background runner {} exiting", i);
|
2020-04-11 16:51:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-22 17:04:33 +00:00
|
|
|
select! {
|
|
|
|
_ = self.job_notify.notified().fuse() => (),
|
|
|
|
_ = stop_signal.recv().fuse() => (),
|
|
|
|
}
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
fn dequeue_job(&self, must_exit: bool) -> Option<Job> {
|
|
|
|
let mut queue = self.queue_out.lock().unwrap();
|
2020-04-11 16:51:11 +00:00
|
|
|
while let Ok((job, cancellable)) = queue.try_recv() {
|
|
|
|
if cancellable && must_exit {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return Some(job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|