This commit is contained in:
Adrian Heine 2017-05-19 12:59:27 +02:00
parent 8882712925
commit 9268254369
4 changed files with 122 additions and 9 deletions

View file

@ -88,15 +88,32 @@ WantedBy=default.target
}
impl<'a, C, R> NodeJsSystemdUserService<'a, C, R> where C: Deref<Target=str>, R: CommandRunner {
fn systemctl_wait_for_dbus(&self, args: &[&str]) -> Result<String, Box<Error>> {
let mut tries = 5;
loop {
let result = try!(self.command_runner.run_with_args("systemctl", args));
if !result.status.success() {
let raw_stderr = try!(String::from_utf8(result.stderr));
let stderr = raw_stderr.trim_right();
if stderr != "Failed to connect to bus: No such file or directory" {
return Err(stderr.into());
}
} else {
return Ok(try!(String::from_utf8(result.stdout)).trim_right().to_string());
}
tries -= 1;
if tries == 0 {
return Err("Gave up waiting for dbus to appear".to_string().into());
}
sleep(Duration::from_millis(500));
}
}
fn check_if_service(&self) -> Result<bool, Box<Error>> {
loop {
// Check if service is registered
let active_state = try!(self.command_runner.run_with_args("systemctl", &["--user", "show", "--property", "ActiveState", self.service_name]));
if !active_state.status.success() {
return Err(String::from_utf8(active_state.stderr).unwrap().trim_right().into());
}
// Check if service is running
match String::from_utf8(active_state.stdout).unwrap().trim_right() {
let active_state = try!(self.systemctl_wait_for_dbus(&["--user", "show", "--property", "ActiveState", self.service_name]));
match active_state.as_ref() {
"ActiveState=activating" => sleep(Duration::from_millis(500)),
"ActiveState=active" => return Ok(true),
"ActiveState=failed" => return Err(Box::new(NodeJsSystemdUserServiceError::ActivationFailed(self.command_runner.run_with_args("journalctl", &["--user", &format!("--user-unit={}", self.service_name)])) as NodeJsSystemdUserServiceError<io::Error>)),
@ -141,8 +158,8 @@ impl<'a, C, R> Symbol for NodeJsSystemdUserService<'a, C, R> where C: Deref<Targ
fn execute(&self) -> Result<(), Box<Error>> {
try!(self.file.execute());
try!(self.command_runner.run_with_args("systemctl", &["--user", "enable", self.service_name]));
try!(self.command_runner.run_with_args("systemctl", &["--user", "start", self.service_name]));
try!(self.systemctl_wait_for_dbus(&["--user", "enable", self.service_name]));
try!(self.systemctl_wait_for_dbus(&["--user", "start", self.service_name]));
if !(try!(self.check_if_service())) {
return Err(Box::new(NodeJsSystemdUserServiceError::GenericError as NodeJsSystemdUserServiceError<io::Error>));