fix: serialize ping state updates
This commit is contained in:
@@ -2,18 +2,15 @@ import Foundation
|
||||
import UserNotifications
|
||||
|
||||
enum PingService {
|
||||
private static var previousPingStates: [String: Bool] = [:]
|
||||
private static var suppressedUntil: [String: Date] = [:]
|
||||
private static let stateStore = PingStateStore()
|
||||
|
||||
static func suppressChecks(for hostname: String, duration: TimeInterval) {
|
||||
suppressedUntil[hostname] = Date().addingTimeInterval(duration)
|
||||
static func suppressChecks(for hostname: String, duration: TimeInterval) async {
|
||||
await stateStore.suppressChecks(for: hostname, duration: duration)
|
||||
}
|
||||
|
||||
static func ping(hostname: String, apiKey: String, notificationsEnabled: Bool = true) async -> Bool {
|
||||
if let suppressedUntil = suppressedUntil[hostname], suppressedUntil > Date() {
|
||||
if await stateStore.shouldSkipPing(for: hostname) {
|
||||
return false
|
||||
} else {
|
||||
suppressedUntil.removeValue(forKey: hostname)
|
||||
}
|
||||
|
||||
guard let url = URL(string: "https://\(hostname)/api/v2/ping") else {
|
||||
@@ -29,38 +26,32 @@ enum PingService {
|
||||
do {
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
|
||||
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
await handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
return false
|
||||
}
|
||||
|
||||
if let result = try? JSONDecoder().decode([String: String].self, from: data), result["response"] == "pong" {
|
||||
handlePingSuccess(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
await handlePingSuccess(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
return true
|
||||
} else {
|
||||
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
await handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
return false
|
||||
}
|
||||
} catch {
|
||||
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
await handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private static func handlePingSuccess(for hostname: String, notificationsEnabled: Bool) {
|
||||
let wasPreviouslyDown = previousPingStates[hostname] == false
|
||||
previousPingStates[hostname] = true
|
||||
|
||||
if wasPreviouslyDown && notificationsEnabled {
|
||||
sendNotification(title: "Server Online", body: "\(hostname) is now online")
|
||||
private static func handlePingSuccess(for hostname: String, notificationsEnabled: Bool) async {
|
||||
if let notification = await stateStore.recordSuccess(for: hostname, notificationsEnabled: notificationsEnabled) {
|
||||
sendNotification(title: notification.title, body: notification.body)
|
||||
}
|
||||
}
|
||||
|
||||
private static func handlePingFailure(for hostname: String, notificationsEnabled: Bool) {
|
||||
let wasPreviouslyUp = previousPingStates[hostname] != false
|
||||
previousPingStates[hostname] = false
|
||||
|
||||
if wasPreviouslyUp && notificationsEnabled {
|
||||
sendNotification(title: "Server Offline", body: "\(hostname) is offline")
|
||||
private static func handlePingFailure(for hostname: String, notificationsEnabled: Bool) async {
|
||||
if let notification = await stateStore.recordFailure(for: hostname, notificationsEnabled: notificationsEnabled) {
|
||||
sendNotification(title: notification.title, body: notification.body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,3 +65,55 @@ enum PingService {
|
||||
UNUserNotificationCenter.current().add(request)
|
||||
}
|
||||
}
|
||||
|
||||
private actor PingStateStore {
|
||||
private var previousPingStates: [String: Bool] = [:]
|
||||
private var suppressedUntil: [String: Date] = [:]
|
||||
|
||||
func suppressChecks(for hostname: String, duration: TimeInterval) {
|
||||
suppressedUntil[hostname] = Date().addingTimeInterval(duration)
|
||||
previousPingStates[hostname] = false
|
||||
}
|
||||
|
||||
func shouldSkipPing(for hostname: String) -> Bool {
|
||||
if let suppressedUntil = suppressedUntil[hostname], suppressedUntil > Date() {
|
||||
return true
|
||||
}
|
||||
|
||||
suppressedUntil.removeValue(forKey: hostname)
|
||||
return false
|
||||
}
|
||||
|
||||
func recordSuccess(for hostname: String, notificationsEnabled: Bool) -> PingNotification? {
|
||||
let wasPreviouslyDown = previousPingStates[hostname] == false
|
||||
previousPingStates[hostname] = true
|
||||
|
||||
guard wasPreviouslyDown, notificationsEnabled else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return PingNotification(
|
||||
title: "Server Online",
|
||||
body: "\(hostname) is now online"
|
||||
)
|
||||
}
|
||||
|
||||
func recordFailure(for hostname: String, notificationsEnabled: Bool) -> PingNotification? {
|
||||
let wasPreviouslyUp = previousPingStates[hostname] != false
|
||||
previousPingStates[hostname] = false
|
||||
|
||||
guard wasPreviouslyUp, notificationsEnabled else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return PingNotification(
|
||||
title: "Server Offline",
|
||||
body: "\(hostname) is offline"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private struct PingNotification {
|
||||
let title: String
|
||||
let body: String
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user