Move tick to beginning of loop

The first tick completes immediately. Before this change messages would
be sent twice at first.
This commit is contained in:
Artemis Tosini 2023-12-14 21:10:32 +00:00
parent 92a611581f
commit ca44c81e73
Signed by: artemist
GPG key ID: EE5227935FE3FF18
6 changed files with 13 additions and 13 deletions

View file

@ -19,6 +19,8 @@ pub async fn battery(config: BatteryConfig, output: OutputChannel) -> eyre::Resu
let mut interval = tokio::time::interval(config.update); let mut interval = tokio::time::interval(config.update);
loop { loop {
interval.tick().await;
let charge_now = async { let charge_now = async {
let mut raw = String::new(); let mut raw = String::new();
File::open(&now_path) File::open(&now_path)
@ -56,7 +58,5 @@ pub async fn battery(config: BatteryConfig, output: OutputChannel) -> eyre::Resu
..Default::default() ..Default::default()
}) })
.await?; .await?;
interval.tick().await;
} }
} }

View file

@ -19,6 +19,8 @@ pub async fn hostname(
); );
let mut interval = tokio::time::interval(config.update); let mut interval = tokio::time::interval(config.update);
loop { loop {
interval.tick().await;
let reply = proxy.hostname(); let reply = proxy.hostname();
let hostname: String = reply.await?; let hostname: String = reply.await?;
output output
@ -28,7 +30,5 @@ pub async fn hostname(
..Default::default() ..Default::default()
}) })
.await?; .await?;
interval.tick().await;
} }
} }

View file

@ -37,6 +37,8 @@ pub async fn iwd(
let mut interval = tokio::time::interval(config.update); let mut interval = tokio::time::interval(config.update);
loop { loop {
interval.tick().await;
let managed_objects = root_proxy.get_managed_objects().await?; let managed_objects = root_proxy.get_managed_objects().await?;
let interface_path = get_interface_path(&managed_objects, &config.interface) let interface_path = get_interface_path(&managed_objects, &config.interface)
.ok_or_eyre("Couldn't find interface")?; .ok_or_eyre("Couldn't find interface")?;
@ -64,7 +66,5 @@ pub async fn iwd(
..Default::default() ..Default::default()
}) })
.await?; .await?;
interval.tick().await;
} }
} }

View file

@ -7,6 +7,8 @@ use tokio::io::AsyncReadExt;
pub async fn load(config: LoadConfig, output: OutputChannel) -> eyre::Result<()> { pub async fn load(config: LoadConfig, output: OutputChannel) -> eyre::Result<()> {
let mut interval = tokio::time::interval(config.update); let mut interval = tokio::time::interval(config.update);
loop { loop {
interval.tick().await;
let mut raw = String::new(); let mut raw = String::new();
let mut file = File::open("/proc/loadavg").await?; let mut file = File::open("/proc/loadavg").await?;
file.read_to_string(&mut raw).await?; file.read_to_string(&mut raw).await?;
@ -18,7 +20,5 @@ pub async fn load(config: LoadConfig, output: OutputChannel) -> eyre::Result<()>
..Default::default() ..Default::default()
}) })
.await?; .await?;
interval.tick().await;
} }
} }

View file

@ -38,10 +38,12 @@ async fn find_time_zone(config: LocalTimeConfig, sender: watch::Sender<State>) -
.build() .build()
.unwrap(); .unwrap();
let mut interval = tokio::time::interval(config.update_interval);
let mut prev_addr = None; let mut prev_addr = None;
let mut interval = tokio::time::interval(config.update_interval);
loop { loop {
interval.tick().await;
let curr_addr = if let Ok(curr_ipv6) = local_ip_address::local_ipv6() { let curr_addr = if let Ok(curr_ipv6) = local_ip_address::local_ipv6() {
Some(curr_ipv6) Some(curr_ipv6)
} else { } else {
@ -61,8 +63,6 @@ async fn find_time_zone(config: LocalTimeConfig, sender: watch::Sender<State>) -
prev_addr = curr_addr; prev_addr = curr_addr;
} }
} }
interval.tick().await;
} }
} }

View file

@ -30,6 +30,8 @@ fn extract_value(line: &str) -> eyre::Result<u64> {
pub async fn memory(config: MemoryConfig, output: OutputChannel) -> eyre::Result<()> { pub async fn memory(config: MemoryConfig, output: OutputChannel) -> eyre::Result<()> {
let mut interval = tokio::time::interval(config.update); let mut interval = tokio::time::interval(config.update);
loop { loop {
interval.tick().await;
let mut raw = [0u8; 256]; let mut raw = [0u8; 256];
let mut file = File::open("/proc/meminfo").await?; let mut file = File::open("/proc/meminfo").await?;
file.read_exact(&mut raw).await?; file.read_exact(&mut raw).await?;
@ -58,7 +60,5 @@ pub async fn memory(config: MemoryConfig, output: OutputChannel) -> eyre::Result
..Default::default() ..Default::default()
}) })
.await?; .await?;
interval.tick().await;
} }
} }