Fix "lqosd is running" error when it clearly isn't.

* Fix the Python code to actually call `is_lqosd_alive()` instead of just
checking that it exists (`is_lqosd_alive`).
* Fix the os.exit command syntax.
* Cleanup the blocking Tokio/lqosd request handler to pass better messages.
* Catch the "file not found" and replace it with a nicer message.
This commit is contained in:
Herbert Wolverson 2023-01-23 21:59:02 +00:00
parent 37355189b9
commit 11428f65e7
3 changed files with 40 additions and 23 deletions

View File

@ -1250,11 +1250,11 @@ def refreshShapersUpdateOnly():
print("refreshShapersUpdateOnly completed on " + datetime.now().strftime("%d/%m/%Y %H:%M:%S")) print("refreshShapersUpdateOnly completed on " + datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
if __name__ == '__main__': if __name__ == '__main__':
if is_lqosd_alive: if is_lqosd_alive():
print("lqosd is running") print("lqosd is running")
else: else:
print("ERROR: lqosd is not running. Aborting") print("ERROR: lqosd is not running. Aborting")
os.exit() os._exit(-1)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(

View File

@ -1,26 +1,44 @@
use tokio::{net::UnixStream, io::{AsyncWriteExt, AsyncReadExt}};
use crate::{BUS_SOCKET_PATH, BusSession, BusRequest, encode_request, decode_response, BusResponse};
use anyhow::Result;
use super::PREALLOCATE_CLIENT_BUFFER_BYTES; use super::PREALLOCATE_CLIENT_BUFFER_BYTES;
use crate::{
decode_response, encode_request, BusRequest, BusResponse, BusSession,
BUS_SOCKET_PATH,
};
use anyhow::{Error, Result};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::UnixStream,
};
/// Convenient wrapper for accessing the bus /// Convenient wrapper for accessing the bus
/// ///
/// ## Arguments /// ## Arguments
/// ///
/// * `requests` a vector of `BusRequest` requests to make. /// * `requests` a vector of `BusRequest` requests to make.
/// ///
/// **Returns** Either an error, or a vector of `BusResponse` replies /// **Returns** Either an error, or a vector of `BusResponse` replies
pub async fn bus_request(requests: Vec<BusRequest>) -> Result<Vec<BusResponse>> { pub async fn bus_request(
let mut stream = UnixStream::connect(BUS_SOCKET_PATH).await.unwrap(); requests: Vec<BusRequest>,
let test = BusSession { ) -> Result<Vec<BusResponse>> {
persist: false, let stream = UnixStream::connect(BUS_SOCKET_PATH).await;
requests, match &stream {
}; Err(e) => match e.kind() {
let msg = encode_request(&test)?; std::io::ErrorKind::NotFound => {
stream.write(&msg).await?; return Err(Error::msg(format!(
let mut buf = Vec::with_capacity(PREALLOCATE_CLIENT_BUFFER_BYTES); "{} not found. Check permissions and that lqosd is running.",
let _ = stream.read_to_end(&mut buf).await.unwrap(); BUS_SOCKET_PATH
let reply = decode_response(&buf)?; )))
}
_ => {}
},
_ => {}
}
let mut stream = stream.unwrap();
let test = BusSession { persist: false, requests };
let msg = encode_request(&test)?;
stream.write(&msg).await?;
let mut buf = Vec::with_capacity(PREALLOCATE_CLIENT_BUFFER_BYTES);
let _ = stream.read_to_end(&mut buf).await?;
let reply = decode_response(&buf)?;
Ok(reply.responses) Ok(reply.responses)
} }

View File

@ -7,8 +7,7 @@ pub fn run_query(requests: Vec<BusRequest>) -> Result<Vec<BusResponse>> {
let mut replies = Vec::with_capacity(8); let mut replies = Vec::with_capacity(8);
tokio::runtime::Builder::new_current_thread() tokio::runtime::Builder::new_current_thread()
.enable_all() .enable_all()
.build() .build()?
.unwrap()
.block_on(async { .block_on(async {
replies.extend_from_slice(&bus_request(requests).await?); replies.extend_from_slice(&bus_request(requests).await?);
Ok(replies) Ok(replies)