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"))
if __name__ == '__main__':
if is_lqosd_alive:
if is_lqosd_alive():
print("lqosd is running")
else:
print("ERROR: lqosd is not running. Aborting")
os.exit()
os._exit(-1)
parser = argparse.ArgumentParser()
parser.add_argument(

View File

@ -1,7 +1,13 @@
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 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
///
@ -10,16 +16,28 @@ use super::PREALLOCATE_CLIENT_BUFFER_BYTES;
/// * `requests` a vector of `BusRequest` requests to make.
///
/// **Returns** Either an error, or a vector of `BusResponse` replies
pub async fn bus_request(requests: Vec<BusRequest>) -> Result<Vec<BusResponse>> {
let mut stream = UnixStream::connect(BUS_SOCKET_PATH).await.unwrap();
let test = BusSession {
persist: false,
requests,
};
pub async fn bus_request(
requests: Vec<BusRequest>,
) -> Result<Vec<BusResponse>> {
let stream = UnixStream::connect(BUS_SOCKET_PATH).await;
match &stream {
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => {
return Err(Error::msg(format!(
"{} not found. Check permissions and that lqosd is running.",
BUS_SOCKET_PATH
)))
}
_ => {}
},
_ => {}
}
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.unwrap();
let _ = stream.read_to_end(&mut buf).await?;
let reply = decode_response(&buf)?;
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);
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.build()?
.block_on(async {
replies.extend_from_slice(&bus_request(requests).await?);
Ok(replies)