Fixes aus dem Bundesliga-Einsatz
- Upload: kein Absturz mehr bei abreißender Verbindung (Restzeit-Berechnung lief über), SIGPIPE ignoriert; bei Netzwechsel werden laufende Übertragungen abgebrochen und auf dem neuen Netz automatisch neu gestartet (Versuch zählt nicht), ohne Netz warten sie; tote Verbindungen enden nach 45 s - LR-Export: Bilder, die sich auf der Platte ändern, werden neu eingelesen (vorher blieb der erste, unvollständige Stand stehen); nie leeres IPTC über das JPEG schreiben; Zuordnung auch mit altem Plugin - I-Fenster: Cursor in der Beschreibung steht oben (wie PM), nicht am Ende - ⌘A wählt alle Bilder, ⌘⌫ legt die Auswahl (RAW + JPEG + XMP) nach Rückfrage in den Papierkorb - Gesendet-Status getrennt: grünes FTP- und blaues Immich-Abzeichen, Filter je Ziel, Info in Lupe/I-Fenster - nach dem Ingest sind die neu importierten Bilder ausgewählt (direkt weiter nach Lightroom) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
1 parent
fff701674a
commit
b2210757dd
10 files changed
+298
-33
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import Foundation
|
||||
import Network
|
||||
import SwiftUI
|
||||
import LichtkastenCore
|
||||
|
||||
@@ -47,12 +48,58 @@ final class UploadQueue: ObservableObject {
|
||||
private var lastSample: (time: Date, bytes: Int64)?
|
||||
private var rateTimer: Timer?
|
||||
|
||||
// MARK: Network changes (Wi-Fi ↔ hotspot ↔ LAN in the stadium)
|
||||
/// No network: new transfers wait until it is back.
|
||||
@Published private(set) var networkPaused = false
|
||||
private let pathMonitor = NWPathMonitor()
|
||||
private var lastPathSignature: String?
|
||||
/// running transfers, so they can be stopped when the network changes
|
||||
private var runningTasks: [UUID: Task<Void, Never>] = [:]
|
||||
/// jobs stopped because of a network change – they start again by themselves, the attempt does not count
|
||||
private var interruptedByNetwork = Set<UUID>()
|
||||
private var resumeTask: Task<Void, Never>?
|
||||
|
||||
init() {
|
||||
pathMonitor.pathUpdateHandler = { [weak self] path in
|
||||
let signature = "\(path.status)|" + path.availableInterfaces.map(\.name).joined(separator: ",")
|
||||
let online = path.status == .satisfied
|
||||
Task { @MainActor [weak self] in self?.networkChanged(signature: signature, online: online) }
|
||||
}
|
||||
pathMonitor.start(queue: DispatchQueue(label: "lichtkasten.network"))
|
||||
}
|
||||
|
||||
/// The network changed: running transfers on the old connection would hang or die – stop them cleanly
|
||||
/// and start them again on the new connection (when there is one).
|
||||
private func networkChanged(signature: String, online: Bool) {
|
||||
defer { lastPathSignature = signature }
|
||||
guard let last = lastPathSignature, last != signature else { return }
|
||||
networkPaused = !online
|
||||
if !runningTasks.isEmpty {
|
||||
interruptedByNetwork.formUnion(runningTasks.keys)
|
||||
for t in runningTasks.values { t.cancel() }
|
||||
state?.statusText = online ? "Netzwerk gewechselt – laufende Uploads werden neu gestartet"
|
||||
: "Netzwerk weg – Uploads werden fortgesetzt, sobald es wieder da ist"
|
||||
}
|
||||
resumeTask?.cancel()
|
||||
guard online else { return }
|
||||
resumeTask = Task { [weak self] in
|
||||
try? await Task.sleep(nanoseconds: 2_000_000_000) // let the new connection settle (DHCP, DNS)
|
||||
guard !Task.isCancelled else { return }
|
||||
self?.immichClient = nil // fresh URLSession on the new network
|
||||
self?.pump()
|
||||
}
|
||||
}
|
||||
|
||||
var rateText: String {
|
||||
guard rate > 0 else { return "" }
|
||||
if networkPaused { return "Kein Netz – Uploads warten" }
|
||||
guard rate.isFinite, rate > 0 else { return "" }
|
||||
// a stalled connection lets the smoothed rate fall towards 0 → the ETA would be astronomically large
|
||||
// (Int(Double) overflow = crash). Below 1 KB/s it is shown as stalled instead.
|
||||
guard rate >= 1024 else { return "Verbindung stockt …" }
|
||||
let r = ByteCountFormatter.string(fromByteCount: Int64(rate), countStyle: .file) + "/s"
|
||||
let mbit = String(format: "%.1f Mbit/s", rate * 8 / 1_000_000)
|
||||
guard remainingBytes > 0 else { return "\(r) (\(mbit))" }
|
||||
let secs = Int(Double(remainingBytes) / rate)
|
||||
let secs = Int(min(Double(remainingBytes) / rate, 359_999))
|
||||
let eta = secs >= 3600 ? String(format: "%d:%02d:%02d", secs / 3600, secs % 3600 / 60, secs % 60)
|
||||
: String(format: "%d:%02d", secs / 60, secs % 60)
|
||||
return "\(r) (\(mbit)) · noch \(eta)"
|
||||
@@ -79,8 +126,9 @@ final class UploadQueue: ObservableObject {
|
||||
if let last = lastSample {
|
||||
let dt = now.timeIntervalSince(last.time)
|
||||
if dt > 0 {
|
||||
let instant = Double(sentCounter - last.bytes) / dt
|
||||
let instant = max(0, Double(sentCounter - last.bytes) / dt) // a restarted file must not go negative
|
||||
rate = rate == 0 ? instant : rate * 0.6 + instant * 0.4 // smoothing
|
||||
if rate < 1 { rate = 0 }
|
||||
}
|
||||
}
|
||||
lastSample = (now, sentCounter)
|
||||
@@ -178,12 +226,12 @@ final class UploadQueue: ObservableObject {
|
||||
}
|
||||
|
||||
private func pump() {
|
||||
while running < maxParallel, let idx = jobs.firstIndex(where: { $0.status == .waiting }) {
|
||||
while !networkPaused, running < maxParallel, let idx = jobs.firstIndex(where: { $0.status == .waiting }) {
|
||||
jobs[idx].status = .running
|
||||
jobs[idx].attempts += 1
|
||||
running += 1
|
||||
let job = jobs[idx]
|
||||
Task { await self.run(job) }
|
||||
runningTasks[job.id] = Task { await self.run(job) }
|
||||
}
|
||||
if running == 0 { finishImmichBatch() }
|
||||
}
|
||||
@@ -203,7 +251,7 @@ final class UploadQueue: ObservableObject {
|
||||
}
|
||||
|
||||
private func run(_ job: UploadJob) async {
|
||||
defer { running -= 1; pump() }
|
||||
defer { running -= 1; runningTasks[job.id] = nil; pump() }
|
||||
guard let state else { setStatus(job.id, .failed("App-Zustand nicht verfügbar")); return }
|
||||
startRateTimer()
|
||||
let jobID = job.id
|
||||
@@ -238,6 +286,13 @@ final class UploadQueue: ObservableObject {
|
||||
}
|
||||
} catch UploadError.remoteNameConflict(let name) {
|
||||
setStatus(job.id, .failed(UploadError.remoteNameConflict(name).localizedDescription))
|
||||
} catch where interruptedByNetwork.contains(job.id) {
|
||||
// stopped by a network change: wait for the new connection, this attempt does not count
|
||||
interruptedByNetwork.remove(job.id)
|
||||
if let i = jobs.firstIndex(where: { $0.id == job.id }) {
|
||||
jobs[i].status = .waiting
|
||||
jobs[i].attempts = max(0, jobs[i].attempts - 1)
|
||||
}
|
||||
} catch {
|
||||
if let i = jobs.firstIndex(where: { $0.id == job.id }), jobs[i].attempts < 3 {
|
||||
jobs[i].status = .waiting
|
||||
|
||||
Reference in new issue
Block a user