From 1a00f73191abea79179301752e70b4ca1c9b0253 Mon Sep 17 00:00:00 2001
From: meeh <meeh@mail.i2p>
Date: Fri, 12 Oct 2018 20:52:24 +0000
Subject: [PATCH] OSX Launcher: Extended start/stop/load/unload to be able to
 set terminationHandler for more reliable execution.

---
 .../Utils/LaunchAgent+Status.swift            |  8 +++---
 .../Utils/LaunchAgentManager.swift            | 28 +++++++++++++------
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift b/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift
index fa7286d68a..8e1a19245d 100644
--- a/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift
+++ b/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift
@@ -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
diff --git a/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift b/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift
index 2eb50fe369..b9c26c78f8 100644
--- a/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift
+++ b/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift
@@ -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`
-- 
GitLab