OSX Launcher: Extended start/stop/load/unload to be able to set terminationHandler for more reliable execution.

This commit is contained in:
meeh
2018-10-12 20:52:24 +00:00
parent d8cfe21e92
commit 1a00f73191
2 changed files with 24 additions and 12 deletions

View File

@@ -34,15 +34,15 @@ extension LaunchAgent {
/// Run `launchctl start` on the agent
///
/// Check the status of the job with `.status()`
public func start() {
LaunchAgentManager.shared.start(self)
public func start(_ callback: ((Process) -> Void)? = nil ) {
LaunchAgentManager.shared.start(self, callback)
}
/// Run `launchctl stop` on the agent
///
/// Check the status of the job with `.status()`
public func stop() {
LaunchAgentManager.shared.stop(self)
public func stop(_ callback: ((Process) -> Void)? = nil ) {
LaunchAgentManager.shared.stop(self, callback)
}
/// Run `launchctl load` on the agent

View File

@@ -88,41 +88,53 @@ extension LaunchAgentManager {
/// Run `launchctl start` on the agent
///
/// Check the status of the job with `.status(_: LaunchAgent)`
public func start(_ agent: LaunchAgent) {
public func start(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) {
let arguments = ["start", agent.label]
Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
if ((termHandler) != nil) {
proc.terminationHandler = termHandler
}
}
/// Run `launchctl stop` on the agent
///
/// Check the status of the job with `.status(_: LaunchAgent)`
public func stop(_ agent: LaunchAgent) {
public func stop(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) {
let arguments = ["stop", agent.label]
Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
if ((termHandler) != nil) {
proc.terminationHandler = termHandler
}
}
/// Run `launchctl load` on the agent
///
/// Check the status of the job with `.status(_: LaunchAgent)`
public func load(_ agent: LaunchAgent) throws {
public func load(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) throws {
guard let agentURL = agent.url else {
throw LaunchAgentManagerError.urlNotSet(label: agent.label)
}
let arguments = ["load", agentURL.path]
Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
if ((termHandler) != nil) {
proc.terminationHandler = termHandler
}
}
/// Run `launchctl unload` on the agent
///
/// Check the status of the job with `.status(_: LaunchAgent)`
public func unload(_ agent: LaunchAgent) throws {
public func unload(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) throws {
guard let agentURL = agent.url else {
throw LaunchAgentManagerError.urlNotSet(label: agent.label)
}
let arguments = ["unload", agentURL.path]
Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments)
if ((termHandler) != nil) {
proc.terminationHandler = termHandler
}
}
/// Retreives the status of the LaunchAgent from `launchctl`