Experiment with futures

This commit is contained in:
Adrian Heine 2020-08-16 11:08:22 +02:00
parent 907fbf95db
commit 2d3e3688fa
44 changed files with 2081 additions and 1242 deletions

View file

@ -1,5 +1,6 @@
use crate::command_runner::CommandRunner;
use crate::symbols::Symbol;
use async_trait::async_trait;
use std::borrow::Borrow;
use std::error::Error;
use std::marker::PhantomData;
@ -21,15 +22,20 @@ impl<_C, C, S> ReloadService<_C, C, S> {
}
}
#[async_trait(?Send)]
impl<S: AsRef<str>, _C: CommandRunner, C: Borrow<_C>> Symbol for ReloadService<_C, C, S> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
Ok(true)
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
self.command_runner.borrow().run_successfully(
"systemctl",
args!["reload-or-restart", self.service.as_ref()],
)
async fn execute(&self) -> Result<(), Box<dyn Error>> {
self
.command_runner
.borrow()
.run_successfully(
"systemctl",
args!["reload-or-restart", self.service.as_ref()],
)
.await
}
}

View file

@ -1,9 +1,10 @@
use crate::async_utils::sleep;
use crate::command_runner::{CommandRunner, SetuidCommandRunner};
use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::ffi::OsStr;
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
#[derive(Debug)]
@ -29,10 +30,10 @@ impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> UserService<'static, S, U,
}
impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> UserService<'_, S, U, R> {
fn systemctl_wait_for_dbus(&self, args: &[&OsStr]) -> Result<String, Box<dyn Error>> {
async fn systemctl_wait_for_dbus(&self, args: &[&OsStr]) -> Result<String, Box<dyn Error>> {
let mut tries = 5;
loop {
let result = self.command_runner.run_with_args("systemctl", args)?;
let result = self.command_runner.run_with_args("systemctl", args).await?;
if result.status.success() {
return Ok(String::from_utf8(result.stdout)?.trim_end().to_string());
} else {
@ -46,28 +47,35 @@ impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> UserService<'_, S, U, R> {
if tries == 0 {
return Err("Gave up waiting for dbus to appear".to_string().into());
}
sleep(Duration::from_millis(500));
sleep(Duration::from_millis(500)).await;
}
}
fn check_if_service(&self) -> Result<bool, Box<dyn Error>> {
async fn check_if_service(&self) -> Result<bool, Box<dyn Error>> {
loop {
let active_state = self.systemctl_wait_for_dbus(args![
"--user",
"show",
"--property",
"ActiveState",
self.service_name,
])?;
let active_state = self
.systemctl_wait_for_dbus(args![
"--user",
"show",
"--property",
"ActiveState",
self.service_name,
])
.await?;
match active_state.as_ref() {
"ActiveState=activating" => sleep(Duration::from_millis(500)),
"ActiveState=activating" => sleep(Duration::from_millis(500)).await,
"ActiveState=active" => return Ok(true),
"ActiveState=failed" => {
return Err(
String::from_utf8(self.command_runner.get_output(
"journalctl",
args!["--user", format!("--user-unit={}", self.service_name)],
)?)?
String::from_utf8(
self
.command_runner
.get_output(
"journalctl",
args!["--user", format!("--user-unit={}", self.service_name)],
)
.await?,
)?
.into(),
)
}
@ -77,24 +85,29 @@ impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> UserService<'_, S, U, R> {
}
}
#[async_trait(?Send)]
impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> Symbol for UserService<'_, S, U, R> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
self.check_if_service()
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
self.check_if_service().await
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
self.systemctl_wait_for_dbus(args!["--user", "enable", self.service_name])?;
self.systemctl_wait_for_dbus(args!["--user", "restart", self.service_name])?;
async fn execute(&self) -> Result<(), Box<dyn Error>> {
self
.systemctl_wait_for_dbus(args!["--user", "enable", self.service_name])
.await?;
self
.systemctl_wait_for_dbus(args!["--user", "restart", self.service_name])
.await?;
loop {
if !(self.check_if_service()?) {
if !(self.check_if_service().await?) {
return Err("Generic error".into());
}
if self.socket_path.as_ref().exists() {
return Ok(());
}
sleep(Duration::from_millis(500));
sleep(Duration::from_millis(500)).await;
}
}
}

View file

@ -1,5 +1,6 @@
use crate::command_runner::CommandRunner;
use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::path::Path;
@ -18,16 +19,18 @@ impl<'a, U, C> UserSession<'a, U, C> {
}
}
#[async_trait(?Send)]
impl<U: AsRef<str>, C: CommandRunner> Symbol for UserSession<'_, U, C> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let path = Path::new("/var/lib/systemd/linger").join(self.user_name.as_ref());
Ok(path.exists())
// Could also do `loginctl show-user ${self.user_name} | grep -F 'Linger=yes`
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
async fn execute(&self) -> Result<(), Box<dyn Error>> {
self
.command_runner
.run_successfully("loginctl", args!["enable-linger", self.user_name.as_ref()])
.await
}
}