Skip to content

Commit

Permalink
ConfigureInitService: retry kickstart on macos (#1345)
Browse files Browse the repository at this point in the history
* ConfigureInitService: retry kickstart on macos

* fixup: macos retry_* doc comments
  • Loading branch information
cole-h authored Dec 9, 2024
1 parent baa906c commit 229832d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
13 changes: 3 additions & 10 deletions src/action/common/configure_init_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,9 @@ impl Action for ConfigureInitService {
}

if *start_daemon {
execute_command(
Command::new("launchctl")
.process_group(0)
.arg("kickstart")
.arg("-k")
.arg(format!("{domain}/{service}"))
.stdin(std::process::Stdio::null()),
)
.await
.map_err(Self::error)?;
crate::action::macos::retry_kickstart(domain, service)
.await
.map_err(Self::error)?;
}
},
InitSystem::Systemd => {
Expand Down
33 changes: 3 additions & 30 deletions src/action/macos/kickstart_launchctl_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::process::Output;
use std::time::Duration;

use tokio::process::Command;
use tracing::{span, Span};
Expand Down Expand Up @@ -95,35 +94,9 @@ impl Action for KickstartLaunchctlService {

#[tracing::instrument(level = "debug", skip_all)]
async fn execute(&mut self) -> Result<(), ActionError> {
let mut retry_tokens: usize = 10;
loop {
let mut command = Command::new("launchctl");
command.process_group(0);
command.args(["kickstart", "-k"]);
command.arg(format!("{}/{}", self.domain, self.service));
command.stdin(std::process::Stdio::null());
command.stderr(std::process::Stdio::null());
command.stdout(std::process::Stdio::null());
tracing::debug!(%retry_tokens, command = ?command.as_std(), "Waiting for kickstart to succeed");

let output = command
.output()
.await
.map_err(|e| ActionErrorKind::command(&command, e))
.map_err(Self::error)?;

if output.status.success() {
break;
} else if retry_tokens == 0 {
return Err(Self::error(ActionErrorKind::command_output(
&command, output,
)))?;
} else {
retry_tokens = retry_tokens.saturating_sub(1);
}

tokio::time::sleep(Duration::from_millis(500)).await;
}
super::retry_kickstart(&self.domain, &self.service)
.await
.map_err(Self::error)?;

Ok(())
}
Expand Down
44 changes: 42 additions & 2 deletions src/action/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub(crate) async fn wait_for_nix_store_dir() -> Result<(), ActionErrorKind> {
Ok(())
}

/// Wait for `launchctl bootstrap {domain} {service}` to succeed up to `retry_tokens * 500ms` amount
/// Wait for `launchctl bootstrap {domain} {service_path}` to succeed up to `retry_tokens * 500ms` amount
/// of time.
#[tracing::instrument]
pub(crate) async fn retry_bootstrap(
Expand Down Expand Up @@ -205,7 +205,7 @@ pub(crate) async fn retry_bootstrap(
Ok(())
}

/// Wait for `launchctl bootout {domain} {service_path}` to succeed up to `retry_tokens * 500ms` amount
/// Wait for `launchctl bootout {domain}/{service_name}` to succeed up to `retry_tokens * 500ms` amount
/// of time.
#[tracing::instrument]
pub(crate) async fn retry_bootout(domain: &str, service_name: &str) -> Result<(), ActionErrorKind> {
Expand Down Expand Up @@ -257,3 +257,43 @@ pub(crate) async fn retry_bootout(domain: &str, service_name: &str) -> Result<()

Ok(())
}

/// Wait for `launchctl kickstart {domain}/{service_name}` to succeed up to `retry_tokens * 500ms` amount
/// of time.
#[tracing::instrument]
pub(crate) async fn retry_kickstart(
domain: &str,
service_name: &str,
) -> Result<(), ActionErrorKind> {
let service_identifier = [domain, service_name].join("/");

let mut retry_tokens: usize = 10;
loop {
let mut command = Command::new("launchctl");
command.process_group(0);
command.arg("kickstart");
command.arg("-k");
command.arg(&service_identifier);
command.stdin(std::process::Stdio::null());
command.stderr(std::process::Stdio::null());
command.stdout(std::process::Stdio::null());
tracing::debug!(%retry_tokens, command = ?command.as_std(), "Waiting for kickstart to succeed");

let output = command
.output()
.await
.map_err(|e| ActionErrorKind::command(&command, e))?;

if output.status.success() {
break;
} else if retry_tokens == 0 {
return Err(ActionErrorKind::command_output(&command, output))?;
} else {
retry_tokens = retry_tokens.saturating_sub(1);
}

tokio::time::sleep(Duration::from_millis(500)).await;
}

Ok(())
}

0 comments on commit 229832d

Please sign in to comment.