From 96975f8e57164e3f44c516ac638daddbc8cf37da Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Thu, 24 Jul 2025 12:32:22 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Clean=20up=20startup=20tim?= =?UTF-8?q?ers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Domain/App/Startup+Timers.swift | 52 +++++++++++++++++++++++--- phpmon/Domain/App/Startup.swift | 25 +------------ 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/phpmon/Domain/App/Startup+Timers.swift b/phpmon/Domain/App/Startup+Timers.swift index 4e2bc03..3d3fcbc 100644 --- a/phpmon/Domain/App/Startup+Timers.swift +++ b/phpmon/Domain/App/Startup+Timers.swift @@ -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() + } } diff --git a/phpmon/Domain/App/Startup.swift b/phpmon/Domain/App/Startup.swift index 7e4617e..ebc6c0c 100644 --- a/phpmon/Domain/App/Startup.swift +++ b/phpmon/Domain/App/Startup.swift @@ -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