Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Extract start daemon function
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumerose committed Apr 27, 2021
1 parent f8454e4 commit 3dfa477
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions CodeReady Containers/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
})

DispatchQueue.global(qos: .background).async {
let task = Process()
let stdin = Pipe()
#if DEBUG
task.launchPath = "/usr/local/bin/crc"
#else
task.launchPath = NSString.path(withComponents: [Bundle.main.bundlePath, "Contents", "Resources", "crc"])
#endif
task.arguments = ["daemon", "--watchdog"]
task.standardInput = stdin
do {
try task.run()
} catch let error {
fatal(message: "Cannot start the daemon",
informativeMsg: "Check the logs and restart the application.\nError: \(error.localizedDescription)")
return
}
task.waitUntilExit()
if task.terminationStatus == 2 {
fatal(message: "Setup incomplete",
informativeMsg: "Open a terminal, run 'crc setup', and start again this application.")
} else {
fatal(message: "Daemon crashed",
informativeMsg: "Check the logs and restart the application")
}
self.startDaemon()
}

DispatchQueue.global(qos: .background).async {
Expand All @@ -113,6 +90,33 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
NotificationCenter.default.addObserver(self, selector: #selector(updateViewWithClusterStatus(_:)), name: statusNotification, object: nil)
}

func startDaemon() {
let task = Process()
let stdin = Pipe()
#if DEBUG
task.launchPath = "/usr/local/bin/crc"
#else
task.launchPath = NSString.path(withComponents: [Bundle.main.bundlePath, "Contents", "Resources", "crc"])
#endif
task.arguments = ["daemon", "--watchdog"]
task.standardInput = stdin
do {
try task.run()
} catch let error {
fatal(message: "Cannot start the daemon",
informativeMsg: "Check the logs and restart the application.\nError: \(error.localizedDescription)")
return
}
task.waitUntilExit()
if task.terminationStatus == 2 {
fatal(message: "Setup incomplete",
informativeMsg: "Open a terminal, run 'crc setup', and start again this application.")
} else {
fatal(message: "Daemon crashed",
informativeMsg: "Check the logs and restart the application")
}
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
// Maybe kill the daemon too
Expand Down

0 comments on commit 3dfa477

Please sign in to comment.