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
|
|
|
|
2021-03-15 21:36:41 +00:00
|
|
|
use arc_swap::ArcSwapOption;
|
2020-04-11 16:51:11 +00:00
|
|
|
use std::sync::Arc;
|
2021-03-15 21:36:41 +00:00
|
|
|
use tokio::sync::{mpsc, watch};
|
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 21:36:41 +00:00
|
|
|
queue_in: ArcSwapOption<mpsc::UnboundedSender<(Job, bool)>>,
|
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();
|
2021-03-15 21:36:41 +00:00
|
|
|
|
|
|
|
let mut workers = vec![];
|
|
|
|
let queue_out = Arc::new(tokio::sync::Mutex::new(queue_out));
|
|
|
|
|
|
|
|
for i in 0..n_runners {
|
|
|
|
let queue_out = queue_out.clone();
|
|
|
|
let stop_signal = stop_signal.clone();
|
|
|
|
|
|
|
|
workers.push(tokio::spawn(async move {
|
|
|
|
while let Some((job, cancellable)) = queue_out.lock().await.recv().await {
|
|
|
|
if cancellable && *stop_signal.borrow() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Err(e) = job.await {
|
|
|
|
error!("Job failed: {}", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info!("Worker {} exiting", i);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
Arc::new(Self {
|
|
|
|
stop_signal,
|
2021-03-15 21:36:41 +00:00
|
|
|
queue_in: ArcSwapOption::new(Some(Arc::new(queue_in))),
|
|
|
|
workers: Mutex::new(workers),
|
2020-04-11 16:51:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(self: Arc<Self>) {
|
|
|
|
let mut stop_signal = self.stop_signal.clone();
|
2021-03-15 21:36:41 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let exit_now = match stop_signal.changed().await {
|
|
|
|
Ok(()) => *stop_signal.borrow(),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Watch .changed() error: {}", e);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
2020-04-11 16:51:11 +00:00
|
|
|
if exit_now {
|
2021-03-15 21:36:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("Closing background job queue_in...");
|
|
|
|
drop(self.queue_in.swap(None));
|
|
|
|
|
|
|
|
info!("Waiting for all workers to terminate...");
|
|
|
|
while let Some(task) = self.workers.lock().unwrap().pop() {
|
|
|
|
if let Err(e) = task.await {
|
|
|
|
warn!("Error awaiting task: {}", e);
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:36:41 +00:00
|
|
|
// Spawn a task to be run in background
|
|
|
|
pub async fn spawn<T>(&self, job: T)
|
2020-04-11 16:51:11 +00:00
|
|
|
where
|
|
|
|
T: Future<Output = JobOutput> + Send + 'static,
|
|
|
|
{
|
2021-03-15 21:36:41 +00:00
|
|
|
match self.queue_in.load().as_ref() {
|
|
|
|
Some(chan) => {
|
|
|
|
let boxed: Job = Box::pin(job);
|
|
|
|
chan.send((boxed, false)).map_err(|_| "send error").unwrap();
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
warn!("Doing background job now because we are exiting...");
|
|
|
|
if let Err(e) = job.await {
|
|
|
|
warn!("Task failed: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 21:36:41 +00:00
|
|
|
match self.queue_in.load().as_ref() {
|
|
|
|
Some(chan) => {
|
|
|
|
let boxed: Job = Box::pin(job);
|
|
|
|
chan.send((boxed, false)).map_err(|_| "send error").unwrap();
|
|
|
|
}
|
|
|
|
None => (), // drop job if we are exiting
|
|
|
|
}
|
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
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|