src/server/command_socket.rs: implement auto_remove flag

Remove the socket file on close.
This commit is contained in:
Dietmar Maurer 2019-04-09 11:47:23 +02:00
parent 634132fe10
commit e201753629
1 changed files with 12 additions and 5 deletions

View File

@ -13,18 +13,20 @@ use serde_json::Value;
use std::sync::Arc; use std::sync::Arc;
/// Listens on a Unix Socket to handle simple command asynchronously /// Listens on a Unix Socket to handle simple command asynchronously
pub fn create_control_socket<P, F>(path: P, f: F) -> Result<impl Future<Item=(), Error=()>, Error> pub fn create_control_socket<P, F>(path: P, auto_remove: bool, f: F) -> Result<impl Future<Item=(), Error=()>, Error>
where P: Into<PathBuf>, where P: Into<PathBuf>,
F: Send + Sync +'static + Fn(Value) -> Result<Value, Error>, F: Send + Sync +'static + Fn(Value) -> Result<Value, Error>,
{ {
let path: PathBuf = path.into(); let path: PathBuf = path.into();
let path1: PathBuf = path.clone();
if auto_remove { let _ = std::fs::remove_file(&path); }
let socket = UnixListener::bind(&path)?; let socket = UnixListener::bind(&path)?;
let f = Arc::new(f); let f = Arc::new(f);
let path = Arc::new(path); let path2 = Arc::new(path);
let path2 = path.clone(); let path3 = path2.clone();
let path3 = path.clone();
let control_future = socket.incoming() let control_future = socket.incoming()
.map_err(move |err| { eprintln!("failed to accept on control socket {:?}: {}", path2, err); }) .map_err(move |err| { eprintln!("failed to accept on control socket {:?}: {}", path2, err); })
@ -61,7 +63,12 @@ pub fn create_control_socket<P, F>(path: P, f: F) -> Result<impl Future<Item=(),
}); });
let abort_future = super::last_worker_future().map_err(|_| {}); let abort_future = super::last_worker_future().map_err(|_| {});
let task = control_future.select(abort_future).map(|_| {}).map_err(|_| {}); // let task = control_future.select(abort_future).map(|_| {}).map_err(|_| {});
let task = control_future.select(abort_future)
.then(move |_| {
if auto_remove { let _ = std::fs::remove_file(path1); }
Ok(())
});
Ok(task) Ok(task)
} }