Skip to content

Commit

Permalink
Have retry_{bootstrap,bootout} check that it hasn't been done yet
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-h committed Sep 6, 2024
1 parent f6311b1 commit 6669159
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/action/common/configure_init_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl Action for ConfigureInitService {
})?;
}

crate::action::macos::retry_bootstrap(&domain, &service_dest)
crate::action::macos::retry_bootstrap(&domain, &service, &service_dest)
.await
.map_err(Self::error)?;

Expand Down Expand Up @@ -533,6 +533,9 @@ impl Action for ConfigureInitService {
InitSystem::Launchd => {
crate::action::macos::retry_bootout(
DARWIN_LAUNCHD_DOMAIN,
self.service_name
.as_ref()
.expect("service_name should be set for launchd"),
self.service_dest
.as_ref()
.expect("service_dest should be defined for launchd"),
Expand Down
4 changes: 2 additions & 2 deletions src/action/macos/bootstrap_launchctl_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Action for BootstrapLaunchctlService {
}

if !*is_present {
crate::action::macos::retry_bootstrap(DARWIN_LAUNCHD_DOMAIN, &path)
crate::action::macos::retry_bootstrap(DARWIN_LAUNCHD_DOMAIN, &service, &path)
.await
.map_err(Self::error)?;
}
Expand All @@ -139,7 +139,7 @@ impl Action for BootstrapLaunchctlService {

#[tracing::instrument(level = "debug", skip_all)]
async fn revert(&mut self) -> Result<(), ActionError> {
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &self.path)
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &self.service, &self.path)
.await
.map_err(Self::error)?;

Expand Down
2 changes: 1 addition & 1 deletion src/action/macos/create_determinate_volume_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Action for CreateDeterminateVolumeService {
} = self;

if *needs_bootout {
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &path)
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &mount_service_label, &path)
.await
.map_err(Self::error)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/macos/create_nix_hook_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Action for CreateNixHookService {
} = self;

if *needs_bootout {
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &path)
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &service_label, &path)
.await
.map_err(Self::error)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/macos/create_volume_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Action for CreateVolumeService {
} = self;

if *needs_bootout {
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &path)
crate::action::macos::retry_bootout(DARWIN_LAUNCHD_DOMAIN, &mount_service_label, &path)
.await
.map_err(Self::error)?;
}
Expand Down
43 changes: 41 additions & 2 deletions src/action/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,35 @@ pub(crate) async fn wait_for_nix_store_dir() -> Result<(), ActionErrorKind> {
/// Wait for `launchctl bootstrap {domain} {service}` to succeed up to `retry_tokens * 500ms` amount
/// of time.
#[tracing::instrument]
pub(crate) async fn retry_bootstrap(domain: &str, service: &Path) -> Result<(), ActionErrorKind> {
pub(crate) async fn retry_bootstrap(
domain: &str,
service_name: &str,
service_path: &Path,
) -> Result<(), ActionErrorKind> {
let check_service_running = execute_command(
Command::new("launchctl")
.process_group(0)
.arg("print")
.arg([domain, service_name].join("/"))
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped()),
)
.await;

if check_service_running.is_ok() {
// NOTE(cole-h): if `launchctl print` succeeds, that means the service is already loaded
// and so our retry will fail.
return Ok(());
}

let mut retry_tokens: usize = 10;
loop {
let mut command = Command::new("launchctl");
command.process_group(0);
command.arg("bootstrap");
command.arg(domain);
command.arg(service);
command.arg(service_path);
command.stdin(std::process::Stdio::null());
command.stderr(std::process::Stdio::null());
command.stdout(std::process::Stdio::null());
Expand Down Expand Up @@ -180,8 +201,26 @@ pub(crate) async fn retry_bootstrap(domain: &str, service: &Path) -> Result<(),
#[tracing::instrument]
pub(crate) async fn retry_bootout(
domain: &str,
service_name: &str,
service_path: &Path,
) -> Result<(), ActionErrorKind> {
let check_service_running = execute_command(
Command::new("launchctl")
.process_group(0)
.arg("print")
.arg([domain, service_name].join("/"))
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped()),
)
.await;

if check_service_running.is_err() {
// NOTE(cole-h): if `launchctl print` fails, that means the service is already unloaded and
// so our retry will fail.
return Ok(());
}

let mut retry_tokens: usize = 10;
loop {
let mut command = Command::new("launchctl");
Expand Down

0 comments on commit 6669159

Please sign in to comment.