feat: add wait-for-idle

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
2026-09-06 10:52:23 +00:00
parent 5ccbb44fb5
commit 835f213e49
6 changed files with 47 additions and 0 deletions
+20
View File
@@ -201,6 +201,26 @@ pub async fn list_devices(when: When) -> Vec<String> {
ret
}
pub async fn wait_for_idle() {
loop {
let mut waited = false;
let keys: Vec<String> = DEVICES.read().unwrap().keys().map(Into::into).collect();
for key in keys {
let Some(device) = DEVICES.read().unwrap().get(&key).cloned() else {
continue;
};
if device.lock.try_lock().is_ok() {
continue;
}
waited = true;
_ = device.lock.lock().await;
}
if !waited {
return;
}
}
}
fn dev_root() -> PathBuf {
std::env::var("LXDEVICED_DEV_ROOT")
.as_deref()
+6
View File
@@ -34,6 +34,7 @@ async fn handle_connection(mut connection: Connection) -> std::io::Result<()> {
Request::Trigger(set, action) => handler(trigger(set, action)),
Request::ReloadRules => handler(reload_rules()),
Request::ListDevices(when) => handler(list_devices(when)),
Request::WaitForIdle => handler(wait_for_idle()),
};
let reply = handler.await;
connection.send(reply).await?;
@@ -57,6 +58,11 @@ async fn list_devices(when: When) -> Result<Vec<String>, Error> {
Ok(crate::device::list_devices(when).await)
}
async fn wait_for_idle() -> Result<(), Error> {
crate::device::wait_for_idle().await;
Ok(())
}
fn handler<R: Serialize>(
fut: impl Future<Output = Result<R, Error>> + Send + 'static,
) -> Pin<Box<dyn Future<Output = Result<serde_json::Value, Error>> + Send>> {