Add load, remove unwraps, fix high CPU
This commit is contained in:
parent
49c82ade0b
commit
224739a7ec
21
src/main.rs
21
src/main.rs
|
@ -5,7 +5,6 @@ pub mod tiles;
|
|||
use dbus_tokio::connection::new_session_sync;
|
||||
use std::sync::Arc;
|
||||
use tile::Tile;
|
||||
use tokio;
|
||||
use tokio::sync::mpsc::channel;
|
||||
use uuid::Uuid;
|
||||
|
||||
|
@ -22,17 +21,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
|
||||
let (sender, receiver) = channel(1024);
|
||||
|
||||
let instance = Uuid::new_v4().to_string().into_boxed_str();
|
||||
let tiles: Vec<Arc<dyn Tile>> = vec![Arc::new(tiles::Time::new(0, sender, instance))];
|
||||
let tiles: Vec<Arc<dyn Tile>> = vec![
|
||||
Arc::new(tiles::Load::new(
|
||||
0,
|
||||
sender.clone(),
|
||||
Uuid::new_v4().to_string().into_boxed_str(),
|
||||
)),
|
||||
Arc::new(tiles::Time::new(
|
||||
1,
|
||||
sender,
|
||||
Uuid::new_v4().to_string().into_boxed_str(),
|
||||
)),
|
||||
];
|
||||
|
||||
for tile in &tiles {
|
||||
tile.clone().spawn();
|
||||
}
|
||||
|
||||
let num_tiles = tiles.len();
|
||||
tokio::spawn(async move {
|
||||
output::launch(num_tiles, receiver).await.unwrap();
|
||||
});
|
||||
output::launch(num_tiles, receiver).await?;
|
||||
|
||||
loop {}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -71,5 +71,5 @@ pub struct TileData {
|
|||
}
|
||||
|
||||
pub trait Tile: Send {
|
||||
fn spawn(self: Arc<Self>) -> JoinHandle<()>;
|
||||
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>>;
|
||||
}
|
||||
|
|
60
src/tiles/load.rs
Normal file
60
src/tiles/load.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
use crate::tile::{Block, Tile, TileData};
|
||||
use async_std::sync::RwLock;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc::{error::SendError, Sender};
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::interval;
|
||||
|
||||
pub struct Load {
|
||||
sender_id: usize,
|
||||
sender: RwLock<Sender<TileData>>,
|
||||
instance: Box<str>,
|
||||
}
|
||||
|
||||
impl Load {
|
||||
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Load {
|
||||
Load {
|
||||
sender_id,
|
||||
sender: RwLock::new(sender),
|
||||
instance,
|
||||
}
|
||||
}
|
||||
|
||||
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> {
|
||||
let mut sender = self.sender.write().await;
|
||||
sender.send(data).await
|
||||
}
|
||||
|
||||
async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut timer = interval(Duration::from_secs(5));
|
||||
let mut raw = String::new();
|
||||
loop {
|
||||
timer.tick().await;
|
||||
File::open("/proc/loadavg")?.read_to_string(&mut raw)?;
|
||||
let (load, _rest) = raw.split_at(raw.find(' ').unwrap_or(0));
|
||||
let block = Block {
|
||||
full_text: load.into(),
|
||||
instance: self.instance.clone(),
|
||||
name: "load".into(),
|
||||
..Default::default()
|
||||
};
|
||||
let data = TileData {
|
||||
block,
|
||||
sender_id: self.sender_id,
|
||||
};
|
||||
self.send(data).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tile for Load {
|
||||
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
|
||||
tokio::spawn(async move {
|
||||
let instance = self;
|
||||
instance.run().await
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,2 +1,4 @@
|
|||
pub mod load;
|
||||
pub mod time;
|
||||
pub use load::Load;
|
||||
pub use time::Time;
|
||||
|
|
|
@ -47,10 +47,10 @@ impl Time {
|
|||
self.send(data).await
|
||||
}
|
||||
|
||||
async fn run(&self) {
|
||||
async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut time = Local::now();
|
||||
loop {
|
||||
self.send_time(time).await.unwrap();
|
||||
self.send_time(time).await?;
|
||||
time = Local::now();
|
||||
let millis_part = time.naive_local().timestamp_subsec_millis() as u64;
|
||||
let delay_ms = 1000u64 - millis_part % 1000; // Don't crash if we hit a leap second
|
||||
|
@ -60,12 +60,10 @@ impl Time {
|
|||
}
|
||||
|
||||
impl Tile for Time {
|
||||
fn spawn(self: Arc<Self>) -> JoinHandle<()> {
|
||||
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
|
||||
tokio::spawn(async move {
|
||||
let instance = self;
|
||||
loop {
|
||||
instance.run().await
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue