mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-06 03:20:09 +02:00
♻️ Clean up startup timers
This commit is contained in:
@ -7,12 +7,20 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AppKit
|
||||
import NVAlert
|
||||
|
||||
extension Startup {
|
||||
@MainActor static var startupTimer: Timer?
|
||||
@MainActor static var launchTime: Date?
|
||||
|
||||
@MainActor func startTimeoutTimer() {
|
||||
/** Returns a human-readable version to indicate how many seconds elapsed since boot. */
|
||||
@MainActor static var humanReadableSinceBootTime: String {
|
||||
return String(format: "%.2f", Date().timeIntervalSince(Self.launchTime!))
|
||||
}
|
||||
|
||||
/** Starts the timeout timer that keeps track of how long the app takes to boot. */
|
||||
@MainActor func startStartupTimer() {
|
||||
Self.launchTime = Date()
|
||||
Self.startupTimer = Timer.scheduledTimer(
|
||||
timeInterval: Constants.SlowBootThresholdInterval, target: self,
|
||||
@ -20,13 +28,47 @@ extension Startup {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Invalidates and stops the startup timer.
|
||||
This is only called if the slow boot threshold is not exceeded.
|
||||
*/
|
||||
@MainActor static func invalidateTimeoutTimer() {
|
||||
let elapsedTime = Date().timeIntervalSince(Self.launchTime!)
|
||||
let printableTime = String(format: "%.2f", elapsedTime)
|
||||
if Self.startupTimer == nil {
|
||||
return
|
||||
}
|
||||
|
||||
Log.info("PHP Monitor launched quickly enough!")
|
||||
Log.info("PHP Monitor boot time: \(printableTime) sec")
|
||||
Log.info("PHP Monitor was quick; elapsed time: \(Self.humanReadableSinceBootTime) sec.")
|
||||
Self.startupTimer?.invalidate()
|
||||
Self.startupTimer = nil
|
||||
}
|
||||
|
||||
/**
|
||||
Displays an alert for when the application startup process takes too long.
|
||||
*/
|
||||
@MainActor @objc func startupTimeout() {
|
||||
Log.info("PHP Monitor was slow; elapsed time: \(Self.humanReadableSinceBootTime) sec.")
|
||||
|
||||
// Invalidate the timer
|
||||
Self.startupTimer?.invalidate()
|
||||
Self.startupTimer = nil
|
||||
|
||||
// Present an alert that lets the user know about the slow start
|
||||
NVAlert()
|
||||
.withInformation(
|
||||
title: "startup.timeout.title".localized,
|
||||
subtitle: "startup.timeout.subtitle".localized,
|
||||
description: "startup.timeout.description".localized
|
||||
)
|
||||
.withPrimary(text: "alert.cannot_start.close".localized, action: { vc in
|
||||
vc.close(with: .alertFirstButtonReturn)
|
||||
exit(1)
|
||||
})
|
||||
.withSecondary(text: "startup.timeout.ignore".localized, action: { vc in
|
||||
vc.close(with: .alertSecondButtonReturn)
|
||||
})
|
||||
.withTertiary(text: "", action: { _ in
|
||||
NSWorkspace.shared.open(URL(string: "https://github.com/nicoverbruggen/phpmon/issues/294")!)
|
||||
})
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ class Startup {
|
||||
|
||||
// Set up a "background" timer on the main thread
|
||||
Task { @MainActor in
|
||||
startTimeoutTimer()
|
||||
startStartupTimer()
|
||||
}
|
||||
|
||||
for group in self.groups {
|
||||
@ -54,29 +54,6 @@ class Startup {
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
Displays an alert for when the application startup process takes too long.
|
||||
*/
|
||||
@MainActor @objc func startupTimeout() {
|
||||
NVAlert()
|
||||
.withInformation(
|
||||
title: "startup.timeout.title".localized,
|
||||
subtitle: "startup.timeout.subtitle".localized,
|
||||
description: "startup.timeout.description".localized
|
||||
)
|
||||
.withPrimary(text: "alert.cannot_start.close".localized, action: { vc in
|
||||
vc.close(with: .alertFirstButtonReturn)
|
||||
exit(1)
|
||||
})
|
||||
.withSecondary(text: "startup.timeout.ignore".localized, action: { vc in
|
||||
vc.close(with: .alertSecondButtonReturn)
|
||||
})
|
||||
.withTertiary(text: "", action: { _ in
|
||||
NSWorkspace.shared.open(URL(string: "https://github.com/nicoverbruggen/phpmon/issues/294")!)
|
||||
})
|
||||
.show()
|
||||
}
|
||||
|
||||
/**
|
||||
Displays an alert for a particular check. There are two types of alerts:
|
||||
- ones that require an app restart, which prompt the user to exit the app
|
||||
|
Reference in New Issue
Block a user