auto restart server
This commit is contained in:
@@ -4,15 +4,6 @@ import SwiftUI
|
||||
|
||||
typealias Permission = Components.Schemas.Permission
|
||||
|
||||
struct LaunchAgent: Decodable {
|
||||
let programArguments: [String]
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case programArguments = "ProgramArguments"
|
||||
}
|
||||
}
|
||||
|
||||
let agentPlist = FileManager.default.homeDirectoryForCurrentUser.appending(path: "Library/LaunchAgents/run.fc.byre.plist")
|
||||
let serverHelper = Bundle.main.bundleURL.appending(path: "Contents/Helpers/byre").path
|
||||
private let service = "gui/\(getuid())/run.fc.byre"
|
||||
|
||||
|
||||
@@ -1,43 +1,16 @@
|
||||
import Foundation
|
||||
import Sparkle
|
||||
|
||||
private let lastVersionKey = "lastLaunchedVersion"
|
||||
|
||||
@MainActor
|
||||
final class Updater {
|
||||
static let shared = Updater()
|
||||
private lazy var controller = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil)
|
||||
|
||||
func start() {
|
||||
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
|
||||
let last = UserDefaults.standard.string(forKey: lastVersionKey)
|
||||
UserDefaults.standard.set(version, forKey: lastVersionKey)
|
||||
if last != nil && last != version {
|
||||
restartServer()
|
||||
}
|
||||
_ = controller
|
||||
}
|
||||
|
||||
func check() {
|
||||
controller.checkForUpdates(nil)
|
||||
}
|
||||
|
||||
private func restartServer() {
|
||||
guard let data = try? Data(contentsOf: agentPlist),
|
||||
let plist = try? PropertyListDecoder().decode(LaunchAgent.self, from: data),
|
||||
plist.programArguments.first == serverHelper
|
||||
else {
|
||||
return
|
||||
}
|
||||
let p = Process()
|
||||
p.executableURL = URL(filePath: "/bin/launchctl")
|
||||
p.arguments = ["kickstart", "-k", "gui/\(getuid())/run.fc.byre"]
|
||||
do {
|
||||
try p.run()
|
||||
p.waitUntilExit()
|
||||
log.info("restarted byre server: status \(p.terminationStatus)")
|
||||
} catch {
|
||||
log.error("restart byre server: \(errorText(error), privacy: .public)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user