Presence: resilient local fallback
This commit is contained in:
@@ -58,12 +58,13 @@ final class InstancesStore: ObservableObject {
|
|||||||
self.isLoading = true
|
self.isLoading = true
|
||||||
defer { self.isLoading = false }
|
defer { self.isLoading = false }
|
||||||
do {
|
do {
|
||||||
|
PresenceReporter.shared.sendImmediate(reason: "instances-refresh")
|
||||||
let data = try await ControlChannel.shared.request(method: "system-presence")
|
let data = try await ControlChannel.shared.request(method: "system-presence")
|
||||||
self.lastPayload = data
|
self.lastPayload = data
|
||||||
if data.isEmpty {
|
if data.isEmpty {
|
||||||
self.logger.error("instances fetch returned empty payload")
|
self.logger.error("instances fetch returned empty payload")
|
||||||
self.instances = [self.localFallbackInstance()]
|
self.instances = [self.localFallbackInstance(reason: "no presence payload")]
|
||||||
self.lastError = "No presence data returned from relay yet."
|
self.lastError = nil
|
||||||
await self.probeHealthIfNeeded()
|
await self.probeHealthIfNeeded()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -82,7 +83,7 @@ final class InstancesStore: ObservableObject {
|
|||||||
ts: entry.ts)
|
ts: entry.ts)
|
||||||
}
|
}
|
||||||
if withIDs.isEmpty {
|
if withIDs.isEmpty {
|
||||||
self.instances = [self.localFallbackInstance()]
|
self.instances = [self.localFallbackInstance(reason: "no presence entries")]
|
||||||
self.lastError = nil
|
self.lastError = nil
|
||||||
await self.probeHealthIfNeeded()
|
await self.probeHealthIfNeeded()
|
||||||
} else {
|
} else {
|
||||||
@@ -96,13 +97,13 @@ final class InstancesStore: ObservableObject {
|
|||||||
len=\(self.lastPayload?.count ?? 0, privacy: .public) \
|
len=\(self.lastPayload?.count ?? 0, privacy: .public) \
|
||||||
utf8=\(self.snippet(self.lastPayload), privacy: .public)
|
utf8=\(self.snippet(self.lastPayload), privacy: .public)
|
||||||
""")
|
""")
|
||||||
self.instances = [self.localFallbackInstance()]
|
self.instances = [self.localFallbackInstance(reason: "presence decode failed")]
|
||||||
self.lastError = "Decode failed: \(error.localizedDescription)"
|
self.lastError = nil
|
||||||
await self.probeHealthIfNeeded()
|
await self.probeHealthIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func localFallbackInstance() -> InstanceInfo {
|
private func localFallbackInstance(reason: String) -> InstanceInfo {
|
||||||
let host = Host.current().localizedName ?? "this-mac"
|
let host = Host.current().localizedName ?? "this-mac"
|
||||||
let ip = Self.primaryIPv4Address()
|
let ip = Self.primaryIPv4Address()
|
||||||
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
|
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
|
||||||
@@ -115,7 +116,7 @@ final class InstancesStore: ObservableObject {
|
|||||||
version: version,
|
version: version,
|
||||||
lastInputSeconds: Self.lastInputSeconds(),
|
lastInputSeconds: Self.lastInputSeconds(),
|
||||||
mode: "local",
|
mode: "local",
|
||||||
reason: "fallback",
|
reason: reason,
|
||||||
text: text,
|
text: text,
|
||||||
ts: ts)
|
ts: ts)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user