Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigureInitService: retry kickstart on macos #1345

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
cole-h marked this conversation as resolved.
Show resolved Hide resolved
} 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(())
}
Loading