1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

🏗 WIP: Service toggling

This commit is contained in:
2023-01-06 18:33:55 +01:00
parent 6d52992c9d
commit 7b07520440
4 changed files with 29 additions and 37 deletions

View File

@ -65,23 +65,6 @@ class Actions {
}
}
// MARK: - Third Party Services
public static func stopService(name: String) async {
await brew(
"services stop \(name)",
sudo: ServicesManager.shared[name]?.formula.elevated ?? false
)
await ServicesManager.shared.reloadServicesStatus()
}
public static func startService(name: String) async {
await brew(
"services start \(name)",
sudo: ServicesManager.shared[name]?.formula.elevated ?? false
)
await ServicesManager.shared.reloadServicesStatus()
}
// MARK: - Finding Config Files
public static func openGenericPhpConfigFolder() {

View File

@ -63,12 +63,11 @@ class HomebrewService: Decodable, Equatable, Hashable {
hasher.combine(service_name)
hasher.combine(pid)
hasher.combine(status)
hasher.combine(running)
hasher.combine(user)
}
static func == (lhs: HomebrewService, rhs: HomebrewService) -> Bool {
return lhs.name == rhs.name
&& lhs.service_name == rhs.service_name
&& lhs.pid == rhs.pid
&& lhs.status == rhs.status
return lhs.hashValue == rhs.hashValue
}
}

View File

@ -14,7 +14,6 @@ import Foundation
public enum ServiceStatus: String {
case active
case inactive
case loading
case missing
}
@ -26,17 +25,11 @@ public class ServiceWrapper: ObservableObject, Identifiable, Hashable {
var formula: HomebrewFormula
var service: HomebrewService?
var isBusy: Bool = false
public var name: String {
return formula.name
}
public var status: ServiceStatus {
if isBusy {
return .loading
}
guard let service = self.service else {
return .missing
}
@ -46,16 +39,20 @@ public class ServiceWrapper: ObservableObject, Identifiable, Hashable {
init(formula: HomebrewFormula) {
self.formula = formula
self.isBusy = true
}
public static func == (lhs: ServiceWrapper, rhs: ServiceWrapper) -> Bool {
return lhs.service == rhs.service
&& lhs.formula == rhs.formula
return lhs.hashValue == rhs.hashValue
}
public func hash(into hasher: inout Hasher) {
hasher.combine(formula)
hasher.combine(service)
}
public func broadcastChanged() {
Task { @MainActor in
self.objectWillChange.send()
}
}
}

View File

@ -23,6 +23,7 @@ class ValetServicesManager: ServicesManager {
*/
override func reloadServicesStatus() async {
await withTaskGroup(of: [HomebrewService].self, body: { group in
// First, retrieve the status of the formulae that run as root
group.addTask {
let rootServiceNames = self.formulae
.filter { $0.elevated }
@ -37,6 +38,7 @@ class ValetServicesManager: ServicesManager {
.filter({ return rootServiceNames.contains($0.name) })
}
// At the same time, retrieve the status of the formulae that run as user
group.addTask {
let userServiceNames = self.formulae
.filter { !$0.elevated }
@ -59,17 +61,28 @@ class ValetServicesManager: ServicesManager {
}
}
// Ensure that every wrapper is considered no longer busy
for wrapper in serviceWrappers {
wrapper.isBusy = false
}
// Broadcast that all services have been updated
self.broadcastServicesUpdated()
})
}
override func toggleService(named: String) async {
// TODO
guard let wrapper = self[named] else {
return Log.err("The wrapper for '\(named)' is missing.")
}
if wrapper.service == nil {
return Log.err("The Homebrew service for \(named) is missing.")
}
// Prepare the appropriate command to stop or start a service
let action = wrapper.service!.running ? "stop" : "start"
let command = "services \(action) \(wrapper.formula.name)"
// Run the command
await brew(command, sudo: wrapper.formula.elevated)
// Reload the services status to confirm this worked
await ServicesManager.shared.reloadServicesStatus()
}
}