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

Parallelize unlinking PHP versions

This commit is contained in:
2021-04-14 18:46:33 +02:00
parent 0c0e7fc87d
commit e83d507e79
3 changed files with 65 additions and 24 deletions

View File

@ -218,6 +218,12 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
} }
} }
@objc func stopAllServices() {
waitAndExecute {
Actions.stopAllServices()
}
}
@objc func restartNginx() { @objc func restartNginx() {
waitAndExecute { waitAndExecute {
Actions.restartNginx() Actions.restartNginx()
@ -289,26 +295,29 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
// Update the menu // Update the menu
update() update()
let completion = {
// Mark as no longer busy
App.shared.busy = false
// Perform UI updates on main thread
DispatchQueue.main.async { [self] in
updatePhpVersionInStatusBar()
update()
// Send a notification that the switch has been completed
LocalNotification.send(
title: String(format: "notification.version_changed_title".localized, sender.version),
subtitle: String(format: "notification.version_changed_desc".localized, sender.version)
)
}
}
// Switch the PHP version // Switch the PHP version
Actions.switchToPhpVersion( Actions.switchToPhpVersion(
version: sender.version, version: sender.version,
availableVersions: App.shared.availablePhpVersions availableVersions: App.shared.availablePhpVersions,
completed: completion
) )
// Mark as no longer busy
App.shared.busy = false
// Perform UI updates on main thread
DispatchQueue.main.async { [self] in
updatePhpVersionInStatusBar()
update()
// Send a notification that the switch has been completed
LocalNotification.send(
title: String(format: "notification.version_changed_title".localized, sender.version),
subtitle: String(format: "notification.version_changed_desc".localized, sender.version)
)
}
} }
} }

View File

@ -78,6 +78,13 @@ class Actions {
brew("services restart dnsmasq", sudo: true) brew("services restart dnsmasq", sudo: true)
} }
public static func stopAllServices()
{
brew("services stop \(App.phpInstall!.formula)", sudo: true)
brew("services stop nginx", sudo: true)
brew("services stop dnsmasq", sudo: true)
}
/** /**
Switching to a new PHP version involves: Switching to a new PHP version involves:
- unlinking the current version - unlinking the current version
@ -87,18 +94,42 @@ class Actions {
Please note that depending on which version is installed, Please note that depending on which version is installed,
the version that is switched to may or may not be identical to `php` (without @version). the version that is switched to may or may not be identical to `php` (without @version).
*/ */
public static func switchToPhpVersion(version: String, availableVersions: [String]) public static func switchToPhpVersion(
{ version: String,
availableVersions: [String],
completed: @escaping () -> Void
) {
print("Switching to \(version)")
let group = DispatchGroup()
group.enter()
var versionsDisabled: [String: Bool] = [:]
availableVersions.forEach { (available) in availableVersions.forEach { (available) in
let formula = (available == App.shared.brewPhpVersion) ? "php" : "php@\(available)" versionsDisabled[available] = false
brew("unlink \(formula)")
brew("services stop \(formula)", sudo: true)
} }
let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)" availableVersions.forEach { (available) in
DispatchQueue.global(qos: .userInitiated).async {
let formula = (available == App.shared.brewPhpVersion)
? "php" : "php@\(available)"
brew("unlink \(formula)")
brew("services stop \(formula)", sudo: true)
versionsDisabled[available] = true
if !versionsDisabled.values.contains(false) {
group.leave()
}
}
}
brew("link \(formula) --overwrite --force") group.notify(queue: .global(qos: .userInitiated)) {
brew("services start \(formula)", sudo: true) let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)"
brew("link \(formula) --overwrite --force")
brew("services start \(formula)", sudo: true)
completed()
}
} }
// MARK: - Finding Config Files // MARK: - Finding Config Files

View File

@ -24,6 +24,7 @@
"mi_restart_dnsmasq" = "Restart service: dnsmasq"; "mi_restart_dnsmasq" = "Restart service: dnsmasq";
"mi_restart_specific" = "Restart specific service"; "mi_restart_specific" = "Restart specific service";
"mi_restart_all_services" = "Restart all services"; "mi_restart_all_services" = "Restart all services";
"mi_stop_all_services" = "Stop all services";
"mi_force_load_latest" = "Force load latest PHP version"; "mi_force_load_latest" = "Force load latest PHP version";
"mi_php_refresh" = "Refresh information"; "mi_php_refresh" = "Refresh information";