Skip to content

Commit

Permalink
fix: service start/stop on MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
medcl committed Feb 19, 2024
1 parent 26671e8 commit 571b6a0
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/launchd.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::{
utils, ServiceInstallCtx, ServiceLevel, ServiceManager, ServiceStartCtx, ServiceStopCtx,
ServiceUninstallCtx,
utils, ServiceInstallCtx, ServiceLevel, ServiceManager, ServiceStartCtx,
ServiceStopCtx, ServiceUninstallCtx,
};
use plist::Dictionary;
use plist::Value;
use plist::{Dictionary, Value};
use std::{
ffi::OsStr,
io,
Expand Down Expand Up @@ -134,11 +133,29 @@ impl ServiceManager for LaunchdServiceManager {
}

fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
let dir_path = if self.user {
user_agent_dir_path()?
} else {
global_daemon_dir_path()
};
let qualified_name = ctx.label.to_qualified_name();
let plist_path = dir_path.join(format!("{}.plist", qualified_name));

launchctl("load", plist_path.to_string_lossy().as_ref())?;
launchctl("start", &ctx.label.to_qualified_name())
}

fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
launchctl("stop", &ctx.label.to_qualified_name())
let dir_path = if self.user {
user_agent_dir_path()?
} else {
global_daemon_dir_path()
};
let qualified_name = ctx.label.to_qualified_name();
let plist_path = dir_path.join(format!("{}.plist", qualified_name));

launchctl("stop", &ctx.label.to_qualified_name());
launchctl("unload", plist_path.to_string_lossy().as_ref())
}

fn level(&self) -> ServiceLevel {
Expand Down Expand Up @@ -192,7 +209,12 @@ fn global_daemon_dir_path() -> PathBuf {

fn user_agent_dir_path() -> io::Result<PathBuf> {
Ok(dirs::home_dir()
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Unable to locate home directory"))?
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"Unable to locate home directory",
)
})?
.join("Library")
.join("LaunchAgents"))
}
Expand Down

0 comments on commit 571b6a0

Please sign in to comment.